mirror of
https://github.com/excalidraw/excalidraw.git
synced 2026-11-20 21:23:22 +03:00
perf(element): measure skeleton arrow labels once, as placed
Review finding 11. `bindTextToContainer` ran `redrawTextBoundingBox` on every label, so an arrow's label was wrapped and measured against the arrow as given, then the placement pass (`updateBoundArrow` -> `handleBindTextResize`) wrapped and measured it again against the arrow shortened by its binding gaps, always throwing the first result away. Reproduction: a scratch vitest mocked `wrapText` / `measureText` with counters and converted two shapes and a 400px arrow bound to both with the label "aaaa bbbb cccc dddd eeee fff". Failure case: observed wrapText x2 (maxWidth 280, then 271.6) and measureText x3 (newTextElement's unwrapped measurement, the discarded redraw, the placed rewrap); expected wrapText x1, measureText x2. Six labelled arrows on one shape: wrapText 13, measureText 20 (now 7 / 14). Unbound labelled arrows were already measured once (wrap 1 / measure 2). Fix: `bindTextToContainer` no longer measures arrow labels; the transform records them, the placement pass drops each label `updateBoundArrow` just measured, and the labels left (of arrows not placed) are measured after it with `redrawTextBoundingBox`, as before. Nothing between creating a label and the pass reads its size: binding only sets `startBinding` / `endBinding` (no points), `labelPoints` is still taken on the input arrow, and for arrow containers `handleBindTextResize` ignores the old label height. Measuring the leftovers after the pass, rather than predicting at creation which arrows get placed, means no label can go unmeasured. A scratch dump of 18 scenarios (bound / one end / same shape both ends / elbow / created and text ends / missing end id / ids before data / frame / single point / autoResize: false / labelPosition in and out of range / unbound / hub of six / shape labels) is identical before and after (seeds, nonces and timestamps stripped, versions kept), except the label `version` of placed arrows, one lower for the dropped mutation; the 4 snapshot changes are exactly that. Added a test asserting the wrap / measure counts for a bound and an unbound labelled arrow (fails before: 3 wraps vs 2).
This commit is contained in:
3 files changed
+70
-5
No files matched your search
@@ -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<Number>,
|
||||
"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<Number>,
|
||||
"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<Number>,
|
||||
"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<Number>,
|
||||
"verticalAlign": "middle",
|
||||
"width": 120,
|
||||
|
||||
@@ -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<LocalPoint>(0, 0), pointFrom<LocalPoint>(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<LocalPoint>(0, 0), pointFrom<LocalPoint>(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();
|
||||
});
|
||||
});
|
||||
@@ -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<ExcalidrawTextElement["id"], GlobalPoint>();
|
||||
// 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
|
||||
|
||||
Reference in new issue
Block a user