From 2fb5c4cd13713058f53e7bf9c845d090de04aa0c Mon Sep 17 00:00:00 2001 From: Faustino Kialungila Date: Tue, 7 Jan 2020 08:04:15 +0100 Subject: [PATCH] Add styles copy and pasting in the context menu (#227) --- src/index.tsx | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 70ebcdc5..2af36f01 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -62,7 +62,7 @@ const META_KEY = /Mac|iPod|iPhone|iPad/.test(window.navigator.platform) ? "metaKey" : "ctrlKey"; -let COPIED_STYLES: string = "{}"; +let copiedStyles: string = "{}"; function isArrowKey(keyCode: string) { return ( @@ -222,26 +222,10 @@ class App extends React.Component<{}, AppState> { 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); - } + this.copyStyles(); // 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(); + this.pasteStyles(); event.preventDefault(); } }; @@ -263,6 +247,29 @@ class App extends React.Component<{}, AppState> { } }; + private copyStyles = () => { + const element = elements.find(el => el.isSelected); + if (element) { + copiedStyles = JSON.stringify(element); + } + }; + + private pasteStyles = () => { + const pastedElement = JSON.parse(copiedStyles); + 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; + element.roughness = pastedElement?.roughness; + generateDraw(element); + } + }); + this.forceUpdate(); + }; + private moveAllLeft = () => { moveAllLeft(elements, getSelectedIndices(elements)); this.forceUpdate(); @@ -621,6 +628,8 @@ class App extends React.Component<{}, AppState> { label: "Paste", action: () => this.pasteFromClipboard(x, y) }, + { label: "Copy Styles", action: this.copyStyles }, + { label: "Paste Styles", action: this.pasteStyles }, { label: "Delete", action: this.deleteSelectedElements }, { label: "Move Forward", action: this.moveOneRight }, { label: "Send to Front", action: this.moveAllRight },