From 7ec30e56dae12372f52cf0e0a89bb295f0d0ff2a Mon Sep 17 00:00:00 2001 From: Steve Ruiz Date: Tue, 2 Jul 2024 09:27:52 +0100 Subject: [PATCH] Fix duplicate distance (#4056) This PR fixes the distance between duplicated shapes to match the editor.options.adjacentPositions value. ### Change type - [x] `bugfix` ### Release notes - Fixed a bug that caused the distance offset for duplicated shapes to not match other duplication distance offsets. --- .../tldraw/src/lib/ui/context/actions.tsx | 12 +++++------ packages/tldraw/src/test/duplicate.test.ts | 20 ++++++++++++++++++- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/packages/tldraw/src/lib/ui/context/actions.tsx b/packages/tldraw/src/lib/ui/context/actions.tsx index b9afd836d..16e8c0980 100644 --- a/packages/tldraw/src/lib/ui/context/actions.tsx +++ b/packages/tldraw/src/lib/ui/context/actions.tsx @@ -503,15 +503,15 @@ export function ActionsProvider({ overrides, children }: ActionsProviderProps) { } else { ids = editor.getSelectedShapeIds() const commonBounds = Box.Common(compact(ids.map((id) => editor.getShapePageBounds(id)))) - offset = !editor.getCameraOptions().isLocked + offset = editor.getCameraOptions().isLocked ? { - x: commonBounds.width + 20, - y: 0, + // same as the adjacent note margin + x: editor.options.adjacentShapeMargin, + y: editor.options.adjacentShapeMargin, } : { - // same as the adjacent note margin - x: 20, - y: 20, + x: commonBounds.width + editor.options.adjacentShapeMargin, + y: 0, } } diff --git a/packages/tldraw/src/test/duplicate.test.ts b/packages/tldraw/src/test/duplicate.test.ts index 4c651e78a..d8dc0ffbd 100644 --- a/packages/tldraw/src/test/duplicate.test.ts +++ b/packages/tldraw/src/test/duplicate.test.ts @@ -20,9 +20,27 @@ const ids = { beforeEach(() => { editor = new TestEditor() - editor.selectAll().deleteShapes(editor.getSelectedShapeIds()) }) + +it('duplicates a shape in the same place', () => { + editor.createShape({ id: ids.box1, type: 'geo', x: 0, y: 0, props: { w: 100, h: 100 } }) + editor.select(ids.box1) + editor.duplicateShapes([ids.box1]) + expect(editor.getCurrentPageShapes().length).toBe(2) + expect(editor.getShape(ids.box1)).toMatchObject({ x: 0, y: 0 }) + expect(editor.getLastCreatedShape()).toMatchObject({ x: 0, y: 0 }) +}) + +it('duplicates a shape with an offset', () => { + editor.createShape({ id: ids.box1, type: 'geo', x: 0, y: 0, props: { w: 100, h: 100 } }) + editor.select(ids.box1) + editor.duplicateShapes([ids.box1], { x: 10, y: 10 }) + expect(editor.getCurrentPageShapes().length).toBe(2) + expect(editor.getShape(ids.box1)).toMatchObject({ x: 0, y: 0 }) + expect(editor.getLastCreatedShape()).toMatchObject({ x: 10, y: 10 }) +}) + it('creates new bindings for arrows when pasting', async () => { editor .selectAll()