mirror of
https://github.com/excalidraw/excalidraw.git
synced 2026-11-29 16:40:40 +03:00
fix: Stubs
This commit is contained in:
1 parent
bc398e4f61
commit
a4f6ace07f
4 files changed
+329
-143
No files matched your search
@@ -44,7 +44,7 @@ import {
|
||||
HEADING_UP,
|
||||
} from "./heading";
|
||||
import { isBindableElement } from "./typeChecks";
|
||||
import { aabbForElement, elementCenterPoint, getArrowheadSize } from "./bounds";
|
||||
import { aabbForElement, elementCenterPoint } from "./bounds";
|
||||
import { getHoveredElementForBinding } from "./collision";
|
||||
import { ELBOW_ARROW_CORNER_RADIUS } from "./shape";
|
||||
import {
|
||||
@@ -339,12 +339,6 @@ export const routeElbowArrow = (
|
||||
? DIRECTIONS[(directionIndex(boundEndHeading) + 1) % 4]
|
||||
: boundEndHeading;
|
||||
|
||||
// room to round the corner, or for an arrowhead and some of the rounding
|
||||
const minLeg = (arrowhead: Arrowhead | null) =>
|
||||
arrowhead
|
||||
? getArrowheadSize(arrowhead) + ELBOW_ARROW_CORNER_RADIUS / 4
|
||||
: ELBOW_ARROW_CORNER_RADIUS / 2;
|
||||
|
||||
const frame = frameFor(startGlobalPoint, startHeading, endHeading);
|
||||
// the elements' outlines (with their binding gap, and with the padding
|
||||
// around that) in the frame, or the endpoints themselves where they're not
|
||||
@@ -415,8 +409,8 @@ export const routeElbowArrow = (
|
||||
end: toFrame(frame, endGlobalPoint),
|
||||
facing:
|
||||
facingVector[0] === -1 ? "left" : facingVector[0] === 1 ? "right" : "up",
|
||||
startMinLeg: minLeg(arrow.startArrowhead),
|
||||
endMinLeg: minLeg(arrow.endArrowhead),
|
||||
startArrowhead: !!arrow.startArrowhead,
|
||||
endArrowhead: !!arrow.endArrowhead,
|
||||
sameElement,
|
||||
radius: ELBOW_ARROW_CORNER_RADIUS,
|
||||
});
|
||||
@@ -462,9 +456,13 @@ export const getElbowArrowRoute = (
|
||||
/**
|
||||
* The route's corner points, without any repeated (or closer than a tiny
|
||||
* fraction of a px, from rounding), or between segments running on along
|
||||
* the same line the same way, and without any step too short to see
|
||||
* (between endpoints nearly, but not exactly, in line): only where the route
|
||||
* carries on the same way after it, so it's never left doubling back
|
||||
* the same line the same way, and without any step too short to see: only
|
||||
* where the route carries on the same way after it, so it's never left
|
||||
* doubling back. The segments either side of the step are put on one line,
|
||||
* moving the corner after it (or, where that's the end, the one before it)
|
||||
* along the segment it turns onto, so the route stays orthogonal, but for
|
||||
* between endpoints nearly, but not exactly, in line, which it runs straight
|
||||
* between.
|
||||
*/
|
||||
const tidyRoute = (points: GlobalPoint[]): GlobalPoint[] => {
|
||||
const same = (a: number, b: number) => Math.abs(a - b) < 1e-6;
|
||||
@@ -491,8 +489,6 @@ const tidyRoute = (points: GlobalPoint[]): GlobalPoint[] => {
|
||||
}
|
||||
|
||||
const isStep = (i: number) =>
|
||||
i >= 1 &&
|
||||
i + 2 < tidy.length &&
|
||||
Math.abs(tidy[i][0] - tidy[i + 1][0]) +
|
||||
Math.abs(tidy[i][1] - tidy[i + 1][1]) <
|
||||
DEDUP_THRESHOLD &&
|
||||
@@ -501,5 +497,28 @@ const tidyRoute = (points: GlobalPoint[]): GlobalPoint[] => {
|
||||
Math.sign(tidy[i][1] - tidy[i - 1][1]) ===
|
||||
Math.sign(tidy[i + 2][1] - tidy[i + 1][1]);
|
||||
|
||||
return tidy.filter((_, i) => !isStep(i) && !isStep(i - 1));
|
||||
for (let i = 1; i + 2 < tidy.length; ) {
|
||||
if (!isStep(i)) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
// (the segments either side of the step run along x where they're
|
||||
// horizontal, the step moving them apart in y)
|
||||
const x =
|
||||
Math.abs(tidy[i][0] - tidy[i - 1][0]) >
|
||||
Math.abs(tidy[i][1] - tidy[i - 1][1]);
|
||||
const onto = (point: GlobalPoint, line: GlobalPoint) =>
|
||||
x
|
||||
? pointFrom<GlobalPoint>(point[0], line[1])
|
||||
: pointFrom<GlobalPoint>(line[0], point[1]);
|
||||
if (i + 3 < tidy.length) {
|
||||
tidy[i + 2] = onto(tidy[i + 2], tidy[i]);
|
||||
} else if (i > 1) {
|
||||
tidy[i - 1] = onto(tidy[i - 1], tidy[i + 1]);
|
||||
}
|
||||
tidy.splice(i, 2);
|
||||
i = Math.max(1, i - 1);
|
||||
}
|
||||
|
||||
return tidy;
|
||||
};
|
||||
@@ -35,6 +35,12 @@ import type { Side } from "./elbowArrowOutline";
|
||||
/** The clearance a route keeps around the elements it connects */
|
||||
export const PADDING = 40;
|
||||
|
||||
/**
|
||||
* The shortest the first and the last segment can be, where there isn't
|
||||
* room for more (an arrowhead on a shorter one is drawn smaller to fit)
|
||||
*/
|
||||
export const MIN_LEG = 10;
|
||||
|
||||
/**
|
||||
* How much faster than an element moves a segment moves out of its way, as
|
||||
* it moves into the segment's way: fast, so the segment is soon clear of it,
|
||||
@@ -90,9 +96,12 @@ export type RouteInput = {
|
||||
end: Point;
|
||||
/** which way the end faces: left, right or up */
|
||||
facing: "left" | "right" | "up";
|
||||
/** the shortest the first and the last segment can be */
|
||||
startMinLeg: number;
|
||||
endMinLeg: number;
|
||||
/**
|
||||
* whether the ends have arrowheads: where there isn't room for both legs,
|
||||
* the one to an arrowhead gets it first
|
||||
*/
|
||||
startArrowhead: boolean;
|
||||
endArrowhead: boolean;
|
||||
/** both ends bound to the same element */
|
||||
sameElement: boolean;
|
||||
/** the corner radius, for how far apart segments turning back are */
|
||||
@@ -143,7 +152,7 @@ const clampClear = (v: number, lo: number, hi: number, radius: number) => {
|
||||
* ahead of it, closer than twice that, only half the room to it, so the
|
||||
* route turns halfway between them; plus how far it is to the side of the
|
||||
* leg's line, or how far behind the leg it starts, so the leg changes
|
||||
* continuously as the elements move. Never shorter than `minLeg`.
|
||||
* continuously as the elements move. Never shorter than `MIN_LEG`.
|
||||
*
|
||||
* By the elements' bounds, rather than their outlines: off the leg's line,
|
||||
* an outline only reaches as far as its corner nearest the line, which for a
|
||||
@@ -151,15 +160,9 @@ const clampClear = (v: number, lo: number, hi: number, radius: number) => {
|
||||
* that's beside it, rather than ahead of it (and change as fast as a slanted
|
||||
* side slides past the line, rather than as fast as the element moves).
|
||||
*/
|
||||
const legLength = (
|
||||
point: Point,
|
||||
heading: Point,
|
||||
own: Box,
|
||||
other: Box,
|
||||
minLeg: number,
|
||||
) => {
|
||||
const legLength = (point: Point, heading: Point, own: Box, other: Box) => {
|
||||
if (!other.solid || other === own) {
|
||||
return Math.max(minLeg, PADDING);
|
||||
return PADDING;
|
||||
}
|
||||
const axis = heading[0] !== 0 ? 0 : 1;
|
||||
const sign = heading[axis];
|
||||
@@ -179,7 +182,7 @@ const legLength = (
|
||||
const beside = Math.max(acrossLo - across, across - acrossHi, 0);
|
||||
|
||||
return Math.max(
|
||||
minLeg,
|
||||
MIN_LEG,
|
||||
Math.min(PADDING, beside + (ahead >= 0 ? ahead / 2 : -ahead)),
|
||||
);
|
||||
};
|
||||
@@ -189,8 +192,8 @@ const legLength = (
|
||||
* other (`overshoot`), twice as fast, up to its full length: so the route
|
||||
* turning back between them widens continuously from nothing, but soon
|
||||
*/
|
||||
const grow = (overshoot: number, leg: number, minLeg: number) =>
|
||||
clamp(2 * overshoot, 0, Math.max(0, leg - minLeg));
|
||||
const grow = (overshoot: number, leg: number) =>
|
||||
clamp(2 * overshoot, 0, Math.max(0, leg - MIN_LEG));
|
||||
|
||||
/**
|
||||
* Whether a detour between `y1` and `y2` goes over the top (to `top`) rather
|
||||
@@ -296,6 +299,23 @@ const overlapping = (a: Box, b: Box) =>
|
||||
Math.max(a.l + a.gap, b.l + b.gap) < Math.min(a.r - a.gap, b.r - b.gap) &&
|
||||
Math.max(a.t + a.gap, b.t + b.gap) < Math.min(a.b - a.gap, b.b - b.gap);
|
||||
|
||||
/**
|
||||
* Where a vertical segment at `at`, from `from` to `to`, runs beside `box`
|
||||
* (over the element's own extent along it) within its padding, or through
|
||||
* it: the span it does over, or null where it doesn't, as where it only
|
||||
* runs by its padding's corner, past the element's end
|
||||
*/
|
||||
const besidePadding = (box: Box, at: number, from: number, to: number) => {
|
||||
if (!box.solid) {
|
||||
return null;
|
||||
}
|
||||
const lo = Math.max(Math.min(from, to), box.t + box.gap);
|
||||
const hi = Math.min(Math.max(from, to), box.b - box.gap);
|
||||
return lo < hi && runsThrough(box.far, [at, lo], [at, hi])
|
||||
? ([lo, hi] as const)
|
||||
: null;
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Routes
|
||||
// -----------------------------------------------------------------------------
|
||||
@@ -309,30 +329,28 @@ const overlapping = (a: Box, b: Box) =>
|
||||
const routeFacing = (input: RouteInput): Route => {
|
||||
const { start: a, endBox: b, end } = input;
|
||||
const [ex, ey] = end;
|
||||
const ms = input.startMinLeg;
|
||||
const me = input.endMinLeg;
|
||||
// (where the turn between them, from the start's line to the end's, is
|
||||
// clear of them)
|
||||
const startExit = a.solid ? Math.max(0, near(a, "r", 0, ey)) : 0;
|
||||
const endExit = b.solid ? Math.min(ex, near(b, "l", 0, ey)) : ex;
|
||||
const legS = legLength([0, 0], [1, 0], a, b, ms);
|
||||
const legE = legLength(end, [-1, 0], b, a, me);
|
||||
const lo = Math.max(ms, startExit);
|
||||
const hi = Math.min(ex - me, endExit);
|
||||
const legS = legLength([0, 0], [1, 0], a, b);
|
||||
const legE = legLength(end, [-1, 0], b, a);
|
||||
const lo = Math.max(MIN_LEG, startExit);
|
||||
const hi = Math.min(ex - MIN_LEG, endExit);
|
||||
const overshoot = lo - hi;
|
||||
|
||||
if (overshoot <= 0) {
|
||||
// (halfway between the elements, past both legs, or, where there isn't
|
||||
// room for both, as far into it as the end with the arrowhead (the
|
||||
// longer shortest leg) leaves, or halfway, with the same at both ends)
|
||||
// room for both, as far into it as the leg to the only arrowhead
|
||||
// leaves, or halfway, with the same at both ends)
|
||||
const legsFrom = Math.max(lo, legS);
|
||||
const legsTo = Math.min(hi, ex - legE);
|
||||
const x = clamp(
|
||||
legsFrom <= legsTo
|
||||
? clamp(mid(startExit, endExit), legsFrom, legsTo)
|
||||
: me > ms
|
||||
: input.endArrowhead && !input.startArrowhead
|
||||
? legsTo
|
||||
: ms > me
|
||||
: input.startArrowhead && !input.endArrowhead
|
||||
? legsFrom
|
||||
: mid(legsFrom, legsTo),
|
||||
lo,
|
||||
@@ -346,15 +364,15 @@ const routeFacing = (input: RouteInput): Route => {
|
||||
|
||||
const down = ey >= 0;
|
||||
const [upper, lower] = down ? [a, b] : [b, a];
|
||||
const squeeze = input.sameElement ? 0 : ms + me + 2 * input.radius;
|
||||
const squeeze = input.sameElement ? 0 : 2 * (MIN_LEG + input.radius);
|
||||
// (only squeezing between elements beside each other, rather than through
|
||||
// where they overlap)
|
||||
const squeezes = overshoot <= squeeze && !overlapping(a, b);
|
||||
// the S, with its legs shortened by `shorten` (from none, to as short as
|
||||
// they can be)
|
||||
const turnBack = (shorten: number) => {
|
||||
const x1 = lo + (1 - shorten) * grow(overshoot, legS, ms);
|
||||
const x2 = hi - (1 - shorten) * grow(overshoot, legE, me);
|
||||
const x1 = lo + (1 - shorten) * grow(overshoot, legS);
|
||||
const x2 = hi - (1 - shorten) * grow(overshoot, legE);
|
||||
// (between where the upper one reaches down to and the lower one up to,
|
||||
// over the span the segment between them runs)
|
||||
const upperEdge = near(upper, "b", x2, x1);
|
||||
@@ -409,22 +427,24 @@ const routeFacing = (input: RouteInput): Route => {
|
||||
// (with legs of their whole length, as it's not turning between them)
|
||||
let ax1 = Math.max(
|
||||
a.solid ? Math.max(0, near(a, "r", s1, s2)) : 0,
|
||||
Math.max(ms, PADDING),
|
||||
PADDING,
|
||||
);
|
||||
// (and around the far side of the other element, where the segment
|
||||
// there would run through it: going over to there gradually would run
|
||||
// through it all the way)
|
||||
const pastEnd = b.solid && runsThrough(b.outline, [ax1, 0], [ax1, out]);
|
||||
// (and around the far side of the other element, with its padding,
|
||||
// where the segment there would run beside it within its padding, or
|
||||
// through it: going over to there gradually would run through it all
|
||||
// the way, and from its padding, the far side of the padding is just
|
||||
// where the segment is as it runs into it, so it doesn't jump there)
|
||||
const pastEnd = besidePadding(b, ax1, s1, s2);
|
||||
if (pastEnd) {
|
||||
ax1 = Math.max(ax1, far(b, "r", s1, s2));
|
||||
ax1 = Math.max(ax1, far(b, "r", ...pastEnd));
|
||||
}
|
||||
let ax2 = Math.min(
|
||||
b.solid ? Math.min(ex, near(b, "l", e1, e2)) : ex,
|
||||
ex - Math.max(me, PADDING),
|
||||
ex - PADDING,
|
||||
);
|
||||
const pastStart = a.solid && runsThrough(a.outline, [ax2, ey], [ax2, out]);
|
||||
const pastStart = besidePadding(a, ax2, e1, e2);
|
||||
if (pastStart) {
|
||||
ax2 = Math.min(ax2, far(a, "l", e1, e2));
|
||||
ax2 = Math.min(ax2, far(a, "l", ...pastStart));
|
||||
}
|
||||
// (and as far out as the elements reach over the span it runs past them)
|
||||
const solid = [a, b].filter((box) => box.solid);
|
||||
@@ -457,13 +477,11 @@ const routeFacing = (input: RouteInput): Route => {
|
||||
const routeParallel = (input: RouteInput): Route => {
|
||||
const { start: a, endBox: b, end } = input;
|
||||
const [ex, ey] = end;
|
||||
const ms = input.startMinLeg;
|
||||
const me = input.endMinLeg;
|
||||
// (where the turn from the start's line to the end's is clear of them)
|
||||
const startExit = a.solid ? Math.max(0, near(a, "r", 0, ey)) : 0;
|
||||
const endExit = b.solid ? Math.max(ex, near(b, "r", 0, ey)) : ex;
|
||||
let xs = Math.max(startExit, legLength([0, 0], [1, 0], a, b, ms));
|
||||
let xe = Math.max(endExit, ex + legLength(end, [1, 0], b, a, me));
|
||||
let xs = Math.max(startExit, legLength([0, 0], [1, 0], a, b));
|
||||
let xe = Math.max(endExit, ex + legLength(end, [1, 0], b, a));
|
||||
let x = Math.max(xs, xe);
|
||||
|
||||
// (the first segment past the start's leg, clear of the end's element,
|
||||
@@ -510,30 +528,28 @@ const routeParallel = (input: RouteInput): Route => {
|
||||
const routeAcross = (input: RouteInput): Route => {
|
||||
const { start: a, endBox: b, end } = input;
|
||||
const [ex, ey] = end;
|
||||
const ms = input.startMinLeg;
|
||||
const me = input.endMinLeg;
|
||||
// (where the L's turn, down from the start's line to the end, is clear of
|
||||
// the start's element, and where the end's leg, up from it, is clear of
|
||||
// the end's, under the start's line along to it)
|
||||
const startExit = a.solid ? Math.max(0, near(a, "r", 0, Math.max(0, ey))) : 0;
|
||||
const endExit = b.solid ? Math.min(ey, near(b, "t", 0, ex)) : ey;
|
||||
const legS = legLength([0, 0], [1, 0], a, b, ms);
|
||||
const legE = legLength(end, [0, -1], b, a, me);
|
||||
const lo = Math.max(ms, startExit);
|
||||
const legS = legLength([0, 0], [1, 0], a, b);
|
||||
const legE = legLength(end, [0, -1], b, a);
|
||||
const lo = Math.max(MIN_LEG, startExit);
|
||||
// how far the end is behind where the start's leg can turn, and how far
|
||||
// above where the end's leg can start
|
||||
const behind = lo - ex;
|
||||
const above = Math.max(me - ey, -endExit);
|
||||
const above = Math.max(MIN_LEG - ey, -endExit);
|
||||
|
||||
if (above > 0) {
|
||||
// (clear of the start's element above its line, where it climbs)
|
||||
const upExit = a.solid ? Math.max(0, near(a, "r", -Infinity, 0)) : 0;
|
||||
const upLo = Math.max(ms, upExit);
|
||||
const upLo = Math.max(MIN_LEG, upExit);
|
||||
const upBehind = upLo - ex;
|
||||
const endLeft = b.solid ? near(b, "l", -Infinity, 0) : ex;
|
||||
let x1 =
|
||||
upBehind > 0
|
||||
? upLo + grow(upBehind, legS, ms)
|
||||
? upLo + grow(upBehind, legS)
|
||||
: clamp(mid(upExit, endLeft), upLo, ex);
|
||||
// (climbing a leg's length out, or as far as halfway to the end's
|
||||
// element ahead of it, past the end, the further the higher it climbs,
|
||||
@@ -560,7 +576,7 @@ const routeAcross = (input: RouteInput): Route => {
|
||||
past > ex - input.radius ? Math.max(past, leg, ex + input.radius) : past;
|
||||
// (above the end's element, where it reaches over the segment's span)
|
||||
const climb = Math.max(above, b.solid ? -near(b, "t", ex, x1) : -Infinity);
|
||||
let y = -climb - grow(climb, legE, me);
|
||||
let y = -climb - grow(climb, legE);
|
||||
// (over the start's element, where the segment there runs back over
|
||||
// it, as far as it does)
|
||||
y += dodgeUp(a, y, ex, x1);
|
||||
@@ -595,31 +611,22 @@ const routeAcross = (input: RouteInput): Route => {
|
||||
}
|
||||
|
||||
if (behind > 0) {
|
||||
const x1 = lo + grow(behind, legS, ms);
|
||||
const x1 = lo + grow(behind, legS);
|
||||
// (between where the start's element reaches down to and the end's up
|
||||
// to, over the span the segment between them runs)
|
||||
const startEdge = near(a, "b", ex, x1);
|
||||
const endEdge = near(b, "t", ex, x1);
|
||||
const endTop = b.solid ? Math.min(ey, endEdge) : ey;
|
||||
const squeeze = input.sameElement ? 0 : ms + me + 2 * input.radius;
|
||||
// (halfway between the elements, clear of the start's leg, short of the
|
||||
// end's)
|
||||
const hi = Math.max(0, endTop - me);
|
||||
let y = clamp(mid(startEdge, endEdge), Math.min(input.radius, hi / 2), hi);
|
||||
// (but where the segment there runs back under the start's element, as
|
||||
// far as it does, no closer to it than to the end, where there isn't
|
||||
// room for the end's leg as well, at the expense of the end's leg: so it
|
||||
// stays between them, rather than running along the element)
|
||||
const reachAlong = a.solid
|
||||
? Math.min(x1, a.r - a.gap) - Math.max(ex, a.l + a.gap)
|
||||
: 0;
|
||||
y += Math.max(
|
||||
0,
|
||||
Math.min(
|
||||
mid(reach(a.outline, "b", ex, x1), endTop) - y,
|
||||
DODGE_RATE * reachAlong,
|
||||
endTop - input.radius / 2 - y,
|
||||
),
|
||||
const squeeze = input.sameElement ? 0 : 2 * (MIN_LEG + input.radius);
|
||||
// (halfway between the elements, clear of the start's leg: where there
|
||||
// isn't room for the end's whole leg as well, at the expense of the
|
||||
// end's leg, down to its shortest, so it stays between them rather than
|
||||
// running along the start's element)
|
||||
const hi = Math.max(0, endTop - MIN_LEG);
|
||||
const y = clamp(
|
||||
mid(startEdge, endEdge),
|
||||
Math.min(input.radius, hi / 2),
|
||||
hi,
|
||||
);
|
||||
const between: Point[] = [[0, 0], [x1, 0], [x1, y], [ex, y], end];
|
||||
const apart = endEdge - startEdge >= -(a.gap + b.gap);
|
||||
|
||||
@@ -26,6 +26,7 @@ import type { LocalPoint } from "@excalidraw/math";
|
||||
import { getBindingGap } from "../src/binding";
|
||||
import { Scene } from "../src/Scene";
|
||||
import { ELBOW_ARROW_CORNER_RADIUS } from "../src/shape";
|
||||
import { MIN_LEG, PADDING } from "../src/elbowArrowTemplates";
|
||||
|
||||
import type {
|
||||
ExcalidrawArrowElement,
|
||||
@@ -539,14 +540,24 @@ describe("elbow arrow routes with one or two corners", () => {
|
||||
expect(arrow.points.length).toBeGreaterThan(3);
|
||||
});
|
||||
|
||||
it("doesn't put the arrowhead on a leg too short for it", () => {
|
||||
it("connects elements with a single corner on a short leg, arrowhead and all", () => {
|
||||
// 25px from the rectangle's side to the diamond's top
|
||||
const arrow = connect(152.5);
|
||||
|
||||
expect(arrow.points).toCloselyEqualPoints([
|
||||
[0, 0],
|
||||
[195, 0],
|
||||
[195, 19.15],
|
||||
]);
|
||||
});
|
||||
|
||||
it("doesn't route a leg shorter than the shortest a leg can be", () => {
|
||||
// about 9px from the rectangle's side to the diamond's top
|
||||
const arrow = connect(142.5);
|
||||
expect(arrow.points.length).toBeGreaterThan(3);
|
||||
});
|
||||
|
||||
it("routes again to fit an arrowhead added to a short leg", () => {
|
||||
it("keeps a short leg the same with an arrowhead added to it", () => {
|
||||
API.setAppState({ currentItemEndArrowhead: null });
|
||||
// about 10px from the rectangle's side to the diamond's top
|
||||
const arrow = connect(143.5);
|
||||
@@ -566,9 +577,12 @@ describe("elbow arrow routes with one or two corners", () => {
|
||||
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);
|
||||
expect(updated.points).toCloselyEqualPoints([
|
||||
[0, 0],
|
||||
[195, 0],
|
||||
[195, 10.15],
|
||||
]);
|
||||
});
|
||||
|
||||
it("doesn't route a leg too short to keep its corner", () => {
|
||||
@@ -635,28 +649,28 @@ describe("elbow arrow routes with one or two corners", () => {
|
||||
mouse.reset();
|
||||
mouse.moveTo(0, 120);
|
||||
mouse.down();
|
||||
mouse.moveTo(0, 140);
|
||||
mouse.moveTo(0, 150);
|
||||
mouse.up();
|
||||
h.app.scene.mutateElement(arrow, {
|
||||
fixedSegments: [
|
||||
{
|
||||
index: 3,
|
||||
start: pointFrom<LocalPoint>(44, -20),
|
||||
end: pointFrom<LocalPoint>(195, -20),
|
||||
start: pointFrom<LocalPoint>(44, -30),
|
||||
end: pointFrom<LocalPoint>(195, -30),
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(arrow.points).toCloselyEqualPoints([
|
||||
[0, 0],
|
||||
[44, 0],
|
||||
[44, -20],
|
||||
[195, -20],
|
||||
[195, 14.15],
|
||||
[44, -30],
|
||||
[195, -30],
|
||||
[195, 4.15],
|
||||
]);
|
||||
|
||||
// and back up, the first leg lines up with the fixed segment
|
||||
mouse.reset();
|
||||
mouse.moveTo(0, 140);
|
||||
mouse.moveTo(0, 150);
|
||||
mouse.down();
|
||||
mouse.moveTo(0, 120);
|
||||
mouse.up();
|
||||
@@ -728,9 +742,19 @@ describe("elbow arrow routes with one or two corners", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("doesn't put the arrowhead on a half too short for it", () => {
|
||||
it("turns between them on legs as short as they can be, the arrowhead's getting the rest", () => {
|
||||
const arrow = connectBelow(40);
|
||||
|
||||
expect(arrow.points).toCloselyEqualPoints([
|
||||
[0, 0],
|
||||
[0, 10],
|
||||
[518, 10],
|
||||
[518, 28],
|
||||
]);
|
||||
});
|
||||
|
||||
it("doesn't turn between them where there isn't room for both legs", () => {
|
||||
const arrow = connectBelow(30);
|
||||
expect(arrow.points.length).toBeGreaterThan(4);
|
||||
});
|
||||
});
|
||||
@@ -1082,13 +1106,13 @@ describe("elbow arrow routing between overlapping elements", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
it("doesn't go around elements as one where only their bounds overlap", () => {
|
||||
// diamonds tip to tip, their bounds overlapping, but not the diamonds,
|
||||
// with the start on the top of the lower one, a little right of the end
|
||||
// on the right of the upper one
|
||||
// diamonds tip to tip, their bounds overlapping, but not the diamonds,
|
||||
// with the start on the top of the lower one, and the end on the right of
|
||||
// the upper one
|
||||
const routeTipToTip = (lowerX: number) => {
|
||||
const lower = API.createElement({
|
||||
type: "diamond",
|
||||
x: 2290.31,
|
||||
x: lowerX,
|
||||
y: -706.24,
|
||||
width: 226.95,
|
||||
height: 248.6,
|
||||
@@ -1119,22 +1143,39 @@ describe("elbow arrow routing between overlapping elements", () => {
|
||||
endArrowhead: "arrow",
|
||||
}) as unknown as ExcalidrawElbowArrowElement;
|
||||
|
||||
const { points } = updateElbowArrowRoute(
|
||||
return updateElbowArrowRoute(
|
||||
arrow,
|
||||
arrayToMap([lower, upper]) as NonDeletedSceneElementsMap,
|
||||
);
|
||||
).points!;
|
||||
};
|
||||
|
||||
it("doesn't go around elements as one where only their bounds overlap", () => {
|
||||
// the start 20px right of the end: up, and left into it
|
||||
const points = routeTipToTip(2290.31);
|
||||
|
||||
const start = points[0];
|
||||
const end = points[points.length - 1];
|
||||
expect(end[0]).toBeCloseTo(start[0] - 20.15);
|
||||
expect(points).toCloselyEqualPoints([start, [start[0], end[1]], end]);
|
||||
});
|
||||
|
||||
it("turns back about halfway where only their bounds overlap, the end right by the start's line", () => {
|
||||
// the start just right of the end, too close to it for the end's leg
|
||||
const points = routeTipToTip(2270.31);
|
||||
|
||||
// up, turning back about halfway between the endpoints (halfway between
|
||||
// the elements), rather than a U turn squeezed in right next to the end
|
||||
const start = points![0];
|
||||
const end = points![points!.length - 1];
|
||||
const turn = points![1][1];
|
||||
const start = points[0];
|
||||
const end = points[points.length - 1];
|
||||
const turn = points[1][1];
|
||||
const back = points[2][0];
|
||||
expect(turn).toBeCloseTo((start[1] + end[1]) / 2, -1);
|
||||
expect(back - end[0]).toBeGreaterThan(MIN_LEG);
|
||||
expect(points).toCloselyEqualPoints([
|
||||
start,
|
||||
[start[0], turn],
|
||||
[end[0] + 40, turn],
|
||||
[end[0] + 40, end[1]],
|
||||
[back, turn],
|
||||
[back, end[1]],
|
||||
end,
|
||||
]);
|
||||
});
|
||||
@@ -1296,16 +1337,30 @@ describe("elbow arrow routing between overlapping elements", () => {
|
||||
arrayToMap([lower, upper]) as NonDeletedSceneElementsMap,
|
||||
);
|
||||
|
||||
// up a padding, right a padding past the end, down and left into it:
|
||||
// a short loop, rather than one around the bounds of both
|
||||
// up a hairpin's width past the end, right past the end (the end's leg
|
||||
// short of the padding, ramping in from its shortest, as the end is
|
||||
// just by the start's line), down and left into it: a short loop,
|
||||
// rather than one around the bounds of both
|
||||
expect(points).toHaveLength(5);
|
||||
expect(points![0][1] - points![1][1]).toBeCloseTo(40, 0);
|
||||
expect(points![2][0] - points![4][0]).toBeCloseTo(40, 0);
|
||||
expect(points![3][1] - points![2][1]).toBeCloseTo(
|
||||
ELBOW_ARROW_CORNER_RADIUS,
|
||||
0,
|
||||
);
|
||||
expect(points![2][0] - points![4][0]).toBeCloseTo(28.3, 0);
|
||||
});
|
||||
|
||||
it.each([2160, 2180, 2189.7, 2200, 2220, 2240])(
|
||||
it.each([
|
||||
[2160, 40, 40],
|
||||
[2180, 40, 40],
|
||||
[2189.7, 40, 40],
|
||||
[2200, 40, 40],
|
||||
[2220, 40, 40],
|
||||
// (the end just left of the start's line, its leg ramping in from its
|
||||
// shortest, and the start's a hairpin's width past it)
|
||||
[2240, 35, 20.8],
|
||||
])(
|
||||
"loops on out of the other one's bounds, rather than around them, the start deep in them (the lower one at x=%d)",
|
||||
(lowerX) => {
|
||||
(lowerX, startLeg, endLeg) => {
|
||||
// as above, the start on top of the lower one inside the upper one's
|
||||
// bounds, the deeper the further left: the route has to get out of
|
||||
// them anyway, and gets out the nearer way, rather than through them
|
||||
@@ -1350,11 +1405,95 @@ describe("elbow arrow routing between overlapping elements", () => {
|
||||
|
||||
// up a padding, right a padding past the end, down and left into it
|
||||
expect(points).toHaveLength(5);
|
||||
expect(points![0][1] - points![1][1]).toBeCloseTo(40, 0);
|
||||
expect(points![2][0] - points![4][0]).toBeCloseTo(40, 0);
|
||||
expect(points![0][1] - points![1][1]).toBeCloseTo(startLeg, 0);
|
||||
expect(points![2][0] - points![4][0]).toBeCloseTo(endLeg, 0);
|
||||
},
|
||||
);
|
||||
|
||||
it("goes around both by the padding past the other one, rather than along its side, as either is dragged sideways", () => {
|
||||
// rectangles the same size, nearly on top of each other, from the right
|
||||
// of the one behind to the left of the one in front, which reaches out
|
||||
// past it by just short of the padding (and the binding gap): the route
|
||||
// goes around under both
|
||||
const routeWith = (startX: number, endX: number) => {
|
||||
const start = API.createElement({
|
||||
type: "rectangle",
|
||||
x: startX,
|
||||
y: 440.49,
|
||||
width: 224.54,
|
||||
height: 112,
|
||||
});
|
||||
const end = API.createElement({
|
||||
type: "rectangle",
|
||||
x: endX,
|
||||
y: 447.76,
|
||||
width: 224.54,
|
||||
height: 112,
|
||||
});
|
||||
const arrow = API.createElement({
|
||||
type: "arrow",
|
||||
elbowed: true,
|
||||
x: 0,
|
||||
y: 0,
|
||||
points: [pointFrom<LocalPoint>(0, 0), pointFrom<LocalPoint>(1, 1)],
|
||||
startBinding: {
|
||||
elementId: start.id,
|
||||
fixedPoint: [1.0267, 0.5001],
|
||||
mode: "orbit",
|
||||
},
|
||||
endBinding: {
|
||||
elementId: end.id,
|
||||
fixedPoint: [-0.0267, 0.5001],
|
||||
mode: "orbit",
|
||||
},
|
||||
endArrowhead: "arrow",
|
||||
}) as unknown as ExcalidrawElbowArrowElement;
|
||||
const update = updateElbowArrowRoute(
|
||||
arrow,
|
||||
arrayToMap([start, end]) as NonDeletedSceneElementsMap,
|
||||
);
|
||||
return {
|
||||
start,
|
||||
end,
|
||||
points: update.points!.map(
|
||||
([px, py]) => [update.x! + px, update.y! + py] as [number, number],
|
||||
),
|
||||
};
|
||||
};
|
||||
|
||||
// past the other one's far side by the padding: down past the right of
|
||||
// the one in front, and up past the left of the one behind
|
||||
const { start, end, points } = routeWith(985.19, 1030.78);
|
||||
expect(points).toHaveLength(6);
|
||||
expect(points[1][0]).toBeCloseTo(
|
||||
end.x + end.width + getBindingGap(end) + PADDING,
|
||||
);
|
||||
expect(points[3][0]).toBeCloseTo(start.x - getBindingGap(start) - PADDING);
|
||||
|
||||
// and either dragged sideways, in half px steps, the route only ever
|
||||
// moving along with it, never jumping
|
||||
for (const drag of [
|
||||
(dx: number) => routeWith(985.19 + dx, 1030.78),
|
||||
(dx: number) => routeWith(985.19, 1030.78 + dx),
|
||||
]) {
|
||||
const routes = Array.from(
|
||||
{ length: 121 },
|
||||
(_, step) => drag((step - 60) / 2).points,
|
||||
);
|
||||
routes.forEach((route, step) => {
|
||||
expect(route).toHaveLength(6);
|
||||
if (step > 0) {
|
||||
route.forEach(([px, py], i) => {
|
||||
const [qx, qy] = routes[step - 1][i];
|
||||
expect(Math.abs(px - qx) + Math.abs(py - qy)).toBeLessThanOrEqual(
|
||||
0.51,
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it("loops back a hairpin's width where the end is right by where the start's leg ends", () => {
|
||||
// the start on top of the lower one, just right of the upper one, which
|
||||
// overlaps the lower one's top left, and the end on the right of the
|
||||
@@ -1400,15 +1539,16 @@ describe("elbow arrow routing between overlapping elements", () => {
|
||||
arrayToMap([lower, upper]) as NonDeletedSceneElementsMap,
|
||||
);
|
||||
|
||||
// up past where the end is by a hairpin's width, right a padding past
|
||||
// the end, and down and left into it: a short loop, rather than one
|
||||
// around the bounds of both
|
||||
// up past where the end is by a hairpin's width, right past the end
|
||||
// (its leg short of the padding, ramping in from its shortest, as the
|
||||
// end is just right of the start's line), and down and left into it: a
|
||||
// short loop, rather than one around the bounds of both
|
||||
expect(points).toHaveLength(5);
|
||||
expect(points![3][1] - points![2][1]).toBeCloseTo(
|
||||
ELBOW_ARROW_CORNER_RADIUS,
|
||||
0,
|
||||
);
|
||||
expect(points![2][0] - points![4][0]).toBeCloseTo(40, 0);
|
||||
expect(points![2][0] - points![4][0]).toBeCloseTo(36, 0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1590,13 +1730,16 @@ describe("elbow arrow legs next to another element", () => {
|
||||
});
|
||||
|
||||
it.each([
|
||||
[604.32, 517.29],
|
||||
[604.32, 520],
|
||||
[604.32, 525],
|
||||
[608.24, 520],
|
||||
// (the endpoints 7px apart, closer than both legs at their shortest: the
|
||||
// legs ramp in from their shortest as the endpoints close in, and only
|
||||
// get their whole padding a few px further on)
|
||||
[604.32, 517.29, 35.28],
|
||||
[604.32, 520, 35.28],
|
||||
[604.32, 525, 35.28],
|
||||
[608.24, 520, 40],
|
||||
])(
|
||||
"gives both legs their padding where the elements' corners nearly touch (the upper one at %d, %d)",
|
||||
(upperX, upperY) => {
|
||||
(upperX, upperY, leg) => {
|
||||
// the upper element's bottom right corner is just above and left of the
|
||||
// lower one's top left corner (their binding gaps overlapping, and even
|
||||
// their sides a little), and the route turns back between them
|
||||
@@ -1639,8 +1782,8 @@ describe("elbow arrow legs next to another element", () => {
|
||||
);
|
||||
|
||||
expect(points).toHaveLength(6);
|
||||
expect(points![1][0] - points![0][0]).toBeCloseTo(40);
|
||||
expect(points![5][0] - points![4][0]).toBeCloseTo(40);
|
||||
expect(points![1][0] - points![0][0]).toBeCloseTo(leg);
|
||||
expect(points![5][0] - points![4][0]).toBeCloseTo(leg);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -1928,8 +2071,9 @@ describe("elbow arrow loops", () => {
|
||||
arrayToMap([element]) as NonDeletedSceneElementsMap,
|
||||
);
|
||||
|
||||
// a padding (or so) across, turning the same way all around, back to
|
||||
// where it started
|
||||
// three times the shortest leg (or so) across, as far as the legs
|
||||
// ramp in from their shortest, turning the same way all around, back
|
||||
// to where it started
|
||||
expect(points).toHaveLength(5);
|
||||
expect(points![4][0]).toBeCloseTo(points![0][0]);
|
||||
expect(points![4][1]).toBeCloseTo(points![0][1]);
|
||||
@@ -1937,8 +2081,10 @@ describe("elbow arrow loops", () => {
|
||||
.slice(1)
|
||||
.map(([x, y], i) => [x - points![i][0], y - points![i][1]]);
|
||||
segments.forEach(([dx, dy], i) => {
|
||||
expect(Math.abs(dx) + Math.abs(dy)).toBeGreaterThanOrEqual(39.5);
|
||||
expect(Math.abs(dx) + Math.abs(dy)).toBeLessThan(50);
|
||||
expect(Math.abs(dx) + Math.abs(dy)).toBeGreaterThanOrEqual(
|
||||
3 * MIN_LEG - 0.5,
|
||||
);
|
||||
expect(Math.abs(dx) + Math.abs(dy)).toBeLessThan(40);
|
||||
const [nx, ny] = segments[(i + 1) % 4];
|
||||
expect(dx * ny - dy * nx).toBeGreaterThan(0);
|
||||
});
|
||||
@@ -2276,10 +2422,11 @@ describe("elbow arrow routing between elements facing each other", () => {
|
||||
),
|
||||
);
|
||||
expect(jumps).toEqual([]);
|
||||
// halfway between them, as long as that leaves the arrowhead room
|
||||
// halfway between them, as long as that leaves the end's leg its
|
||||
// shortest
|
||||
const [start, , , end] = routes[0];
|
||||
expect(routes[0][1][0]).toBeCloseTo(
|
||||
Math.min((start[0] + end[0]) / 2, end[0] - 29),
|
||||
Math.min((start[0] + end[0]) / 2, end[0] - MIN_LEG),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -2482,7 +2629,7 @@ describe("elbow arrow routing back under the start's element", () => {
|
||||
};
|
||||
|
||||
// dragged up, in 1px steps, from 150px below the start's element until
|
||||
// it's 25px below it (closer, the end's leg keeps the length to turn)
|
||||
// it's 25px below it (closer, the end's leg keeps its shortest)
|
||||
const routes = Array.from({ length: 126 }, (_, step) =>
|
||||
routeWithEndAt(startBottom + 150 - step),
|
||||
);
|
||||
@@ -2490,9 +2637,12 @@ describe("elbow arrow routing back under the start's element", () => {
|
||||
expect(points).toHaveLength(5);
|
||||
const [, , [, turn], , [, endY]] = points;
|
||||
// under the start's element, at least as far from it as from the end,
|
||||
// where there isn't room for both the end's leg and the padding
|
||||
expect(turn - startBottom).toBeGreaterThanOrEqual(endY - turn - 0.01);
|
||||
expect(endY - turn).toBeGreaterThan(0);
|
||||
// where there isn't room for both the end's leg and the padding (but
|
||||
// for the end's leg's shortest)
|
||||
expect(endY - turn).toBeGreaterThanOrEqual(MIN_LEG - 0.01);
|
||||
if (endY - turn > MIN_LEG + 0.01) {
|
||||
expect(turn - startBottom).toBeGreaterThanOrEqual(endY - turn - 0.01);
|
||||
}
|
||||
if (step > 0) {
|
||||
points.forEach(([px, py], i) => {
|
||||
const [qx, qy] = routes[step - 1][i];
|
||||
@@ -2505,6 +2655,6 @@ describe("elbow arrow routing back under the start's element", () => {
|
||||
// (halfway between their outlines, where the gap between them is as in
|
||||
// the scene this was reported in)
|
||||
const [, , [, turn]] = routeWithEndAt(startBottom + 45.67);
|
||||
expect(turn).toBeCloseTo(startBottom + 20, 0);
|
||||
expect(turn).toBeCloseTo(startBottom + 45.67 / 2, 0);
|
||||
});
|
||||
});
|
||||
@@ -18,7 +18,12 @@ import { API } from "@excalidraw/excalidraw/tests/helpers/api";
|
||||
|
||||
import type { LocalPoint } from "@excalidraw/math";
|
||||
|
||||
import { getElbowArrowData, routeElbowArrow } from "../src/elbowArrowRouting";
|
||||
import {
|
||||
DEDUP_THRESHOLD,
|
||||
getElbowArrowData,
|
||||
routeElbowArrow,
|
||||
} from "../src/elbowArrowRouting";
|
||||
import { MIN_LEG } from "../src/elbowArrowTemplates";
|
||||
|
||||
import type {
|
||||
ExcalidrawElbowArrowElement,
|
||||
@@ -203,8 +208,9 @@ const sweep = (shape: keyof typeof SHAPES) => {
|
||||
};
|
||||
|
||||
// How a route looks: segments not orthogonal, legs not along the headings
|
||||
// (or too short for the arrowhead), crossing itself, and how far it runs
|
||||
// inside either element, besides its legs
|
||||
// (or shorter than they can be, but for a step too short to see taken out
|
||||
// next to them), crossing itself, and how far it runs inside either element,
|
||||
// besides its legs
|
||||
const quality = (r: ReturnType<typeof route>) => {
|
||||
const { points, boxes, headings } = r;
|
||||
const issues: string[] = [];
|
||||
@@ -231,7 +237,11 @@ const quality = (r: ReturnType<typeof route>) => {
|
||||
}
|
||||
}
|
||||
const len = (a: Pt, b: Pt) => Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]);
|
||||
if (points.length > 2 && len(e1, e0) < 20) {
|
||||
const shortest = MIN_LEG - DEDUP_THRESHOLD;
|
||||
if (points.length > 2 && len(s0, s1) < shortest) {
|
||||
issues.push("short start leg");
|
||||
}
|
||||
if (points.length > 2 && len(e1, e0) < shortest) {
|
||||
issues.push("short end leg");
|
||||
}
|
||||
// crossing itself
|
||||
|
||||
Reference in new issue
Block a user