mirror of
https://github.com/excalidraw/excalidraw.git
synced 2026-12-06 11:12:51 +03:00
fix(editor): route elbow arrows with a single corner where one fits (#12458)
* 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) * 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) * fix(editor): keep elbow arrows' simple routes when their fixed segments go When an element moves so that an elbow arrow's segment lines up with its fixed one (e.g. moving the start element a grid step, onto the fixed segment's level), they're merged and the fixed segment dropped, and the arrow re-routed, as when a fixed segment is released. That re-route went straight to the grid, without trying the one or two corner route first, which left the jog that route avoids until the arrow was routed again. Any route on the grid now tries the simple route first. Claude Opus 5.5 (xhigh) * refactor(editor): route and clean elbow arrows in one place routeElbowArrow() now tries the one or two corner route, falls back to the grid, and removes short segments and points between collinear ones, so its three callers don't repeat that. They also pass getElbowArrowData()'s result through, rather than destructuring and rebuilding it. Claude Opus 5.5 (xhigh) * refactor(editor): name elbow arrow route getters and updates apart routeElbowArrow() returns a route's points, and rerouteElbowArrow() an element update routing the arrow again, so they're getElbowArrowRoute() and updateElbowArrowRoute() now (and the grid routing getElbowArrowGridRoute()). Claude Opus 5.5 (xhigh) * fix(editor): draw elbow arrows' jogs too short to see straight While an element bound to an elbow arrow is moved so that a fixed segment lines up with the ones around it, the arrow keeps a zero-length jog between them until it's released, and rough.js drew the jog's two corners as a knot. A segment shorter than 1px between two longer ones heading the same way is now drawn straight. Corners of short segments in a row, and short U-turns, keep their corners. Claude Opus 5.5 (xhigh) * fix(editor): keep elbow arrows' simple route legs long enough to round A leg of the one or two corner route without an arrowhead only had to outlast the cleanup of short segments (1px), so the route could leave stubs a few pixels long (e.g. from the side of one element to the facing side of another just below and beside it, with no arrowheads). Their corners lost their rounding, and the arrow looked like a line hugging the element, coming from the wrong side. Every leg is now longer than half the corner radius, keeping at least a quarter of the rounding, and one with an arrowhead also fits it. Shorter legs go to the grid routing instead. Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix(editor): keep elbow arrows' simple routes clear by the binding gap The one or two corner route kept a fixed 5px from the elements it connects, rather than the binding gap the arrow keeps at its ends (which grows with the element's stroke). Where an element's bounds reach past the point the arrow binds to (e.g. an ellipse or a diamond entered below its top), a leg could pass closer to it than that, and over a thick stroke. The route now keeps clear of each element by its binding gap. Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * refactor(editor): check elbow arrow route legs with doBoundsIntersect() segmentCrossesBounds() repeated doBoundsIntersect()'s overlap check, so it now passes it the segment's bounds instead. Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * refactor(editor): tidy up the elbow arrow route getters getSimpleRoute() worked out the last leg of a one corner route also for two corner ones, which then replaced it, and took a Pick<> of the data its only caller passes whole. getElbowArrowGridRoute()'s doc listed parameters it doesn't take. Signed-off-by: Mark Tolmacs <mark@lazycat.hu> --------- Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Co-authored-by: Mark Tolmacs <mark@lazycat.hu>
This commit is contained in:
1 parent
808d068e18
commit
c711e80ec5
7 files changed
+543
-87
No files matched your search
@@ -52,8 +52,14 @@ import {
|
||||
type ExcalidrawElbowArrowElement,
|
||||
type NonDeletedSceneElementsMap,
|
||||
} from "./types";
|
||||
import { aabbForElement, pointInsideBounds } from "./bounds";
|
||||
import {
|
||||
aabbForElement,
|
||||
doBoundsIntersect,
|
||||
getArrowheadSize,
|
||||
pointInsideBounds,
|
||||
} from "./bounds";
|
||||
import { getHoveredElementForBinding } from "./collision";
|
||||
import { ELBOW_ARROW_CORNER_RADIUS } from "./shape";
|
||||
|
||||
import type { Heading } from "./heading";
|
||||
import type {
|
||||
@@ -235,19 +241,16 @@ const handleSegmentRenormalization = (
|
||||
);
|
||||
if (filteredNextFixedSegments.length === 0) {
|
||||
return normalizeArrowElementUpdate(
|
||||
getElbowArrowCornerPoints(
|
||||
removeElbowArrowShortSegments(
|
||||
routeElbowArrow(
|
||||
arrow,
|
||||
getElbowArrowData(
|
||||
arrow,
|
||||
elementsMap,
|
||||
nextPoints.map((p) =>
|
||||
pointFrom<LocalPoint>(p[0] - arrow.x, p[1] - arrow.y),
|
||||
),
|
||||
),
|
||||
) ?? [],
|
||||
getElbowArrowRoute(
|
||||
arrow,
|
||||
getElbowArrowData(
|
||||
arrow,
|
||||
elementsMap,
|
||||
nextPoints.map((p) =>
|
||||
pointFrom<LocalPoint>(p[0] - arrow.x, p[1] - arrow.y),
|
||||
),
|
||||
),
|
||||
elementsMap,
|
||||
),
|
||||
filteredNextFixedSegments,
|
||||
null,
|
||||
@@ -308,15 +311,7 @@ const handleSegmentRelease = (
|
||||
const y = arrow.y + (prevSegment ? prevSegment.end[1] : 0);
|
||||
const startBinding = prevSegment ? null : arrow.startBinding;
|
||||
const endBinding = nextSegment ? null : arrow.endBinding;
|
||||
const {
|
||||
startHeading,
|
||||
endHeading,
|
||||
startGlobalPoint,
|
||||
endGlobalPoint,
|
||||
hoveredStartElement,
|
||||
hoveredEndElement,
|
||||
...rest
|
||||
} = getElbowArrowData(
|
||||
const elbowArrowData = getElbowArrowData(
|
||||
{
|
||||
x,
|
||||
y,
|
||||
@@ -342,19 +337,7 @@ const handleSegmentRelease = (
|
||||
);
|
||||
|
||||
const { points: restoredPoints } = normalizeArrowElementUpdate(
|
||||
getElbowArrowCornerPoints(
|
||||
removeElbowArrowShortSegments(
|
||||
routeElbowArrow(arrow, {
|
||||
startHeading,
|
||||
endHeading,
|
||||
startGlobalPoint,
|
||||
endGlobalPoint,
|
||||
hoveredStartElement,
|
||||
hoveredEndElement,
|
||||
...rest,
|
||||
}) ?? [],
|
||||
),
|
||||
),
|
||||
getElbowArrowRoute(arrow, elbowArrowData, elementsMap),
|
||||
fixedSegments,
|
||||
null,
|
||||
null,
|
||||
@@ -1031,15 +1014,7 @@ export const updateElbowArrowPoints = (
|
||||
);
|
||||
}
|
||||
|
||||
const {
|
||||
startHeading,
|
||||
endHeading,
|
||||
startGlobalPoint,
|
||||
endGlobalPoint,
|
||||
hoveredStartElement,
|
||||
hoveredEndElement,
|
||||
...rest
|
||||
} = getElbowArrowData(
|
||||
const elbowArrowData = getElbowArrowData(
|
||||
{
|
||||
x: arrow.x,
|
||||
y: arrow.y,
|
||||
@@ -1053,6 +1028,14 @@ export const updateElbowArrowPoints = (
|
||||
updatedPoints,
|
||||
options,
|
||||
);
|
||||
const {
|
||||
startHeading,
|
||||
endHeading,
|
||||
startGlobalPoint,
|
||||
endGlobalPoint,
|
||||
hoveredStartElement,
|
||||
hoveredEndElement,
|
||||
} = elbowArrowData;
|
||||
|
||||
// 0. During all element replacement in the scene, we just need to renormalize
|
||||
// the arrow
|
||||
@@ -1100,19 +1083,7 @@ export const updateElbowArrowPoints = (
|
||||
////
|
||||
if (fixedSegments.length === 0) {
|
||||
return normalizeArrowElementUpdate(
|
||||
getElbowArrowCornerPoints(
|
||||
removeElbowArrowShortSegments(
|
||||
routeElbowArrow(arrow, {
|
||||
startHeading,
|
||||
endHeading,
|
||||
startGlobalPoint,
|
||||
endGlobalPoint,
|
||||
hoveredStartElement,
|
||||
hoveredEndElement,
|
||||
...rest,
|
||||
}) ?? [],
|
||||
),
|
||||
),
|
||||
getElbowArrowRoute(arrow, elbowArrowData, elementsMap),
|
||||
fixedSegments,
|
||||
null,
|
||||
null,
|
||||
@@ -1166,6 +1137,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 updateElbowArrowRoute = <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,16 +1408,151 @@ const getElbowArrowData = (
|
||||
};
|
||||
};
|
||||
|
||||
/** Whether an axis-aligned segment passes through `bounds` */
|
||||
const segmentCrossesBounds = (a: GlobalPoint, b: GlobalPoint, bounds: Bounds) =>
|
||||
doBoundsIntersect(
|
||||
[
|
||||
Math.min(a[0], b[0]),
|
||||
Math.min(a[1], b[1]),
|
||||
Math.max(a[0], b[0]),
|
||||
Math.max(a[1], b[1]),
|
||||
],
|
||||
bounds,
|
||||
);
|
||||
|
||||
/**
|
||||
* Generate the elbow arrow segments
|
||||
*
|
||||
* @param arrow
|
||||
* @param elementsMap
|
||||
* @param nextPoints
|
||||
* @param options
|
||||
* @returns
|
||||
* 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 routeElbowArrow = (
|
||||
const getSimpleRoute = (
|
||||
arrow: ElbowArrowState,
|
||||
{
|
||||
startHeading,
|
||||
endHeading,
|
||||
startGlobalPoint,
|
||||
endGlobalPoint,
|
||||
hoveredStartElement,
|
||||
hoveredEndElement,
|
||||
}: ElbowArrowData,
|
||||
elementsMap: ElementsMap,
|
||||
): GlobalPoint[] | null => {
|
||||
const oneCorner =
|
||||
headingIsHorizontal(startHeading) !== headingIsHorizontal(endHeading);
|
||||
if (
|
||||
!hoveredStartElement ||
|
||||
!hoveredEndElement ||
|
||||
hoveredStartElement.id === hoveredEndElement.id ||
|
||||
(!oneCorner && !compareHeading(startHeading, flipHeading(endHeading)))
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 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: number;
|
||||
let lastLeg: number;
|
||||
if (oneCorner) {
|
||||
firstLeg = ahead;
|
||||
lastLeg =
|
||||
(startGlobalPoint[0] - endGlobalPoint[0]) * endHeading[0] +
|
||||
(startGlobalPoint[1] - endGlobalPoint[1]) * endHeading[1];
|
||||
} else {
|
||||
// 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 is to keep its corner rounded (at least a quarter of the radius,
|
||||
// rather than a stub hugging the element), and one with an arrowhead to
|
||||
// fit it too (not to have it reach around the corner)
|
||||
if (
|
||||
firstLeg <=
|
||||
Math.max(
|
||||
ELBOW_ARROW_CORNER_RADIUS / 2,
|
||||
arrow.startArrowhead ? getArrowheadSize(arrow.startArrowhead) : 0,
|
||||
) ||
|
||||
lastLeg <=
|
||||
Math.max(
|
||||
ELBOW_ARROW_CORNER_RADIUS / 2,
|
||||
arrow.endArrowhead ? getArrowheadSize(arrow.endArrowhead) : 0,
|
||||
)
|
||||
) {
|
||||
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 [startBounds, endBounds] = [hoveredStartElement, hoveredEndElement].map(
|
||||
(element) => {
|
||||
const gap = getBindingGap(element);
|
||||
return aabbForElement(element, elementsMap, [gap, gap, gap, gap]);
|
||||
},
|
||||
);
|
||||
// (the first leg leaves the start, and the last enters the end)
|
||||
if (
|
||||
segmentCrossesBounds(startGlobalPoint, firstTurn, endBounds) ||
|
||||
segmentCrossesBounds(firstTurn, lastTurn, startBounds) ||
|
||||
segmentCrossesBounds(firstTurn, lastTurn, endBounds) ||
|
||||
segmentCrossesBounds(lastTurn, endGlobalPoint, startBounds)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// (with one corner, both turns are at it, and merged later)
|
||||
return [startGlobalPoint, firstTurn, lastTurn, endGlobalPoint];
|
||||
};
|
||||
|
||||
/**
|
||||
* The elbow arrow's route: with one or two corners if that fits, or on the
|
||||
* grid otherwise, without short segments and the points between collinear
|
||||
* ones
|
||||
*/
|
||||
const getElbowArrowRoute = (
|
||||
arrow: ElbowArrowState,
|
||||
elbowArrowData: ElbowArrowData,
|
||||
elementsMap: ElementsMap,
|
||||
): GlobalPoint[] =>
|
||||
getElbowArrowCornerPoints(
|
||||
removeElbowArrowShortSegments(
|
||||
getSimpleRoute(arrow, elbowArrowData, elementsMap) ??
|
||||
getElbowArrowGridRoute(arrow, elbowArrowData) ??
|
||||
[],
|
||||
),
|
||||
);
|
||||
|
||||
/**
|
||||
* The elbow arrow's route on the grid, keeping a padding around the elements
|
||||
* it connects, or null if there's none
|
||||
*/
|
||||
const getElbowArrowGridRoute = (
|
||||
arrow: ElbowArrowState,
|
||||
elbowArrowData: ElbowArrowData,
|
||||
): GlobalPoint[] | null => {
|
||||
|
||||
@@ -1076,10 +1076,28 @@ const _generateElementShape = (
|
||||
|
||||
export const ELBOW_ARROW_CORNER_RADIUS = 16;
|
||||
|
||||
const generateElbowArrowShape = (
|
||||
points: readonly LocalPoint[],
|
||||
export const generateElbowArrowShape = (
|
||||
routePoints: readonly LocalPoint[],
|
||||
radius: number,
|
||||
) => {
|
||||
// a jog too short to see (e.g. a fixed segment lined up with the ones
|
||||
// around it, while dragging an element the arrow is bound to) is drawn
|
||||
// straight, as rough.js draws its two corners as a knot: a segment shorter
|
||||
// than 1px between two longer ones heading the same way (not a U-turn, nor
|
||||
// a corner of short segments)
|
||||
const isJog = (i: number) => {
|
||||
if (i < 2 || i > routePoints.length - 2) {
|
||||
return false;
|
||||
}
|
||||
const [a, b, c, d] = routePoints.slice(i - 2, i + 2);
|
||||
return (
|
||||
pointDistance(b, c) < 1 &&
|
||||
pointDistance(a, b) >= 1 &&
|
||||
pointDistance(c, d) >= 1 &&
|
||||
(b[0] - a[0]) * (d[0] - c[0]) + (b[1] - a[1]) * (d[1] - c[1]) > 0
|
||||
);
|
||||
};
|
||||
const points = routePoints.filter((_, i) => !isJog(i) && !isJog(i + 1));
|
||||
const subpoints = [] as [number, number][];
|
||||
for (let i = 1; i < points.length - 1; i += 1) {
|
||||
const prev = points[i - 1];
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -310,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],
|
||||
]);
|
||||
});
|
||||
@@ -428,3 +427,252 @@ describe("elbow arrow ui", () => {
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("elbow arrow routes with one or two corners", () => {
|
||||
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 = 139, a leg too short to round its corner
|
||||
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(139, 200);
|
||||
mouse.click();
|
||||
|
||||
const arrow = h.elements.find(
|
||||
(element) => element.type === "arrow",
|
||||
) as ExcalidrawElbowArrowElement;
|
||||
expect(arrow.points.length).toBeGreaterThan(3);
|
||||
expect(validateElbowPoints(arrow.points)).toBe(true);
|
||||
});
|
||||
|
||||
it("keeps clear of an element by its binding gap, thick strokes included", () => {
|
||||
const connectEllipse = (strokeWidth: number) => {
|
||||
API.setAppState({ currentItemEndArrowhead: null });
|
||||
// the ellipse's top is 12px below the rectangle's side's middle, and
|
||||
// the arrow enters its upper left, below its top
|
||||
UI.createElement("rectangle", { x: 0, y: 100, width: 120, height: 55 });
|
||||
UI.createElement("ellipse", {
|
||||
x: 220,
|
||||
y: 139.5,
|
||||
width: 200,
|
||||
height: 124,
|
||||
});
|
||||
API.updateElement(h.elements[1], { strokeWidth });
|
||||
UI.clickTool("arrow");
|
||||
UI.clickOnTestId("elbow-arrow");
|
||||
mouse.reset();
|
||||
mouse.moveTo(120, 127.5);
|
||||
mouse.click();
|
||||
mouse.moveTo(260, 151.9);
|
||||
mouse.click();
|
||||
|
||||
return h.elements.find(
|
||||
(element) => element.type === "arrow",
|
||||
) as ExcalidrawElbowArrowElement;
|
||||
};
|
||||
|
||||
// a binding gap of 6px: the corner above the ellipse fits
|
||||
expect(connectEllipse(2).points.length).toBe(3);
|
||||
|
||||
API.setElements([]);
|
||||
// 15px: the first leg would run over the stroke
|
||||
expect(connectEllipse(20).points.length).toBeGreaterThan(3);
|
||||
});
|
||||
|
||||
it("connects elements with a single corner when a fixed segment lines up with the start", () => {
|
||||
const arrow = connect(167.5);
|
||||
|
||||
// a step down, the arrow is routed around the diamond, and the segment
|
||||
// above it is fixed at the rectangle's previous middle
|
||||
UI.clickTool("selection");
|
||||
mouse.reset();
|
||||
mouse.moveTo(0, 120);
|
||||
mouse.down();
|
||||
mouse.moveTo(0, 140);
|
||||
mouse.up();
|
||||
h.app.scene.mutateElement(arrow, {
|
||||
fixedSegments: [
|
||||
{
|
||||
index: 3,
|
||||
start: pointFrom<LocalPoint>(44, -20),
|
||||
end: pointFrom<LocalPoint>(195, -20),
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(arrow.points).toCloselyEqualPoints([
|
||||
[0, 0],
|
||||
[44, 0],
|
||||
[44, -20],
|
||||
[195, -20],
|
||||
[195, 14.15],
|
||||
]);
|
||||
|
||||
// and back up, the first leg lines up with the fixed segment
|
||||
mouse.reset();
|
||||
mouse.moveTo(0, 140);
|
||||
mouse.down();
|
||||
mouse.moveTo(0, 120);
|
||||
mouse.up();
|
||||
|
||||
expect(arrow.fixedSegments ?? []).toEqual([]);
|
||||
expect(arrow.points).toCloselyEqualPoints([
|
||||
[0, 0],
|
||||
[195, 0],
|
||||
[195, 34.15],
|
||||
]);
|
||||
});
|
||||
|
||||
// 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);
|
||||
});
|
||||
});
|
||||
@@ -3,7 +3,11 @@ import { pointFrom } from "@excalidraw/math";
|
||||
|
||||
import type { LocalPoint } from "@excalidraw/math";
|
||||
|
||||
import { ShapeCache } from "../src/shape";
|
||||
import {
|
||||
ELBOW_ARROW_CORNER_RADIUS,
|
||||
generateElbowArrowShape,
|
||||
ShapeCache,
|
||||
} from "../src/shape";
|
||||
|
||||
import type { Op } from "roughjs/bin/core";
|
||||
|
||||
@@ -29,3 +33,48 @@ describe("arrowhead shapes", () => {
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("elbow arrow shapes", () => {
|
||||
const getCorners = (points: [number, number][]) =>
|
||||
generateElbowArrowShape(
|
||||
points.map(([x, y]) => pointFrom<LocalPoint>(x, y)),
|
||||
ELBOW_ARROW_CORNER_RADIUS,
|
||||
).match(/Q/g)?.length ?? 0;
|
||||
|
||||
it("draws a jog too short to see straight", () => {
|
||||
// a fixed segment lined up with the first one, while dragging
|
||||
expect(
|
||||
getCorners([
|
||||
[0, 0],
|
||||
[44, 0],
|
||||
[44, -1e-12],
|
||||
[195, -1e-12],
|
||||
[195, 34],
|
||||
]),
|
||||
).toBe(1);
|
||||
});
|
||||
|
||||
it("keeps the corners of short segments in a row", () => {
|
||||
expect(
|
||||
getCorners([
|
||||
[0, 0],
|
||||
[100, 0],
|
||||
[100, 0.5],
|
||||
[100.5, 0.5],
|
||||
[100.5, 100],
|
||||
]),
|
||||
).toBe(3);
|
||||
});
|
||||
|
||||
it("keeps a short U-turn", () => {
|
||||
expect(
|
||||
getCorners([
|
||||
[0, 0],
|
||||
[100, 0],
|
||||
[100, 0.5],
|
||||
[0, 0.5],
|
||||
[0, 50],
|
||||
]),
|
||||
).toBe(3);
|
||||
});
|
||||
});
|
||||
@@ -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,
|
||||
updateElbowArrowRoute,
|
||||
} 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, updateElbowArrowRoute(flipped, elementsMap))
|
||||
: flipped;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -72,6 +72,7 @@ import {
|
||||
|
||||
import {
|
||||
updateElbowArrowPoints,
|
||||
updateElbowArrowRoute,
|
||||
CaptureUpdateAction,
|
||||
toggleLinePolygonState,
|
||||
} from "@excalidraw/element";
|
||||
@@ -1963,7 +1964,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 {
|
||||
@@ -1971,17 +1972,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,
|
||||
updateElbowArrowRoute(
|
||||
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,
|
||||
updateElbowArrowRoute,
|
||||
updateBoundElements,
|
||||
} from "@excalidraw/element";
|
||||
|
||||
@@ -188,6 +190,15 @@ export const actionPasteStyles = register({
|
||||
startArrowhead: elementStylesToCopyFrom.startArrowhead,
|
||||
endArrowhead: elementStylesToCopyFrom.endArrowhead,
|
||||
});
|
||||
if (isElbowArrow(newElement)) {
|
||||
newElement = newElementWith(
|
||||
newElement,
|
||||
updateElbowArrowRoute(
|
||||
newElement,
|
||||
app.scene.getNonDeletedElementsMap(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (isFrameLikeElement(element)) {
|
||||
|
||||
Reference in new issue
Block a user