From ae9b64a623a75ad053b1ef48be7848af39a3b9a7 Mon Sep 17 00:00:00 2001 From: Faustino Kialungila Date: Mon, 16 Mar 2020 22:53:02 +0100 Subject: [PATCH] CMD + D to Duplicate Selection (#982) * cmd+d to duplicate selection * use duplicateElement instead * use duplicateElement instead * Update actionDuplicateSelection.ts * select the new duplicated element * add locale * use event.key instead of event.code Co-authored-by: David Luzar --- src/actions/actionDuplicateSelection.ts | 29 +++++++++++++++++++++++++ src/actions/index.ts | 1 + src/locales/en.json | 3 ++- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/actions/actionDuplicateSelection.ts diff --git a/src/actions/actionDuplicateSelection.ts b/src/actions/actionDuplicateSelection.ts new file mode 100644 index 00000000..d92e3b0c --- /dev/null +++ b/src/actions/actionDuplicateSelection.ts @@ -0,0 +1,29 @@ +import { KEYS } from "../keys"; +import { register } from "./register"; +import { ExcalidrawElement } from "../element/types"; +import { duplicateElement } from "../element"; + +export const actionDuplicateSelection = register({ + name: "duplicateSelection", + perform: (elements, appState) => { + return { + appState, + elements: elements.reduce( + (acc: Array, element: ExcalidrawElement) => { + if (appState.selectedElementIds[element.id]) { + const newElement = duplicateElement(element); + newElement.x = newElement.x + 10; + newElement.y = newElement.y + 10; + appState.selectedElementIds[newElement.id] = true; + delete appState.selectedElementIds[element.id]; + return acc.concat([element, newElement]); + } + return acc.concat(element); + }, + [], + ), + }; + }, + contextItemLabel: "labels.duplicateSelection", + keyTest: event => event[KEYS.CTRL_OR_CMD] && event.key === "d", +}); diff --git a/src/actions/index.ts b/src/actions/index.ts index f6d0e0ee..e9dcf88f 100644 --- a/src/actions/index.ts +++ b/src/actions/index.ts @@ -6,6 +6,7 @@ export { actionSendToBack, } from "./actionZindex"; export { actionSelectAll } from "./actionSelectAll"; +export { actionDuplicateSelection } from "./actionDuplicateSelection"; export { actionChangeStrokeColor, actionChangeBackgroundColor, diff --git a/src/locales/en.json b/src/locales/en.json index 5fcb6e9c..02ec5053 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -43,7 +43,8 @@ "drawingCanvas": "Drawing Canvas", "layers": "Layers", "language": "Language", - "createRoom": "Share a live-collaboration session" + "createRoom": "Share a live-collaboration session", + "duplicateSelection": "Duplicate selected elements" }, "buttons": { "clearReset": "Reset the canvas",