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.
This commit is contained in:
Mark Tolmacs committed 2026-10-23 15:09:59 +00:00
1 parent 45276e6a94
commit eb7e4b0d09
3 files changed
+62 -71

No files matched your search

@@ -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<Number>,
"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<Number>,
"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<Number>,
"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<Number>,
"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<Number>,
"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<Number>,
"width": 100,
"x": 100.5,
"x": 100,
"y": 400,
}
`;
@@ -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<LocalPoint>(0, 0),
pointFrom<LocalPoint>(300, 100),
],
start: { id: "A" },
},
{
type: "arrow",
id: "unbound",
x: 100,
y: 300,
points: [pointFrom<LocalPoint>(0, 0), pointFrom<LocalPoint>(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],
],
});
});
});
-58
View File
@@ -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<readonly LocalPoint[]>(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,