From 00af6dc63557c998eeee12a0e136e730e887d0a9 Mon Sep 17 00:00:00 2001 From: Faustino Kialungila Date: Mon, 6 Jan 2020 22:25:10 +0100 Subject: [PATCH] copy and paste styles --- src/index.tsx | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/index.tsx b/src/index.tsx index 32f4e314a..de6646e55 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -192,7 +192,6 @@ class App extends React.Component<{}, AppState> { } else if (event.metaKey && event.shiftKey && event.code === "KeyF") { this.moveAllRight(); event.preventDefault(); - // Select all: Cmd-A } else if (event.metaKey && event.code === "KeyA") { elements.forEach(element => { @@ -212,6 +211,47 @@ class App extends React.Component<{}, AppState> { } this.forceUpdate(); 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) { + if (!navigator.clipboard) return; + navigator.clipboard + .writeText(JSON.stringify(element)) + .catch(err => alert("Failed to copy shape style!")); + } + // 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); + 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; + generateDraw(element); + } + }); + } + }) + .then(() => { + this.forceUpdate(); + event.preventDefault(); + }) + .catch(err => alert("Failed to read shape style!")); } };