From 7cc6d221ba494ea9e48b60b82085eec816256c07 Mon Sep 17 00:00:00 2001 From: Mark Tolmacs Date: Wed, 25 Nov 2026 14:42:03 +0000 Subject: [PATCH] fix: Centering --- packages/element/src/elbowArrowTemplates.ts | 18 ++--- packages/element/tests/elbowArrow.test.tsx | 75 +++++++++++++++++++++ 2 files changed, 85 insertions(+), 8 deletions(-) diff --git a/packages/element/src/elbowArrowTemplates.ts b/packages/element/src/elbowArrowTemplates.ts index 5fc8143ea5..a97deb41b6 100644 --- a/packages/element/src/elbowArrowTemplates.ts +++ b/packages/element/src/elbowArrowTemplates.ts @@ -496,22 +496,24 @@ const routeAcross = (input: RouteInput): Route => { const gap = b.t - a.b; 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, but past the start's element (only just: its binding gap is - // room enough) where the segment there runs back over it, as far as it - // does, at the expense of the end's leg, as far as it has to) - let y = clampClear( + // end's) + const hi = Math.max(0, endExit - me); + let y = clamp( mid(outlineEdge(a, false, ex, x1), outlineEdge(b, true, ex, x1)), - 0, - Math.max(0, endExit - me), - input.radius, + 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 reach = a.solid ? Math.min(x1, a.r - a.gap) - Math.max(ex, a.l + a.gap) : 0; y += Math.max( 0, Math.min( - a.b - a.gap - y, + mid(outlineEdge(a, false, ex, x1) - a.gap, endExit) - y, DODGE_RATE * reach, endExit - input.radius / 2 - y, ), diff --git a/packages/element/tests/elbowArrow.test.tsx b/packages/element/tests/elbowArrow.test.tsx index ca08e53737..6741505ceb 100644 --- a/packages/element/tests/elbowArrow.test.tsx +++ b/packages/element/tests/elbowArrow.test.tsx @@ -2427,3 +2427,78 @@ describe("elbow arrow legs as the elements get closer", () => { }, ); }); + +describe("elbow arrow routing back under the start's element", () => { + it("turns between the elements as long as there's room, rather than along the start's element", () => { + // from the right of one element to the top of another just below it, + // the end behind the start: the route turns back between them + const start = API.createElement({ + type: "rectangle", + x: 2226.84, + y: 371.39, + width: 224.54, + height: 112, + }); + const startBottom = start.y + start.height; + const routeWithEndAt = (y: number) => { + const end = API.createElement({ + type: "rectangle", + x: 2234.41, + y, + width: 224.54, + height: 112, + }); + const arrow = API.createElement({ + type: "arrow", + elbowed: true, + x: 0, + y: 0, + points: [pointFrom(0, 0), pointFrom(1, 1)], + startBinding: { + elementId: start.id, + fixedPoint: [1.0267, 0.5001], + mode: "orbit", + }, + endBinding: { + elementId: end.id, + fixedPoint: [0.5001, -0.0536], + mode: "orbit", + }, + endArrowhead: "arrow", + }) as unknown as ExcalidrawElbowArrowElement; + const update = updateElbowArrowRoute( + arrow, + arrayToMap([start, end]) as NonDeletedSceneElementsMap, + ); + return update.points!.map( + ([px, py]) => [update.x! + px, update.y! + py] as [number, number], + ); + }; + + // 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) + const routes = Array.from({ length: 126 }, (_, step) => + routeWithEndAt(startBottom + 150 - step), + ); + routes.forEach((points, step) => { + 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); + if (step > 0) { + points.forEach(([px, py], i) => { + const [qx, qy] = routes[step - 1][i]; + expect(Math.abs(px - qx) + Math.abs(py - qy)).toBeLessThanOrEqual( + 2.5, + ); + }); + } + }); + // (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); + }); +});