diff --git a/packages/element/src/__tests__/__snapshots__/transform.test.ts.snap b/packages/element/src/__tests__/__snapshots__/transform.test.ts.snap index a160613d3b..b6717dac9b 100644 --- a/packages/element/src/__tests__/__snapshots__/transform.test.ts.snap +++ b/packages/element/src/__tests__/__snapshots__/transform.test.ts.snap @@ -442,7 +442,7 @@ exports[`Test Transform > Test arrow bindings > should bind arrows to existing t "textAlign": "center", "type": "text", "updated": 1, - "version": 4, + "version": 3, "versionNonce": Any, "verticalAlign": "middle", "width": 130, @@ -553,7 +553,7 @@ exports[`Test Transform > Test arrow bindings > should bind arrows to shapes whe "textAlign": "center", "type": "text", "updated": 1, - "version": 4, + "version": 3, "versionNonce": Any, "verticalAlign": "middle", "width": 130, @@ -740,7 +740,7 @@ exports[`Test Transform > Test arrow bindings > should bind arrows to text when "textAlign": "center", "type": "text", "updated": 1, - "version": 4, + "version": 3, "versionNonce": Any, "verticalAlign": "middle", "width": 130, @@ -1887,7 +1887,7 @@ exports[`Test Transform > should transform the elements correctly when linear el "textAlign": "center", "type": "text", "updated": 1, - "version": 5, + "version": 4, "versionNonce": Any, "verticalAlign": "middle", "width": 120, diff --git a/packages/element/src/__tests__/transform.test.ts b/packages/element/src/__tests__/transform.test.ts index 825bd69abc..9281cace0c 100644 --- a/packages/element/src/__tests__/transform.test.ts +++ b/packages/element/src/__tests__/transform.test.ts @@ -21,6 +21,8 @@ import { import { getArrowheadSize, getCommonBounds } from "../bounds"; import { LinearElementEditor } from "../linearElementEditor"; import { getBoundTextElement } from "../textElement"; +import * as textMeasurements from "../textMeasurements"; +import * as textWrapping from "../textWrapping"; import { convertToExcalidrawElements, type ExcalidrawElementSkeleton, @@ -1650,4 +1652,49 @@ describe("Test Transform", () => { ], }); }); + + it("should wrap and measure arrow labels once, along the arrows as placed", () => { + const wrapTextSpy = vi.spyOn(textWrapping, "wrapText"); + const measureTextSpy = vi.spyOn(textMeasurements, "measureText"); + 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", + id: "bound", + x: 100, + y: 10, + points: [pointFrom(0, 0), pointFrom(400, 0)], + start: { id: "A" }, + end: { id: "B" }, + label: { text: "aaaa bbbb cccc dddd eeee fff" }, + }, + { + type: "arrow", + id: "unbound", + x: 100, + y: 300, + points: [pointFrom(0, 0), pointFrom(400, 0)], + label: { text: "aaaa bbbb cccc dddd eeee fff" }, + }, + ], + opts, + ); + const labelOf = (arrowId: string) => + elements.find( + (element) => element.type === "text" && element.containerId === arrowId, + ) as ExcalidrawTextElement; + + // wrapped along the bound arrow shortened by its binding gaps, and along + // the unbound one as given + expect(labelOf("bound").text).toContain("\n"); + expect(labelOf("unbound").text).not.toContain("\n"); + // once each (plus each unwrapped label's measurement as created) + expect(wrapTextSpy).toHaveBeenCalledTimes(2); + expect(measureTextSpy).toHaveBeenCalledTimes(4); + + wrapTextSpy.mockRestore(); + measureTextSpy.mockRestore(); + }); }); diff --git a/packages/element/src/transform.ts b/packages/element/src/transform.ts index fb518a6ee6..a2ab67b7f5 100644 --- a/packages/element/src/transform.ts +++ b/packages/element/src/transform.ts @@ -315,7 +315,11 @@ const bindTextToContainer = ( : null), }); - redrawTextBoundingBox(textElement, container, scene, renderEnvironment); + // the caller measures an arrow's label once, along its arrow as placed + // (binding shortens a bound one) + if (!isArrowElement(container)) { + redrawTextBoundingBox(textElement, container, scene, renderEnvironment); + } return [container, textElement] as const; }; @@ -749,6 +753,11 @@ export const convertToExcalidrawElements = ( const scene = new Scene(elementsMap); // where on the arrows as given their labels' `labelPosition` puts them const labelPoints = new Map(); + // arrow labels yet to be measured, along their arrows + const unmeasuredArrowLabels = new Map< + ExcalidrawTextElement["id"], + [ExcalidrawArrowElement, ExcalidrawTextElement] + >(); // Add labels and arrow bindings for (const [id, element] of elementsWithIds) { @@ -771,6 +780,9 @@ export const convertToExcalidrawElements = ( ); elementStore.add(container); elementStore.add(text); + if (isArrowElement(container)) { + unmeasuredArrowLabels.set(text.id, [container, text]); + } // given or default alike: omitting it is the same as giving 0.5 if (isArrowElement(container) && text.labelPosition != null) { @@ -865,6 +877,7 @@ export const convertToExcalidrawElements = ( ) { continue; } + // also wraps and measures its label, along it as placed updateBoundArrow( arrow, boundScene, @@ -876,6 +889,7 @@ export const convertToExcalidrawElements = ( if (!label) { continue; } + unmeasuredArrowLabels.delete(label.id); // placing an arrow shortens its path, so its label's `labelPosition` would // no longer put it where it was placed (e.g. by a layout engine) const labelPoint = labelPoints.get(label.id); @@ -908,6 +922,10 @@ export const convertToExcalidrawElements = ( ), ); } + // the labels of arrows not placed, along them as given + for (const [arrow, label] of unmeasuredArrowLabels.values()) { + redrawTextBoundingBox(label, arrow, boundScene, opts?.renderEnvironment); + } // Once all the excalidraw elements are created, we can add frames since we // need to calculate coordinates and dimensions of frame which is possible after all