2020-02-24 15:21:13 +01:00
|
|
|
import {
|
|
|
|
isTextElement,
|
|
|
|
isExcalidrawElement,
|
|
|
|
redrawTextBoundingBox,
|
|
|
|
} from "../element";
|
2020-01-19 13:21:33 -08:00
|
|
|
import { KEYS } from "../keys";
|
2020-02-24 16:29:54 +01:00
|
|
|
import { DEFAULT_FONT } from "../appState";
|
2020-03-07 10:20:38 -05:00
|
|
|
import { register } from "./register";
|
2020-03-09 22:34:50 -07:00
|
|
|
import { mutateTextElement } from "../element/mutateElement";
|
2020-01-12 02:22:03 +04:00
|
|
|
|
|
|
|
let copiedStyles: string = "{}";
|
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
export const actionCopyStyles = register({
|
2020-01-12 02:22:03 +04:00
|
|
|
name: "copyStyles",
|
2020-03-08 10:20:55 -07:00
|
|
|
perform: (elements, appState) => {
|
|
|
|
const element = elements.find(el => appState.selectedElementIds[el.id]);
|
2020-01-12 02:22:03 +04:00
|
|
|
if (element) {
|
|
|
|
copiedStyles = JSON.stringify(element);
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
},
|
2020-01-21 01:14:10 +02:00
|
|
|
contextItemLabel: "labels.copyStyles",
|
2020-03-09 15:06:35 +02:00
|
|
|
keyTest: event =>
|
|
|
|
event[KEYS.CTRL_OR_CMD] && event.shiftKey && event.key === "C",
|
2020-01-24 12:04:54 +02:00
|
|
|
contextMenuOrder: 0,
|
2020-03-07 10:20:38 -05:00
|
|
|
});
|
2020-01-12 02:22:03 +04:00
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
export const actionPasteStyles = register({
|
2020-01-12 02:22:03 +04:00
|
|
|
name: "pasteStyles",
|
2020-03-08 10:20:55 -07:00
|
|
|
perform: (elements, appState) => {
|
2020-01-12 02:22:03 +04:00
|
|
|
const pastedElement = JSON.parse(copiedStyles);
|
2020-02-24 15:21:13 +01:00
|
|
|
if (!isExcalidrawElement(pastedElement)) {
|
|
|
|
return { elements };
|
|
|
|
}
|
2020-01-12 02:22:03 +04:00
|
|
|
return {
|
|
|
|
elements: elements.map(element => {
|
2020-03-08 10:20:55 -07:00
|
|
|
if (appState.selectedElementIds[element.id]) {
|
2020-01-12 02:22:03 +04:00
|
|
|
const newElement = {
|
|
|
|
...element,
|
|
|
|
backgroundColor: pastedElement?.backgroundColor,
|
|
|
|
strokeWidth: pastedElement?.strokeWidth,
|
|
|
|
strokeColor: pastedElement?.strokeColor,
|
|
|
|
fillStyle: pastedElement?.fillStyle,
|
|
|
|
opacity: pastedElement?.opacity,
|
2020-01-24 12:04:54 +02:00
|
|
|
roughness: pastedElement?.roughness,
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
if (isTextElement(newElement)) {
|
2020-03-09 22:34:50 -07:00
|
|
|
mutateTextElement(newElement, newElement => {
|
|
|
|
newElement.font = pastedElement?.font || DEFAULT_FONT;
|
|
|
|
redrawTextBoundingBox(newElement);
|
|
|
|
});
|
2020-01-12 02:22:03 +04:00
|
|
|
}
|
|
|
|
return newElement;
|
|
|
|
}
|
|
|
|
return element;
|
2020-01-24 12:04:54 +02:00
|
|
|
}),
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
},
|
2020-02-05 22:47:10 +04:00
|
|
|
commitToHistory: () => true,
|
2020-01-21 01:14:10 +02:00
|
|
|
contextItemLabel: "labels.pasteStyles",
|
2020-03-09 15:06:35 +02:00
|
|
|
keyTest: event =>
|
|
|
|
event[KEYS.CTRL_OR_CMD] && event.shiftKey && event.key === "V",
|
2020-01-24 12:04:54 +02:00
|
|
|
contextMenuOrder: 1,
|
2020-03-07 10:20:38 -05:00
|
|
|
});
|