From 9a20ac3b8dbc648f466fbf2ddd326522e27e2b05 Mon Sep 17 00:00:00 2001 From: Mark Tolmacs Date: Tue, 11 Mar 2025 15:48:08 +0100 Subject: [PATCH] Insert after duplication --- packages/excalidraw/element/duplicate.ts | 25 ++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/excalidraw/element/duplicate.ts b/packages/excalidraw/element/duplicate.ts index fc4875dde..ac8844a9b 100644 --- a/packages/excalidraw/element/duplicate.ts +++ b/packages/excalidraw/element/duplicate.ts @@ -3,9 +3,14 @@ import { getNewGroupIdsForDuplication } from "../groups"; import { randomId, randomInteger } from "../random"; import type { AppState } from "../types"; import type { Mutable } from "../utility-types"; -import { arrayToMap, getUpdatedTimestamp, isTestEnv } from "../utils"; +import { + arrayToMap, + castArray, + getUpdatedTimestamp, + invariant, + isTestEnv, +} from "../utils"; import { bumpVersion } from "./mutateElement"; -import { isArrowElement } from "./typeChecks"; import type { ExcalidrawElement, GroupId } from "./types"; /** @@ -176,12 +181,28 @@ export const duplicateElements = ( clonedElement.frameId = maybeGetNewIdFor(clonedElement.frameId); } + insertAfterIndex(); + clonedElements.push(clonedElement); } return clonedElements; }; +const insertAfterIndex = ( + elementsWithClones: ExcalidrawElement[], + index: number, + elements: ExcalidrawElement | null | ExcalidrawElement[], +) => { + invariant(index !== -1, "targetIndex === -1 "); + + if (!Array.isArray(elements) && !elements) { + return; + } + + return elementsWithClones.splice(index + 1, 0, ...castArray(elements)); +}; + // Simplified deep clone for the purpose of cloning ExcalidrawElement. // // Only clones plain objects and arrays. Doesn't clone Date, RegExp, Map, Set,