excalidraw/src/actions/actionStyles.ts
Gasim Gasimzada 74764b06eb Regenerate roughjs shape only when the item is updated (#316)
* Regenerate roughjs shape only when the item is updated

* Remove shape object during export and history undo/redo

* Remove shape element during copying

* Fix shape generation during creation
2020-01-11 16:00:00 -08:00

52 lines
1.5 KiB
TypeScript

import { Action } from "./types";
import { isTextElement, redrawTextBoundingBox } from "../element";
import { META_KEY } from "../keys";
let copiedStyles: string = "{}";
export const actionCopyStyles: Action = {
name: "copyStyles",
perform: elements => {
const element = elements.find(el => el.isSelected);
if (element) {
copiedStyles = JSON.stringify(element);
}
return {};
},
contextItemLabel: "Copy Styles",
keyTest: event => event[META_KEY] && event.shiftKey && event.code === "KeyC",
contextMenuOrder: 0
};
export const actionPasteStyles: Action = {
name: "pasteStyles",
perform: elements => {
const pastedElement = JSON.parse(copiedStyles);
return {
elements: elements.map(element => {
if (element.isSelected) {
const newElement = {
...element,
shape: null,
backgroundColor: pastedElement?.backgroundColor,
strokeWidth: pastedElement?.strokeWidth,
strokeColor: pastedElement?.strokeColor,
fillStyle: pastedElement?.fillStyle,
opacity: pastedElement?.opacity,
roughness: pastedElement?.roughness
};
if (isTextElement(newElement)) {
newElement.font = pastedElement?.font;
redrawTextBoundingBox(newElement);
}
return newElement;
}
return element;
})
};
},
contextItemLabel: "Paste Styles",
keyTest: event => event[META_KEY] && event.shiftKey && event.code === "KeyV",
contextMenuOrder: 1
};