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,