2021-01-28 00:41:17 +05:30
|
|
|
import { CODES, KEYS } from "../keys";
|
|
|
|
import { register } from "./register";
|
|
|
|
import { copyToClipboard } from "../clipboard";
|
|
|
|
import { actionDeleteSelected } from "./actionDeleteSelected";
|
|
|
|
import { getSelectedElements } from "../scene/selection";
|
|
|
|
import { exportCanvas } from "../data/index";
|
|
|
|
import { getNonDeletedElements } 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",
|
2021-10-21 22:05:48 +02:00
|
|
|
perform: (elements, appState, _, app) => {
|
|
|
|
copyToClipboard(getNonDeletedElements(elements), 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",
|
|
|
|
perform: (elements, appState, data, app) => {
|
|
|
|
actionCopy.perform(elements, appState, data, app);
|
|
|
|
return actionDeleteSelected.perform(elements, appState, data, app);
|
|
|
|
},
|
|
|
|
contextItemLabel: "labels.cut",
|
|
|
|
keyTest: (event) => event[KEYS.CTRL_OR_CMD] && event.code === CODES.X,
|
|
|
|
});
|
|
|
|
|
|
|
|
export const actionCopyAsSvg = register({
|
|
|
|
name: "copyAsSvg",
|
|
|
|
perform: async (elements, appState, _data, app) => {
|
|
|
|
if (!app.canvas) {
|
|
|
|
return {
|
|
|
|
commitToHistory: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const selectedElements = getSelectedElements(
|
|
|
|
getNonDeletedElements(elements),
|
|
|
|
appState,
|
|
|
|
);
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
return {
|
|
|
|
appState: {
|
|
|
|
...appState,
|
|
|
|
errorMessage: error.message,
|
|
|
|
},
|
|
|
|
commitToHistory: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
contextItemLabel: "labels.copyAsSvg",
|
|
|
|
});
|
|
|
|
|
|
|
|
export const actionCopyAsPng = register({
|
|
|
|
name: "copyAsPng",
|
|
|
|
perform: async (elements, appState, _data, app) => {
|
|
|
|
if (!app.canvas) {
|
|
|
|
return {
|
|
|
|
commitToHistory: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const selectedElements = getSelectedElements(
|
|
|
|
getNonDeletedElements(elements),
|
|
|
|
appState,
|
|
|
|
);
|
|
|
|
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,
|
2021-03-03 17:45:10 +05:30
|
|
|
toastMessage: 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,
|
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
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
|
|
|
});
|