fix(editor): route elbow arrows with two corners where they fit

When the arrow leaves the start element and enters the end one heading the
same way (e.g. from the bottom of one to the top of another below and to the
side of it), and they're closer than the padding the arrow keeps around
them, the route had a few pixels' jog between the two paddings.

Such arrows are now routed with two corners, the same way as with a single
one: turning halfway, as on the grid when there's room. When only one end
has an arrowhead, and halfway would leave it a shorter leg than the
padding, the turn moves toward the other end (keeping enough of that leg to
round its corner).

Claude Opus 5.5 (xhigh)
This commit is contained in:
dwelle committed 2026-10-26 23:27:51 +01:00
1 parent c4df37015b
commit b9388c223f
2 files changed
+128 -40

No files matched your search

+60 -35
View File
@@ -54,6 +54,7 @@ import {
} from "./types";
import { aabbForElement, getArrowheadSize, pointInsideBounds } from "./bounds";
import { getHoveredElementForBinding } from "./collision";
import { ELBOW_ARROW_CORNER_RADIUS } from "./shape";
import type { Heading } from "./heading";
import type {
@@ -1102,7 +1103,7 @@ export const updateElbowArrowPoints = (
return normalizeArrowElementUpdate(
getElbowArrowCornerPoints(
removeElbowArrowShortSegments(
getOneCornerRoute(
getSimpleRoute(
arrow,
{
startHeading,
@@ -1462,14 +1463,16 @@ const segmentCrossesBounds = (
Math.min(a[1], b[1]) < maxY;
/**
* The route between two bound elements with a single corner, if they can be
* connected with one: leaving the start element and entering the end one the
* way their headings tell, clear of both. Routed on the grid, the arrow keeps
* a fixed padding to the elements instead, and steps aside to reach it when
* they're closer than that (e.g. when the end element is just below the
* start's side).
* The route between two bound elements with one corner (when the arrow
* leaves one and enters the other at a right angle) or two (when it leaves
* and enters them heading the same way), if it fits: leaving the start
* element and entering the end one the way their headings tell, clear of
* both. Routed on the grid, the arrow keeps a fixed padding to the elements
* instead, and steps aside to reach it when they're closer than that (e.g.
* when the end element is just below the start's side, or just below the
* start).
*/
const getOneCornerRoute = (
const getSimpleRoute = (
arrow: ElbowArrowState,
{
startHeading,
@@ -1489,26 +1492,44 @@ const getOneCornerRoute = (
>,
elementsMap: ElementsMap,
): GlobalPoint[] | null => {
const oneCorner =
headingIsHorizontal(startHeading) !== headingIsHorizontal(endHeading);
if (
!hoveredStartElement ||
!hoveredEndElement ||
hoveredStartElement.id === hoveredEndElement.id ||
headingIsHorizontal(startHeading) === headingIsHorizontal(endHeading)
(!oneCorner && !compareHeading(startHeading, flipHeading(endHeading)))
) {
return null;
}
const corner = headingIsHorizontal(startHeading)
? pointFrom<GlobalPoint>(endGlobalPoint[0], startGlobalPoint[1])
: pointFrom<GlobalPoint>(startGlobalPoint[0], endGlobalPoint[1]);
// how far the corner is along the start's heading, and away from the end
// along its (headings point away from their element)
const firstLeg =
(corner[0] - startGlobalPoint[0]) * startHeading[0] +
(corner[1] - startGlobalPoint[1]) * startHeading[1];
const lastLeg =
(corner[0] - endGlobalPoint[0]) * endHeading[0] +
(corner[1] - endGlobalPoint[1]) * endHeading[1];
// with one corner, the legs are as long as the end is ahead of the start
// along the start's heading, and the start ahead of the end along the
// end's (headings point away from their element), and with two, they
// share the former
const ahead =
(endGlobalPoint[0] - startGlobalPoint[0]) * startHeading[0] +
(endGlobalPoint[1] - startGlobalPoint[1]) * startHeading[1];
let firstLeg = ahead;
let lastLeg =
(startGlobalPoint[0] - endGlobalPoint[0]) * endHeading[0] +
(startGlobalPoint[1] - endGlobalPoint[1]) * endHeading[1];
if (!oneCorner) {
// halfway, as the grid routing turns when there's room, unless that
// leaves an arrowhead a shorter leg than the padding, and the other end,
// without one, can spare some of its own (keeping enough to round its
// corner)
firstLeg = ahead / 2;
if (!arrow.startArrowhead !== !arrow.endArrowhead) {
const arrowheadLeg = Math.max(
ahead / 2,
Math.min(BASE_PADDING, ahead - ELBOW_ARROW_CORNER_RADIUS * 2),
);
firstLeg = arrow.startArrowhead ? arrowheadLeg : ahead - arrowheadLeg;
}
lastLeg = ahead - firstLeg;
}
// a leg with an arrowhead is to fit it (not to have it reach around the
// corner), and any to outlast the cleanup of short segments
if (
@@ -1524,27 +1545,31 @@ const getOneCornerRoute = (
return null;
}
const firstTurn = pointFrom<GlobalPoint>(
startGlobalPoint[0] + startHeading[0] * firstLeg,
startGlobalPoint[1] + startHeading[1] * firstLeg,
);
const lastTurn = pointFrom<GlobalPoint>(
endGlobalPoint[0] + endHeading[0] * lastLeg,
endGlobalPoint[1] + endHeading[1] * lastLeg,
);
const gap = BASE_BINDING_GAP;
const startBounds = aabbForElement(hoveredStartElement, elementsMap, [
gap,
gap,
gap,
gap,
]);
const endBounds = aabbForElement(hoveredEndElement, elementsMap, [
gap,
gap,
gap,
gap,
]);
const [startBounds, endBounds] = [hoveredStartElement, hoveredEndElement].map(
(element) => aabbForElement(element, elementsMap, [gap, gap, gap, gap]),
);
// (the first leg leaves the start, and the last enters the end)
if (
segmentCrossesBounds(startGlobalPoint, corner, endBounds) ||
segmentCrossesBounds(corner, endGlobalPoint, startBounds)
segmentCrossesBounds(startGlobalPoint, firstTurn, endBounds) ||
segmentCrossesBounds(firstTurn, lastTurn, startBounds) ||
segmentCrossesBounds(firstTurn, lastTurn, endBounds) ||
segmentCrossesBounds(lastTurn, endGlobalPoint, startBounds)
) {
return null;
}
return [startGlobalPoint, corner, endGlobalPoint];
// (with one corner, both turns are at it, and merged later)
return [startGlobalPoint, firstTurn, lastTurn, endGlobalPoint];
};
/**
+68 -5
View File
@@ -311,10 +311,8 @@ describe("elbow arrow ui", () => {
expect(arrow.points.map((point) => point.map(Math.round))).toEqual([
[0, 0],
[36, 0],
[36, 90],
[28, 90],
[28, 164],
[51, 0],
[51, 164],
[101, 164],
]);
});
@@ -430,7 +428,7 @@ describe("elbow arrow ui", () => {
});
});
describe("elbow arrow one-corner routes", () => {
describe("elbow arrow routes with one or two corners", () => {
beforeEach(async () => {
localStorage.clear();
await render(<Excalidraw handleKeyboardGlobally={true} />);
@@ -535,4 +533,69 @@ describe("elbow arrow one-corner routes", () => {
expect(arrow.points.length).toBeGreaterThan(2);
expect(validateElbowPoints(arrow.points)).toBe(true);
});
// a box, and another one `gap` px below and to the right of it, connected
// from the bottom of one to the top of the other
const connectBelow = (gap: number) => {
UI.createElement("rectangle", { x: 0, y: 0, width: 278, height: 135 });
UI.createElement("rectangle", {
x: 518,
y: 135 + gap,
width: 278,
height: 135,
});
UI.clickTool("arrow");
UI.clickOnTestId("elbow-arrow");
mouse.reset();
mouse.moveTo(139, 135);
mouse.click();
mouse.moveTo(657, 135 + gap);
mouse.click();
return h.elements.find(
(element) => element.type === "arrow",
) as ExcalidrawElbowArrowElement;
};
it("connects elements closer than the padding with two corners, turning halfway", () => {
API.setAppState({ currentItemEndArrowhead: null });
const arrow = connectBelow(84);
expect(arrow.points).toCloselyEqualPoints([
[0, 0],
[0, 36],
[518, 36],
[518, 72],
]);
});
it("turns closer to the end without an arrowhead to leave the arrowhead the padding", () => {
const arrow = connectBelow(84);
// 72px apart: the leg to the arrowhead gets the padding, and the other
// leg the rest, still enough to round its corner
expect(arrow.points).toCloselyEqualPoints([
[0, 0],
[0, 32],
[518, 32],
[518, 72],
]);
});
it("turns halfway as on the grid when there's room", () => {
const arrow = connectBelow(120);
expect(arrow.points).toCloselyEqualPoints([
[0, 0],
[0, 54],
[518, 54],
[518, 108],
]);
});
it("doesn't put the arrowhead on a half too short for it", () => {
const arrow = connectBelow(40);
expect(arrow.points.length).toBeGreaterThan(4);
});
});