From fb4f616d31331bb4db8fef27b6d199c3563166bb Mon Sep 17 00:00:00 2001 From: Mark Tolmacs Date: Fri, 23 Oct 2026 09:00:36 +0000 Subject: [PATCH] fix(element): don't grow arrow height to fit rewrapped labels Review finding 1. The skeleton placement pass in convertToExcalidrawElements() calls updateBoundElements(), which calls handleBindTextResize() for arrows with labels. When the label rewraps taller than getBoundTextMaxHeight() allows (for arrows under 80px tall, that is the label's old height), it set the arrow's `height` from the text, leaving it inconsistent with its points. Reproduction steps: convertToExcalidrawElements() with rectangles A (0,0,100x20) and B (500,0,100x20), an arrow at (100,10) with points [[0,0],[400,0]] bound from A to B with label "aaaa bbbb cccc dddd eeee fff", and a frame containing all three. Placement shortens the arrow, so the label wraps to 2 lines (height 25 -> 50). Failure case: observed arrow height 130 (50 + 80 padding) with points [[0,0],[388,0]]; expected 0, as the points are horizontal. The stale height persists through updateScene() and inflates later getBoundTextMaxHeight() results. The same happens in the editor: dragging the end point of a horizontal labelled arrow (linearElementEditor.test.tsx) left it at height 130. Fix: handleBindTextResize() no longer grows arrow containers to fit their label, matching redrawTextBoundingBox(), which already exempts arrows: an arrow's size derives from its points, and its label is positioned along them rather than within its height. The label itself still rewraps. The linearElementEditor inline snapshot that recorded the bogus height of 130 for a horizontal arrow is updated to 0. --- .../element/src/__tests__/transform.test.ts | 30 +++++++++++++++++++ packages/element/src/textElement.ts | 24 +++++++-------- .../tests/linearElementEditor.test.tsx | 2 +- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/packages/element/src/__tests__/transform.test.ts b/packages/element/src/__tests__/transform.test.ts index dd331a65ac..f1597ca69e 100644 --- a/packages/element/src/__tests__/transform.test.ts +++ b/packages/element/src/__tests__/transform.test.ts @@ -1294,6 +1294,36 @@ describe("Test Transform", () => { expect(frame.y + frame.height).toBeGreaterThanOrEqual(maxY); }); + it("should keep bound arrows sized by their points when labels rewrap", () => { + const elements = convertToExcalidrawElements( + [ + { type: "rectangle", id: "A", x: 0, y: 0, width: 100, height: 20 }, + { type: "rectangle", id: "B", x: 500, y: 0, width: 100, height: 20 }, + { + type: "arrow", + x: 100, + y: 10, + points: [pointFrom(0, 0), pointFrom(400, 0)], + start: { id: "A" }, + end: { id: "B" }, + // wraps to a taller label along the arrow shortened by its binding + label: { text: "aaaa bbbb cccc dddd eeee fff" }, + }, + ], + opts, + ); + const arrow = elements.find( + (element) => element.type === "arrow", + ) as ExcalidrawArrowElement; + const label = elements.find( + (element) => element.type === "text", + ) as ExcalidrawTextElement; + + expect(label.text).toContain("\n"); + expect(arrow.points.every(([, y]) => y === 0)).toBe(true); + expect(arrow.height).toBe(0); + }); + it("should place bound arrows listed by elements they aren't bound to", () => { const convert = (stale: boolean) => convertToExcalidrawElements( diff --git a/packages/element/src/textElement.ts b/packages/element/src/textElement.ts index 843b5d0f3a..297a716a17 100644 --- a/packages/element/src/textElement.ts +++ b/packages/element/src/textElement.ts @@ -218,8 +218,9 @@ export const handleBindTextResize = ( nextHeight = metrics.height; nextWidth = metrics.width; } - // increase height in case text element height exceeds - if (nextHeight > maxHeight) { + // increase height in case text element height exceeds (but not an + // arrow's, which its points determine, and its label doesn't grow) + if (!isArrowElement(container) && nextHeight > maxHeight) { containerHeight = computeContainerDimensionForBoundText( nextHeight, container.type, @@ -233,16 +234,15 @@ export const handleBindTextResize = ( scene.mutateElement(container, { height: containerHeight, - ...(!isArrowElement(container) && - getPositionAfterHeightChange( - container, - containerHeight, - shouldResizeFromCenter - ? "center" - : shouldResizeFromTop - ? "bottom" - : "top", - )), + ...getPositionAfterHeightChange( + container, + containerHeight, + shouldResizeFromCenter + ? "center" + : shouldResizeFromTop + ? "bottom" + : "top", + ), }); } diff --git a/packages/element/tests/linearElementEditor.test.tsx b/packages/element/tests/linearElementEditor.test.tsx index acbf649d42..144dff679d 100644 --- a/packages/element/tests/linearElementEditor.test.tsx +++ b/packages/element/tests/linearElementEditor.test.tsx @@ -1517,7 +1517,7 @@ describe("Test Linear Elements", () => { expect({ width: container.width, height: container.height }) .toMatchInlineSnapshot(` { - "height": 130, + "height": 0, "width": 340, } `);