Copy and paste styles (#219)

* copy and paste styles

* save copied styles in memory
This commit is contained in:
Faustino Kialungila 2020-01-06 23:22:48 +01:00 committed by GitHub
parent 8a91f4fe7b
commit 9305a33dba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -55,6 +55,8 @@ const KEYS = {
BACKSPACE: "Backspace" BACKSPACE: "Backspace"
}; };
let COPIED_STYLES: string = "{}";
function isArrowKey(keyCode: string) { function isArrowKey(keyCode: string) {
return ( return (
keyCode === KEYS.ARROW_LEFT || keyCode === KEYS.ARROW_LEFT ||
@ -192,7 +194,6 @@ class App extends React.Component<{}, AppState> {
} else if (event.metaKey && event.shiftKey && event.code === "KeyF") { } else if (event.metaKey && event.shiftKey && event.code === "KeyF") {
this.moveAllRight(); this.moveAllRight();
event.preventDefault(); event.preventDefault();
// Select all: Cmd-A // Select all: Cmd-A
} else if (event.metaKey && event.code === "KeyA") { } else if (event.metaKey && event.code === "KeyA") {
elements.forEach(element => { elements.forEach(element => {
@ -212,6 +213,29 @@ class App extends React.Component<{}, AppState> {
} }
this.forceUpdate(); this.forceUpdate();
event.preventDefault(); event.preventDefault();
// Copy Styles: Cmd-Shift-C
} else if (event.metaKey && event.shiftKey && event.code === "KeyC") {
const element = elements.find(el => el.isSelected);
if (element) {
COPIED_STYLES = JSON.stringify(element);
}
// Paste Styles: Cmd-Shift-V
} else if (event.metaKey && event.shiftKey && event.code === "KeyV") {
const pastedElement = JSON.parse(COPIED_STYLES);
if (pastedElement.type) {
elements.forEach(element => {
if (element.isSelected) {
element.backgroundColor = pastedElement?.backgroundColor;
element.strokeWidth = pastedElement?.strokeWidth;
element.strokeColor = pastedElement?.strokeColor;
element.fillStyle = pastedElement?.fillStyle;
element.opacity = pastedElement?.opacity;
generateDraw(element);
}
});
}
this.forceUpdate();
event.preventDefault();
} }
}; };