From 403e8bd3077ee17f3f44a196779e093978b902be Mon Sep 17 00:00:00 2001 From: Rene <1595098+ReneCode@users.noreply.github.com> Date: Sat, 8 Aug 2020 22:35:34 +0200 Subject: [PATCH] clear selection from copied/duplicatated group (#1973) Co-authored-by: rene_mbp Co-authored-by: dwelle --- src/actions/actionDuplicateSelection.tsx | 50 +++++++++++++++--------- src/actions/manager.tsx | 2 +- src/actions/types.ts | 2 +- src/components/App.tsx | 22 +++++++---- 4 files changed, 47 insertions(+), 29 deletions(-) diff --git a/src/actions/actionDuplicateSelection.tsx b/src/actions/actionDuplicateSelection.tsx index dce00b79..8c4f0dbd 100644 --- a/src/actions/actionDuplicateSelection.tsx +++ b/src/actions/actionDuplicateSelection.tsx @@ -10,6 +10,7 @@ import { t } from "../i18n"; import { getShortcutKey } from "../utils"; import { LinearElementEditor } from "../element/linearElementEditor"; import { mutateElement } from "../element/mutateElement"; +import { selectGroupsForSelectedElements } from "../groups"; export const actionDuplicateSelection = register({ name: "duplicateSelection", @@ -50,28 +51,39 @@ export const actionDuplicateSelection = register({ } const groupIdMap = new Map(); + const newElements: ExcalidrawElement[] = []; + const finalElements = elements.reduce( + (acc: Array, element: ExcalidrawElement) => { + if (appState.selectedElementIds[element.id]) { + const newElement = duplicateElement( + appState.editingGroupId, + groupIdMap, + element, + { + x: element.x + 10, + y: element.y + 10, + }, + ); + newElements.push(newElement); + return acc.concat([element, newElement]); + } + return acc.concat(element); + }, + [], + ); return { - appState, - elements: elements.reduce( - (acc: Array, element: ExcalidrawElement) => { - if (appState.selectedElementIds[element.id]) { - const newElement = duplicateElement( - appState.editingGroupId, - groupIdMap, - element, - { - x: element.x + 10, - y: element.y + 10, - }, - ); - appState.selectedElementIds[newElement.id] = true; - delete appState.selectedElementIds[element.id]; - return acc.concat([element, newElement]); - } - return acc.concat(element); + appState: selectGroupsForSelectedElements( + { + ...appState, + selectedGroupIds: {}, + selectedElementIds: newElements.reduce((acc, element) => { + acc[element.id] = true; + return acc; + }, {} as any), }, - [], + getNonDeletedElements(finalElements), ), + elements: finalElements, commitToHistory: true, }; }, diff --git a/src/actions/manager.tsx b/src/actions/manager.tsx index 865a4201..32b1eae5 100644 --- a/src/actions/manager.tsx +++ b/src/actions/manager.tsx @@ -15,7 +15,7 @@ export class ActionManager implements ActionsManagerInterface { updater: UpdaterFn; - getAppState: () => AppState; + getAppState: () => Readonly; getElementsIncludingDeleted: () => readonly ExcalidrawElement[]; diff --git a/src/actions/types.ts b/src/actions/types.ts index fbe7fba3..08b45f47 100644 --- a/src/actions/types.ts +++ b/src/actions/types.ts @@ -14,7 +14,7 @@ export type ActionResult = type ActionFn = ( elements: readonly ExcalidrawElement[], - appState: AppState, + appState: Readonly, formData: any, ) => ActionResult; diff --git a/src/components/App.tsx b/src/components/App.tsx index 5af3abff..5f77426a 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -956,19 +956,25 @@ class App extends React.Component { y: element.y + dy - minY, }); }); - this.scene.replaceAllElements([ ...this.scene.getElementsIncludingDeleted(), ...newElements, ]); history.resumeRecording(); - this.setState({ - isLibraryOpen: false, - selectedElementIds: newElements.reduce((map, element) => { - map[element.id] = true; - return map; - }, {} as any), - }); + this.setState( + selectGroupsForSelectedElements( + { + ...this.state, + isLibraryOpen: false, + selectedElementIds: newElements.reduce((map, element) => { + map[element.id] = true; + return map; + }, {} as any), + selectedGroupIds: {}, + }, + this.scene.getElements(), + ), + ); }; private addTextFromPaste(text: any) {