fix(editor): route elbow arrows with a single corner where one fits

Elbow arrows keep a padding around the elements they connect, and enter
the end element from the edge of it. When the end element is closer to the
start's side than that (e.g. just below it), the route stepped aside to that
edge first, with a few pixels' jog.

When the elements can be connected with a single corner instead (leaving the
start and entering the end the way they head, both legs clear of both
elements, the legs with arrowheads long enough to fit them), that route is
used, before (and instead of) routing on the grid.

As a leg without an arrowhead can be short now, changing the arrowheads of
an elbow arrow (from the picker, by flipping it, or by pasting styles)
routes it again, to make room for them.

Claude Opus 5.5 (xhigh)
This commit is contained in:
dwelle committed 2026-10-26 23:27:51 +01:00
1 parent b7de45e881
commit c4df37015b
5 files changed
+278 -25

No files matched your search

+132 -10
View File
@@ -52,7 +52,7 @@ import {
type ExcalidrawElbowArrowElement,
type NonDeletedSceneElementsMap,
} from "./types";
import { aabbForElement, pointInsideBounds } from "./bounds";
import { aabbForElement, getArrowheadSize, pointInsideBounds } from "./bounds";
import { getHoveredElementForBinding } from "./collision";
import type { Heading } from "./heading";
@@ -1102,15 +1102,28 @@ export const updateElbowArrowPoints = (
return normalizeArrowElementUpdate(
getElbowArrowCornerPoints(
removeElbowArrowShortSegments(
routeElbowArrow(arrow, {
startHeading,
endHeading,
startGlobalPoint,
endGlobalPoint,
hoveredStartElement,
hoveredEndElement,
...rest,
}) ?? [],
getOneCornerRoute(
arrow,
{
startHeading,
endHeading,
startGlobalPoint,
endGlobalPoint,
hoveredStartElement,
hoveredEndElement,
},
elementsMap,
) ??
routeElbowArrow(arrow, {
startHeading,
endHeading,
startGlobalPoint,
endGlobalPoint,
hoveredStartElement,
hoveredEndElement,
...rest,
}) ??
[],
),
),
fixedSegments,
@@ -1166,6 +1179,18 @@ export const updateElbowArrowPoints = (
);
};
/**
* The update routing an elbow arrow again where it is, e.g. once its
* arrowheads change, as its route makes room for them
*/
export const rerouteElbowArrow = <T extends ExcalidrawElbowArrowElement>(
arrow: Readonly<T>,
elementsMap: NonDeletedSceneElementsMap,
) =>
updateElbowArrowPoints(arrow, elementsMap, {
points: [arrow.points[0], arrow.points[arrow.points.length - 1]],
}) as ElementUpdate<T>;
/**
* Retrieves data necessary for calculating the elbow arrow path.
*
@@ -1425,6 +1450,103 @@ const getElbowArrowData = (
};
};
/** Whether an axis-aligned segment passes through `bounds` */
const segmentCrossesBounds = (
a: GlobalPoint,
b: GlobalPoint,
[minX, minY, maxX, maxY]: Bounds,
) =>
Math.max(a[0], b[0]) > minX &&
Math.min(a[0], b[0]) < maxX &&
Math.max(a[1], b[1]) > minY &&
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).
*/
const getOneCornerRoute = (
arrow: ElbowArrowState,
{
startHeading,
endHeading,
startGlobalPoint,
endGlobalPoint,
hoveredStartElement,
hoveredEndElement,
}: Pick<
ElbowArrowData,
| "startHeading"
| "endHeading"
| "startGlobalPoint"
| "endGlobalPoint"
| "hoveredStartElement"
| "hoveredEndElement"
>,
elementsMap: ElementsMap,
): GlobalPoint[] | null => {
if (
!hoveredStartElement ||
!hoveredEndElement ||
hoveredStartElement.id === hoveredEndElement.id ||
headingIsHorizontal(startHeading) === headingIsHorizontal(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];
// 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 (
firstLeg <=
(arrow.startArrowhead
? getArrowheadSize(arrow.startArrowhead)
: DEDUP_TRESHOLD) ||
lastLeg <=
(arrow.endArrowhead
? getArrowheadSize(arrow.endArrowhead)
: DEDUP_TRESHOLD)
) {
return null;
}
const gap = BASE_BINDING_GAP;
const startBounds = aabbForElement(hoveredStartElement, elementsMap, [
gap,
gap,
gap,
gap,
]);
const endBounds = aabbForElement(hoveredEndElement, elementsMap, [
gap,
gap,
gap,
gap,
]);
if (
segmentCrossesBounds(startGlobalPoint, corner, endBounds) ||
segmentCrossesBounds(corner, endGlobalPoint, startBounds)
) {
return null;
}
return [startGlobalPoint, corner, endGlobalPoint];
};
/**
* Generate the elbow arrow segments
*
+109 -1
View File
@@ -2,6 +2,7 @@ import { ARROW_TYPE, DEFAULT_ZOOM } from "@excalidraw/common";
import { pointFrom } from "@excalidraw/math";
import { Excalidraw } from "@excalidraw/excalidraw";
import { actionSelectAll } from "@excalidraw/excalidraw/actions";
import { actionChangeArrowhead } from "@excalidraw/excalidraw/actions/actionProperties";
import { actionDuplicateSelection } from "@excalidraw/excalidraw/actions/actionDuplicateSelection";
import { API } from "@excalidraw/excalidraw/tests/helpers/api";
import { Pointer, UI } from "@excalidraw/excalidraw/tests/helpers/ui";
@@ -13,7 +14,7 @@ import {
render,
} from "@excalidraw/excalidraw/tests/test-utils";
import "@excalidraw/utils/test-utils";
import { bindBindingElement } from "@excalidraw/element";
import { bindBindingElement, validateElbowPoints } from "@excalidraw/element";
import type { LocalPoint } from "@excalidraw/math";
@@ -428,3 +429,110 @@ describe("elbow arrow ui", () => {
]);
});
});
describe("elbow arrow one-corner routes", () => {
beforeEach(async () => {
localStorage.clear();
await render(<Excalidraw handleKeyboardGlobally={true} />);
});
const connect = (diamondY: number) => {
// the rectangle's right side's middle is at y = 127.5
UI.createElement("rectangle", { x: 0, y: 100, width: 120, height: 55 });
UI.createElement("diamond", {
x: 220,
y: diamondY,
width: 200,
height: 124,
});
UI.clickTool("arrow");
UI.clickOnTestId("elbow-arrow");
mouse.reset();
mouse.moveTo(120, 127.5);
mouse.click();
mouse.moveTo(320, diamondY);
mouse.click();
return h.elements.find(
(element) => element.type === "arrow",
) as ExcalidrawElbowArrowElement;
};
it("connects elements closer than the padding with a single corner", () => {
// 40px from the rectangle's side to the diamond's top: no step to the
// padding around the diamond on the way
const arrow = connect(167.5);
expect(arrow.points).toCloselyEqualPoints([
[0, 0],
[195, 0],
[195, 34.15],
]);
});
it("routes around the elements when a single corner doesn't fit", () => {
const arrow = connect(167.5);
// the rectangle below the diamond's top: the corner would be inside it
UI.clickTool("selection");
mouse.reset();
mouse.moveTo(60, 100);
mouse.down();
mouse.moveTo(60, 180);
mouse.up();
expect(arrow.points.length).toBeGreaterThan(3);
});
it("doesn't put the arrowhead on a leg too short for it", () => {
// 25px from the rectangle's side to the diamond's top
const arrow = connect(152.5);
expect(arrow.points.length).toBeGreaterThan(3);
});
it("routes again to fit an arrowhead added to a short leg", () => {
API.setAppState({ currentItemEndArrowhead: null });
// about 10px from the rectangle's side to the diamond's top
const arrow = connect(143.5);
expect(arrow.points).toCloselyEqualPoints([
[0, 0],
[195, 0],
[195, 10.15],
]);
act(() => {
h.app.actionManager.executeAction(actionChangeArrowhead, "ui", {
position: "end",
type: "arrow",
});
});
const updated = h.elements.find(
(element) => element.id === arrow.id,
) as ExcalidrawElbowArrowElement;
const [corner, end] = updated.points.slice(-2);
expect(updated.endArrowhead).toBe("arrow");
expect(end[1] - corner[1]).toBeGreaterThanOrEqual(25);
});
it("doesn't route a leg too short to keep its corner", () => {
// the arrow leaves the box above at x = 134, and enters the one below
// at x = 135
UI.createElement("rectangle", { x: 0, y: 0, width: 128, height: 100 });
UI.createElement("rectangle", { x: 100, y: 200, width: 128, height: 100 });
UI.clickTool("arrow");
UI.clickOnTestId("elbow-arrow");
mouse.reset();
mouse.moveTo(128, 50);
mouse.click();
mouse.moveTo(135, 200);
mouse.click();
const arrow = h.elements.find(
(element) => element.type === "arrow",
) as ExcalidrawElbowArrowElement;
expect(arrow.points.length).toBeGreaterThan(2);
expect(validateElbowPoints(arrow.points)).toBe(true);
});
});
+9 -2
View File
@@ -4,7 +4,11 @@ import { getCommonBoundingBox } from "@excalidraw/element";
import { newElementWith } from "@excalidraw/element";
import { deepCopyElement } from "@excalidraw/element";
import { resizeMultipleElements } from "@excalidraw/element";
import { isArrowElement, isElbowArrow } from "@excalidraw/element";
import {
isArrowElement,
isElbowArrow,
rerouteElbowArrow,
} from "@excalidraw/element";
import { updateFrameMembershipOfSelectedElements } from "@excalidraw/element";
import { CODES, KEYS, arrayToMap } from "@excalidraw/common";
@@ -121,10 +125,13 @@ const flipElements = (
) {
return selectedElements.map((element) => {
const _element = element as ExcalidrawArrowElement;
return newElementWith(_element, {
const flipped = newElementWith(_element, {
startArrowhead: _element.endArrowhead,
endArrowhead: _element.startArrowhead,
});
return isElbowArrow(flipped)
? newElementWith(flipped, rerouteElbowArrow(flipped, elementsMap))
: flipped;
});
}
@@ -72,6 +72,7 @@ import {
import {
updateElbowArrowPoints,
rerouteElbowArrow,
CaptureUpdateAction,
toggleLinePolygonState,
} from "@excalidraw/element";
@@ -1961,7 +1962,7 @@ export const actionChangeArrowhead = register<{
name: "changeArrowhead",
label: "Change arrowheads",
trackEvent: false,
perform: (elements, appState, value) => {
perform: (elements, appState, value, app) => {
invariant(value, "actionChangeArrowhead: value must be defined");
return {
@@ -1969,17 +1970,21 @@ export const actionChangeArrowhead = register<{
if (isLinearElement(el)) {
const { position, type } = value;
if (position === "start") {
const element: ExcalidrawLinearElement = newElementWith(el, {
startArrowhead: type,
});
return element;
} else if (position === "end") {
const element: ExcalidrawLinearElement = newElementWith(el, {
endArrowhead: type,
});
return element;
}
const element: ExcalidrawLinearElement = newElementWith(
el,
position === "start"
? { startArrowhead: type }
: { endArrowhead: type },
);
return isElbowArrow(element)
? newElementWith(
element,
rerouteElbowArrow(
element,
app.scene.getNonDeletedElementsMap(),
),
)
: element;
}
return el;
@@ -17,6 +17,7 @@ import {
getDefaultRoundnessTypeForElement,
isFrameLikeElement,
isArrowElement,
isElbowArrow,
isExcalidrawElement,
getColorUpdate,
isNonDeletedElement,
@@ -26,6 +27,7 @@ import {
getBaseFontSize,
getBaseFontSizeUpdate,
relayoutStickyNotes,
rerouteElbowArrow,
updateBoundElements,
} from "@excalidraw/element";
@@ -188,6 +190,15 @@ export const actionPasteStyles = register({
startArrowhead: elementStylesToCopyFrom.startArrowhead,
endArrowhead: elementStylesToCopyFrom.endArrowhead,
});
if (isElbowArrow(newElement)) {
newElement = newElementWith(
newElement,
rerouteElbowArrow(
newElement,
app.scene.getNonDeletedElementsMap(),
),
);
}
}
if (isFrameLikeElement(element)) {