From a5a74be45de102a21a073ec306820a4e53152cde Mon Sep 17 00:00:00 2001 From: Mark Tolmacs Date: Wed, 9 Apr 2025 14:20:21 +0200 Subject: [PATCH] Refactor Signed-off-by: Mark Tolmacs --- packages/element/src/linearElementEditor.ts | 14 ++++++++++++++ packages/excalidraw/components/App.tsx | 14 +------------- .../tests/__snapshots__/history.test.tsx.snap | 6 +++--- .../__snapshots__/multiPointCreate.test.tsx.snap | 4 ++-- .../__snapshots__/regressionTests.test.tsx.snap | 4 ++-- .../excalidraw/tests/multiPointCreate.test.tsx | 8 ++++---- 6 files changed, 26 insertions(+), 24 deletions(-) diff --git a/packages/element/src/linearElementEditor.ts b/packages/element/src/linearElementEditor.ts index 9758d37a50..325a0587c2 100644 --- a/packages/element/src/linearElementEditor.ts +++ b/packages/element/src/linearElementEditor.ts @@ -469,6 +469,7 @@ export class LinearElementEditor { editingLinearElement: LinearElementEditor, appState: AppState, scene: Scene, + shouldBind?: boolean, ): LinearElementEditor { const elementsMap = scene.getNonDeletedElementsMap(); const elements = scene.getNonDeletedElements(); @@ -531,6 +532,19 @@ export class LinearElementEditor { } } + if (shouldBind) { + const element = scene.getElement(editingLinearElement.elementId); + if (isBindingElement(element) && isBindingEnabled(appState)) { + bindOrUnbindLinearElement( + element, + bindings.startBindingElement || "keep", + bindings.endBindingElement || "keep", + elementsMap, + scene, + ); + } + } + return { ...editingLinearElement, ...bindings, diff --git a/packages/excalidraw/components/App.tsx b/packages/excalidraw/components/App.tsx index 1b6dd6100c..547163ef9e 100644 --- a/packages/excalidraw/components/App.tsx +++ b/packages/excalidraw/components/App.tsx @@ -8789,21 +8789,9 @@ class App extends React.Component { this.state.selectedLinearElement, this.state, this.scene, + true, ); - const { startBindingElement, endBindingElement } = - linearElementEditor; - const element = this.scene.getElement(linearElementEditor.elementId); - if (isBindingElement(element)) { - bindOrUnbindLinearElement( - element, - startBindingElement, - endBindingElement, - elementsMap, - this.scene, - ); - } - if (linearElementEditor !== this.state.selectedLinearElement) { this.setState({ selectedLinearElement: { diff --git a/packages/excalidraw/tests/__snapshots__/history.test.tsx.snap b/packages/excalidraw/tests/__snapshots__/history.test.tsx.snap index 05611e8aa1..9c78fbef91 100644 --- a/packages/excalidraw/tests/__snapshots__/history.test.tsx.snap +++ b/packages/excalidraw/tests/__snapshots__/history.test.tsx.snap @@ -7500,7 +7500,7 @@ History { exports[`history > multiplayer undo/redo > should iterate through the history when selected or editing linear element was remotely deleted > [end of test] number of elements 1`] = `1`; -exports[`history > multiplayer undo/redo > should iterate through the history when selected or editing linear element was remotely deleted > [end of test] number of renders 1`] = `10`; +exports[`history > multiplayer undo/redo > should iterate through the history when selected or editing linear element was remotely deleted > [end of test] number of renders 1`] = `11`; exports[`history > multiplayer undo/redo > should iterate through the history when when element change relates to remotely deleted element > [end of test] appState 1`] = ` { @@ -10571,7 +10571,7 @@ History { exports[`history > multiplayer undo/redo > should override remotely added points on undo, but restore them on redo > [end of test] number of elements 1`] = `1`; -exports[`history > multiplayer undo/redo > should override remotely added points on undo, but restore them on redo > [end of test] number of renders 1`] = `15`; +exports[`history > multiplayer undo/redo > should override remotely added points on undo, but restore them on redo > [end of test] number of renders 1`] = `16`; exports[`history > multiplayer undo/redo > should redistribute deltas when element gets removed locally but is restored remotely > [end of test] appState 1`] = ` { @@ -20198,4 +20198,4 @@ History { exports[`history > singleplayer undo/redo > should support linear element creation and points manipulation through the editor > [end of test] number of elements 1`] = `1`; -exports[`history > singleplayer undo/redo > should support linear element creation and points manipulation through the editor > [end of test] number of renders 1`] = `21`; +exports[`history > singleplayer undo/redo > should support linear element creation and points manipulation through the editor > [end of test] number of renders 1`] = `22`; diff --git a/packages/excalidraw/tests/__snapshots__/multiPointCreate.test.tsx.snap b/packages/excalidraw/tests/__snapshots__/multiPointCreate.test.tsx.snap index 1b312a5512..2a8af91d30 100644 --- a/packages/excalidraw/tests/__snapshots__/multiPointCreate.test.tsx.snap +++ b/packages/excalidraw/tests/__snapshots__/multiPointCreate.test.tsx.snap @@ -50,7 +50,7 @@ exports[`multi point mode in linear elements > arrow 3`] = ` "type": "arrow", "updated": 1, "version": 8, - "versionNonce": 1604849351, + "versionNonce": 1505387817, "width": 70, "x": 30, "y": 30, @@ -106,7 +106,7 @@ exports[`multi point mode in linear elements > line 3`] = ` "type": "line", "updated": 1, "version": 8, - "versionNonce": 1604849351, + "versionNonce": 1505387817, "width": 70, "x": 30, "y": 30, diff --git a/packages/excalidraw/tests/__snapshots__/regressionTests.test.tsx.snap b/packages/excalidraw/tests/__snapshots__/regressionTests.test.tsx.snap index 319287792c..b5bcc65389 100644 --- a/packages/excalidraw/tests/__snapshots__/regressionTests.test.tsx.snap +++ b/packages/excalidraw/tests/__snapshots__/regressionTests.test.tsx.snap @@ -6835,7 +6835,7 @@ History { exports[`regression tests > draw every type of shape > [end of test] number of elements 1`] = `0`; -exports[`regression tests > draw every type of shape > [end of test] number of renders 1`] = `33`; +exports[`regression tests > draw every type of shape > [end of test] number of renders 1`] = `35`; exports[`regression tests > given a group of selected elements with an element that is not selected inside the group common bounding box when element that is not selected is clicked should switch selection to not selected element on pointer up > [end of test] appState 1`] = ` { @@ -14566,7 +14566,7 @@ History { exports[`regression tests > undo/redo drawing an element > [end of test] number of elements 1`] = `0`; -exports[`regression tests > undo/redo drawing an element > [end of test] number of renders 1`] = `20`; +exports[`regression tests > undo/redo drawing an element > [end of test] number of renders 1`] = `21`; exports[`regression tests > updates fontSize & fontFamily appState > [end of test] appState 1`] = ` { diff --git a/packages/excalidraw/tests/multiPointCreate.test.tsx b/packages/excalidraw/tests/multiPointCreate.test.tsx index cde3c7f983..49c82df205 100644 --- a/packages/excalidraw/tests/multiPointCreate.test.tsx +++ b/packages/excalidraw/tests/multiPointCreate.test.tsx @@ -118,8 +118,8 @@ describe("multi point mode in linear elements", () => { key: KEYS.ENTER, }); - expect(renderInteractiveScene.mock.calls.length).toMatchInlineSnapshot(`7`); - expect(renderStaticScene.mock.calls.length).toMatchInlineSnapshot(`7`); + expect(renderInteractiveScene.mock.calls.length).toMatchInlineSnapshot(`8`); + expect(renderStaticScene.mock.calls.length).toMatchInlineSnapshot(`8`); expect(h.elements.length).toEqual(1); const element = h.elements[0] as ExcalidrawLinearElement; @@ -161,8 +161,8 @@ describe("multi point mode in linear elements", () => { fireEvent.keyDown(document, { key: KEYS.ENTER, }); - expect(renderInteractiveScene.mock.calls.length).toMatchInlineSnapshot(`7`); - expect(renderStaticScene.mock.calls.length).toMatchInlineSnapshot(`7`); + expect(renderInteractiveScene.mock.calls.length).toMatchInlineSnapshot(`8`); + expect(renderStaticScene.mock.calls.length).toMatchInlineSnapshot(`8`); expect(h.elements.length).toEqual(1); const element = h.elements[0] as ExcalidrawLinearElement;