diff --git a/src/element/index.ts b/src/element/index.ts index ca53c437..3348ba03 100644 --- a/src/element/index.ts +++ b/src/element/index.ts @@ -1,4 +1,4 @@ -export { newElement } from "./newElement"; +export { newElement, duplicateElement } from "./newElement"; export { getElementAbsoluteCoords, getDiamondPoints, diff --git a/src/element/newElement.ts b/src/element/newElement.ts index 57d2c954..de5369db 100644 --- a/src/element/newElement.ts +++ b/src/element/newElement.ts @@ -32,3 +32,10 @@ export function newElement( }; return element; } + +export function duplicateElement(element: ReturnType) { + const copy = { ...element }; + copy.id = nanoid(); + copy.seed = randomSeed(); + return copy; +} diff --git a/src/index.tsx b/src/index.tsx index 270128f7..86413b6c 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -4,7 +4,13 @@ import rough from "roughjs/bin/wrappers/rough"; import { moveOneLeft, moveAllLeft, moveOneRight, moveAllRight } from "./zindex"; import { randomSeed } from "./random"; -import { newElement, resizeTest, isTextElement, textWysiwyg } from "./element"; +import { + newElement, + duplicateElement, + resizeTest, + isTextElement, + textWysiwyg +} from "./element"; import { clearSelection, getSelectedIndices, @@ -44,7 +50,6 @@ import { PanelCanvas } from "./components/panels/PanelCanvas"; const { elements } = createScene(); const { history } = createHistory(); - const DEFAULT_PROJECT_NAME = `excalidraw-${getDateTime()}`; const CANVAS_WINDOW_OFFSET_LEFT = 250; @@ -683,7 +688,7 @@ class App extends React.Component<{}, AppState> { elements.push( ...elements.reduce((duplicates, element) => { if (element.isSelected) { - duplicates.push({ ...element }); + duplicates.push(duplicateElement(element)); element.isSelected = false; } return duplicates; @@ -1030,10 +1035,10 @@ class App extends React.Component<{}, AppState> { const dy = y - minY; parsedElements.forEach(parsedElement => { - parsedElement.x += dx; - parsedElement.y += dy; - parsedElement.seed = randomSeed(); - elements.push(parsedElement); + const duplicate = duplicateElement(parsedElement); + duplicate.x += dx; + duplicate.y += dy; + elements.push(duplicate); }); this.forceUpdate(); }