From eb7e4b0d09a9b0ccfc69fc3391c47052efb5fcfd Mon Sep 17 00:00:00 2001 From: Mark Tolmacs Date: Fri, 23 Oct 2026 09:25:38 +0000 Subject: [PATCH] fix(element): don't nudge skeleton arrow ends by 0.5px bindLinearElementToElement() moved every skeleton arrow's start 0.5px towards its end and its end 0.5px towards its start, "so bindings don't overlap with start/end bound element coordinates" (#7195). The placement pass now re-places every bound end with updateBoundArrow(), which puts it off the bound element by the binding gap, so for bound ends the nudge was dead work (a clone, nudge and normalize that's overwritten). It only still had an effect on ends nothing re-places: the unbound end of half-bound arrows, and both ends of unbound arrows (including those whose start/end reference missing ids, which bind nothing). Reproduction (convertToExcalidrawElements, regenerateIds: false): - fully bound: rect A (0,0,100x100), rect B (400,0,100x100), arrow at (100,50) points [0,0]->[300,0], start A, end B - half bound: rect A, arrow at (100,50) points [0,0]->[300,100], start A - half bound: rect B, arrow at (100,50) points [0,0]->[300,0], end B - start: { id: "nope" } (missing), and an arrow with no start/end, both at (100,50) points [0,0]->[300,0] Failure case (observed -> expected): - fully bound: x 106, points [0,0]->[288,0], version 5 both with and without the nudge (it's fully overwritten) - half bound (start): unbound end at (399.5, 149.5) -> (400, 150) - half bound (end): unbound start at x 100.5 -> 100 - missing ref / unbound: x 100.5, points [0,0]->[299,0] while width stays 300 (stale, getNormalizeElementPointsAndCoords() doesn't update it) -> x 100, points [0,0]->[300,0], width 300 Fix: remove the nudge (and the single point early return that only guarded it), so ends are either placed by their bindings or kept where the skeleton puts them. Bound ends can't be left unplaced: the bound elements are always added to the element store, and updateBoundPoint() only skips degenerate arrows the nudge didn't change either. Snapshot changes: the unbound arrows in "should transform linear elements" and "should transform to labelled arrows when label provided for arrows" now keep x as given (100/450 instead of 100.5/450.5) and points spanning their width ([0,0]->[100,0] instead of [99,0]). Their labels don't move (the arrow midpoints are unchanged). The "should bind arrows to existing elements if ids are correct" assertion, whose ids are missing and so bind nothing, now expects x 255 as given (was 255.5). Adds a regression test that a half-bound arrow's unbound end and an unbound arrow stay where the skeleton puts them. Review finding 9. --- .../__snapshots__/transform.test.ts.snap | 24 ++++---- .../element/src/__tests__/transform.test.ts | 51 +++++++++++++++- packages/element/src/transform.ts | 58 ------------------- 3 files changed, 62 insertions(+), 71 deletions(-) diff --git a/packages/element/src/__tests__/__snapshots__/transform.test.ts.snap b/packages/element/src/__tests__/__snapshots__/transform.test.ts.snap index e373a5bc65..a160613d3b 100644 --- a/packages/element/src/__tests__/__snapshots__/transform.test.ts.snap +++ b/packages/element/src/__tests__/__snapshots__/transform.test.ts.snap @@ -906,7 +906,7 @@ exports[`Test Transform > should transform linear elements 1`] = ` 0, ], [ - 99, + 100, 0, ], ], @@ -923,7 +923,7 @@ exports[`Test Transform > should transform linear elements 1`] = ` "version": 2, "versionNonce": Any, "width": 100, - "x": 100.5, + "x": 100, "y": 20, } `; @@ -954,7 +954,7 @@ exports[`Test Transform > should transform linear elements 2`] = ` 0, ], [ - 99, + 100, 0, ], ], @@ -971,7 +971,7 @@ exports[`Test Transform > should transform linear elements 2`] = ` "version": 2, "versionNonce": Any, "width": 100, - "x": 450.5, + "x": 450, "y": 20, } `; @@ -1971,7 +1971,7 @@ exports[`Test Transform > should transform to labelled arrows when label provide 0, ], [ - 99, + 100, 0, ], ], @@ -1988,7 +1988,7 @@ exports[`Test Transform > should transform to labelled arrows when label provide "version": 2, "versionNonce": Any, "width": 100, - "x": 100.5, + "x": 100, "y": 100, } `; @@ -2024,7 +2024,7 @@ exports[`Test Transform > should transform to labelled arrows when label provide 0, ], [ - 99, + 100, 0, ], ], @@ -2041,7 +2041,7 @@ exports[`Test Transform > should transform to labelled arrows when label provide "version": 2, "versionNonce": Any, "width": 100, - "x": 100.5, + "x": 100, "y": 200, } `; @@ -2077,7 +2077,7 @@ exports[`Test Transform > should transform to labelled arrows when label provide 0, ], [ - 99, + 100, 0, ], ], @@ -2094,7 +2094,7 @@ exports[`Test Transform > should transform to labelled arrows when label provide "version": 2, "versionNonce": Any, "width": 100, - "x": 100.5, + "x": 100, "y": 300, } `; @@ -2130,7 +2130,7 @@ exports[`Test Transform > should transform to labelled arrows when label provide 0, ], [ - 99, + 100, 0, ], ], @@ -2147,7 +2147,7 @@ exports[`Test Transform > should transform to labelled arrows when label provide "version": 2, "versionNonce": Any, "width": 100, - "x": 100.5, + "x": 100, "y": 400, } `; diff --git a/packages/element/src/__tests__/transform.test.ts b/packages/element/src/__tests__/transform.test.ts index cead13026f..825bd69abc 100644 --- a/packages/element/src/__tests__/transform.test.ts +++ b/packages/element/src/__tests__/transform.test.ts @@ -756,7 +756,7 @@ describe("Test Transform", () => { const [, , arrow, text] = excalidrawElements; expect(arrow).toMatchObject({ type: "arrow", - x: 255.5, + x: 255, y: 239, boundElements: [ { @@ -1601,4 +1601,53 @@ describe("Test Transform", () => { expect(label.y).toBeCloseTo(y); } }); + + it("should keep arrows' unbound ends where they're given", () => { + const elements = convertToExcalidrawElements( + [ + { type: "rectangle", id: "A", x: 0, y: 0, width: 100, height: 100 }, + { + type: "arrow", + id: "half-bound", + x: 100, + y: 50, + points: [ + pointFrom(0, 0), + pointFrom(300, 100), + ], + start: { id: "A" }, + }, + { + type: "arrow", + id: "unbound", + x: 100, + y: 300, + points: [pointFrom(0, 0), pointFrom(300, 0)], + }, + ], + opts, + ); + const elementsMap = arrayToMap(elements); + const halfBound = elementsMap.get("half-bound") as ExcalidrawArrowElement; + const unbound = elementsMap.get("unbound") as ExcalidrawArrowElement; + + // placed off A by the binding gap + expect(halfBound.x).toBeGreaterThan(100); + const [endX, endY] = LinearElementEditor.getPointAtIndexGlobalCoordinates( + halfBound, + -1, + elementsMap, + ); + expect(endX).toBeCloseTo(400); + expect(endY).toBeCloseTo(150); + expect(unbound).toMatchObject({ + x: 100, + y: 300, + width: 300, + points: [ + [0, 0], + [300, 0], + ], + }); + }); }); diff --git a/packages/element/src/transform.ts b/packages/element/src/transform.ts index 234a8352e7..fb518a6ee6 100644 --- a/packages/element/src/transform.ts +++ b/packages/element/src/transform.ts @@ -499,64 +499,6 @@ const bindLinearElementToElement = ( } } - // Safe check to early return for single point - if (linearElement.points.length < 2) { - return { - linearElement, - startBoundElement, - endBoundElement, - }; - } - - // Update start/end points by 0.5 so bindings don't overlap with start/end bound element coordinates. - const endPointIndex = linearElement.points.length - 1; - const delta = 0.5; - - const newPoints = cloneJSON(linearElement.points); - - // left to right so shift the arrow towards right - if ( - linearElement.points[endPointIndex][0] > - linearElement.points[endPointIndex - 1][0] - ) { - newPoints[0][0] = delta; - newPoints[endPointIndex][0] -= delta; - } - - // right to left so shift the arrow towards left - if ( - linearElement.points[endPointIndex][0] < - linearElement.points[endPointIndex - 1][0] - ) { - newPoints[0][0] = -delta; - newPoints[endPointIndex][0] += delta; - } - // top to bottom so shift the arrow towards top - if ( - linearElement.points[endPointIndex][1] > - linearElement.points[endPointIndex - 1][1] - ) { - newPoints[0][1] = delta; - newPoints[endPointIndex][1] -= delta; - } - - // bottom to top so shift the arrow towards bottom - if ( - linearElement.points[endPointIndex][1] < - linearElement.points[endPointIndex - 1][1] - ) { - newPoints[0][1] = -delta; - newPoints[endPointIndex][1] += delta; - } - - Object.assign( - linearElement, - LinearElementEditor.getNormalizeElementPointsAndCoords({ - ...linearElement, - points: newPoints, - }), - ); - return { linearElement, startBoundElement,