Insert after duplication

This commit is contained in:
Mark Tolmacs 2025-03-11 15:48:08 +01:00
parent c4767a3144
commit 9a20ac3b8d

View file

@ -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,