save copied styles in memory

This commit is contained in:
Faustino Kialungila 2020-01-06 23:20:17 +01:00
parent 00af6dc635
commit 510f4101bb

View file

@ -55,6 +55,8 @@ const KEYS = {
BACKSPACE: "Backspace"
};
let COPIED_STYLES: string = "{}";
function isArrowKey(keyCode: string) {
return (
keyCode === KEYS.ARROW_LEFT ||
@ -215,43 +217,25 @@ class App extends React.Component<{}, AppState> {
} else if (event.metaKey && event.shiftKey && event.code === "KeyC") {
const element = elements.find(el => el.isSelected);
if (element) {
if (!navigator.clipboard) return;
navigator.clipboard
.writeText(JSON.stringify(element))
.catch(err => alert("Failed to copy shape style!"));
COPIED_STYLES = JSON.stringify(element);
}
// Paste Styles: Cmd-Shift-V
} else if (event.metaKey && event.shiftKey && event.code === "KeyV") {
if (!navigator.clipboard) return;
navigator.clipboard
.readText()
.then(stringifiedPastedElement => {
const pastedElement = JSON.parse(stringifiedPastedElement);
const pastedElement = JSON.parse(COPIED_STYLES);
if (pastedElement.type) {
const {
strokeColor,
strokeWidth,
backgroundColor,
fillStyle,
opacity
} = pastedElement;
elements.forEach(element => {
if (element.isSelected) {
element.backgroundColor = backgroundColor;
element.strokeWidth = strokeWidth;
element.strokeColor = strokeColor;
element.fillStyle = fillStyle;
element.opacity = opacity;
element.backgroundColor = pastedElement?.backgroundColor;
element.strokeWidth = pastedElement?.strokeWidth;
element.strokeColor = pastedElement?.strokeColor;
element.fillStyle = pastedElement?.fillStyle;
element.opacity = pastedElement?.opacity;
generateDraw(element);
}
});
}
})
.then(() => {
this.forceUpdate();
event.preventDefault();
})
.catch(err => alert("Failed to read shape style!"));
}
};