excalidraw/src/actions/actionStyles.ts

52 lines
1.5 KiB
TypeScript
Raw Normal View History

import { Action } from "./types";
import { isTextElement, redrawTextBoundingBox } from "../element";
2020-01-19 13:21:33 -08:00
import { KEYS } 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: "labels.copyStyles",
2020-01-19 13:21:33 -08:00
keyTest: event => event[KEYS.META] && event.shiftKey && event.code === "KeyC",
2020-01-24 12:04:54 +02:00
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,
2020-01-24 12:04:54 +02:00
roughness: pastedElement?.roughness,
};
if (isTextElement(newElement)) {
newElement.font = pastedElement?.font;
redrawTextBoundingBox(newElement);
}
return newElement;
}
return element;
2020-01-24 12:04:54 +02:00
}),
};
},
contextItemLabel: "labels.pasteStyles",
2020-01-19 13:21:33 -08:00
keyTest: event => event[KEYS.META] && event.shiftKey && event.code === "KeyV",
2020-01-24 12:04:54 +02:00
contextMenuOrder: 1,
};