2021-01-28 00:41:17 +05:30
|
|
|
import { CODES, KEYS } from "../keys";
|
|
|
|
import { register } from "./register";
|
2022-04-05 23:11:00 +02:00
|
|
|
import {
|
|
|
|
copyTextToSystemClipboard,
|
|
|
|
copyToClipboard,
|
|
|
|
probablySupportsClipboardWriteText,
|
|
|
|
} from "../clipboard";
|
2021-01-28 00:41:17 +05:30
|
|
|
import { actionDeleteSelected } from "./actionDeleteSelected";
|
|
|
|
import { getSelectedElements } from "../scene/selection";
|
|
|
|
import { exportCanvas } from "../data/index";
|
2022-04-05 21:48:59 +02:00
|
|
|
import { getNonDeletedElements, isTextElement } from "../element";
|
2021-01-30 10:30:00 +01:00
|
|
|
import { t } from "../i18n";
|
2021-01-28 00:41:17 +05:30
|
|
|
|
|
|
|
export const actionCopy = register({
|
|
|
|
name: "copy",
|
2022-03-28 14:46:40 +02:00
|
|
|
trackEvent: { category: "element" },
|
2021-10-21 22:05:48 +02:00
|
|
|
perform: (elements, appState, _, app) => {
|
2022-04-29 18:58:44 +05:30
|
|
|
const selectedElements = getSelectedElements(elements, appState, true);
|
|
|
|
|
|
|
|
copyToClipboard(selectedElements, appState, app.files);
|
2021-01-28 00:41:17 +05:30
|
|
|
|
|
|
|
return {
|
|
|
|
commitToHistory: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
contextItemLabel: "labels.copy",
|
2021-03-19 18:36:23 +01:00
|
|
|
// don't supply a shortcut since we handle this conditionally via onCopy event
|
|
|
|
keyTest: undefined,
|
2021-01-28 00:41:17 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
export const actionCut = register({
|
|
|
|
name: "cut",
|
2022-03-28 14:46:40 +02:00
|
|
|
trackEvent: { category: "element" },
|
2021-01-28 00:41:17 +05:30
|
|
|
perform: (elements, appState, data, app) => {
|
|
|
|
actionCopy.perform(elements, appState, data, app);
|
2022-01-13 19:53:22 +01:00
|
|
|
return actionDeleteSelected.perform(elements, appState);
|
2021-01-28 00:41:17 +05:30
|
|
|
},
|
|
|
|
contextItemLabel: "labels.cut",
|
|
|
|
keyTest: (event) => event[KEYS.CTRL_OR_CMD] && event.code === CODES.X,
|
|
|
|
});
|
|
|
|
|
|
|
|
export const actionCopyAsSvg = register({
|
|
|
|
name: "copyAsSvg",
|
2022-03-28 14:46:40 +02:00
|
|
|
trackEvent: { category: "element" },
|
2021-01-28 00:41:17 +05:30
|
|
|
perform: async (elements, appState, _data, app) => {
|
|
|
|
if (!app.canvas) {
|
|
|
|
return {
|
|
|
|
commitToHistory: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const selectedElements = getSelectedElements(
|
|
|
|
getNonDeletedElements(elements),
|
|
|
|
appState,
|
2021-12-16 21:14:03 +05:30
|
|
|
true,
|
2021-01-28 00:41:17 +05:30
|
|
|
);
|
|
|
|
try {
|
|
|
|
await exportCanvas(
|
|
|
|
"clipboard-svg",
|
|
|
|
selectedElements.length
|
|
|
|
? selectedElements
|
|
|
|
: getNonDeletedElements(elements),
|
|
|
|
appState,
|
2021-10-21 22:05:48 +02:00
|
|
|
app.files,
|
2021-01-28 00:41:17 +05:30
|
|
|
appState,
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
commitToHistory: false,
|
|
|
|
};
|
2021-11-02 14:24:16 +02:00
|
|
|
} catch (error: any) {
|
2021-01-28 00:41:17 +05:30
|
|
|
console.error(error);
|
|
|
|
return {
|
|
|
|
appState: {
|
|
|
|
...appState,
|
|
|
|
errorMessage: error.message,
|
|
|
|
},
|
|
|
|
commitToHistory: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
contextItemLabel: "labels.copyAsSvg",
|
|
|
|
});
|
|
|
|
|
|
|
|
export const actionCopyAsPng = register({
|
|
|
|
name: "copyAsPng",
|
2022-03-28 14:46:40 +02:00
|
|
|
trackEvent: { category: "element" },
|
2021-01-28 00:41:17 +05:30
|
|
|
perform: async (elements, appState, _data, app) => {
|
|
|
|
if (!app.canvas) {
|
|
|
|
return {
|
|
|
|
commitToHistory: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const selectedElements = getSelectedElements(
|
|
|
|
getNonDeletedElements(elements),
|
|
|
|
appState,
|
2021-12-16 21:14:03 +05:30
|
|
|
true,
|
2021-01-28 00:41:17 +05:30
|
|
|
);
|
|
|
|
try {
|
|
|
|
await exportCanvas(
|
|
|
|
"clipboard",
|
|
|
|
selectedElements.length
|
|
|
|
? selectedElements
|
|
|
|
: getNonDeletedElements(elements),
|
|
|
|
appState,
|
2021-10-21 22:05:48 +02:00
|
|
|
app.files,
|
2021-01-28 00:41:17 +05:30
|
|
|
appState,
|
|
|
|
);
|
|
|
|
return {
|
2021-01-30 10:30:00 +01:00
|
|
|
appState: {
|
|
|
|
...appState,
|
2022-07-11 18:11:13 +05:30
|
|
|
toast: {
|
|
|
|
message: t("toast.copyToClipboardAsPng", {
|
|
|
|
exportSelection: selectedElements.length
|
|
|
|
? t("toast.selection")
|
|
|
|
: t("toast.canvas"),
|
|
|
|
exportColorScheme: appState.exportWithDarkMode
|
|
|
|
? t("buttons.darkMode")
|
|
|
|
: t("buttons.lightMode"),
|
|
|
|
}),
|
|
|
|
},
|
2021-01-30 10:30:00 +01:00
|
|
|
},
|
2021-01-28 00:41:17 +05:30
|
|
|
commitToHistory: false,
|
|
|
|
};
|
2021-11-02 14:24:16 +02:00
|
|
|
} catch (error: any) {
|
2021-01-28 00:41:17 +05:30
|
|
|
console.error(error);
|
|
|
|
return {
|
|
|
|
appState: {
|
|
|
|
...appState,
|
|
|
|
errorMessage: error.message,
|
|
|
|
},
|
|
|
|
commitToHistory: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
contextItemLabel: "labels.copyAsPng",
|
2021-01-30 10:30:00 +01:00
|
|
|
keyTest: (event) => event.code === CODES.C && event.altKey && event.shiftKey,
|
2021-01-28 00:41:17 +05:30
|
|
|
});
|
2022-04-05 15:31:19 +02:00
|
|
|
|
2022-04-05 23:11:00 +02:00
|
|
|
export const copyText = register({
|
|
|
|
name: "copyText",
|
2022-04-05 15:31:19 +02:00
|
|
|
trackEvent: { category: "element" },
|
2022-04-05 21:48:59 +02:00
|
|
|
perform: (elements, appState) => {
|
|
|
|
const selectedElements = getSelectedElements(
|
|
|
|
getNonDeletedElements(elements),
|
|
|
|
appState,
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
|
2022-04-05 23:11:00 +02:00
|
|
|
const text = selectedElements
|
|
|
|
.reduce((acc: string[], element) => {
|
|
|
|
if (isTextElement(element)) {
|
|
|
|
acc.push(element.text);
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, [])
|
|
|
|
.join("\n\n");
|
2022-04-05 15:31:19 +02:00
|
|
|
copyTextToSystemClipboard(text);
|
|
|
|
return {
|
|
|
|
commitToHistory: false,
|
|
|
|
};
|
|
|
|
},
|
2022-04-05 23:11:00 +02:00
|
|
|
contextItemPredicate: (elements, appState) => {
|
|
|
|
return (
|
|
|
|
probablySupportsClipboardWriteText &&
|
|
|
|
getSelectedElements(elements, appState, true).some(isTextElement)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
contextItemLabel: "labels.copyText",
|
2022-04-05 15:31:19 +02:00
|
|
|
});
|