mirror of
https://github.com/excalidraw/excalidraw.git
synced 2026-11-20 21:23:22 +03:00
fix(element): measure and frame skeleton bound arrows as placed
Placing bound arrows in `convertToExcalidrawElements()` re-measured their labels without the given render environment (`updateBoundElements()` now forwards one to `handleBindTextResize()`), and ran after frames were sized around them, so a label rewrapped by its shortened arrow could stick out of its frame. Arrows are now placed before frames are sized. Claude Opus 5.5 (xhigh)
This commit is contained in:
3 files changed
+103
-25
No files matched your search
@@ -17,11 +17,14 @@ import {
|
||||
newIframeElement,
|
||||
} from "../newElement";
|
||||
|
||||
import { getCommonBounds } from "../bounds";
|
||||
import {
|
||||
convertToExcalidrawElements,
|
||||
type ExcalidrawElementSkeleton,
|
||||
} from "../transform";
|
||||
|
||||
import type { RenderEnvironment } from "../renderEnvironment";
|
||||
|
||||
import type {
|
||||
ExcalidrawArrowElement,
|
||||
ExcalidrawStickyNoteElement,
|
||||
@@ -1227,4 +1230,67 @@ describe("Test Transform", () => {
|
||||
const label = getLabel(arrow.id);
|
||||
expect(label.x + label.width / 2).toBeCloseTo(150);
|
||||
});
|
||||
|
||||
it("should measure bound arrow labels with the given render environment", () => {
|
||||
const renderEnvironment: RenderEnvironment = {
|
||||
createCanvas: () => document.createElement("canvas"),
|
||||
createImage: () => ({} as HTMLImageElement),
|
||||
textMetrics: { getLineWidth: () => 42 },
|
||||
};
|
||||
const elements = convertToExcalidrawElements(
|
||||
[
|
||||
{ type: "rectangle", id: "A", x: 0, y: 0, width: 100, height: 100 },
|
||||
{ type: "rectangle", id: "B", x: 400, y: 0, width: 100, height: 100 },
|
||||
{
|
||||
type: "arrow",
|
||||
x: 100,
|
||||
y: 50,
|
||||
points: [pointFrom<LocalPoint>(0, 0), pointFrom<LocalPoint>(300, 0)],
|
||||
start: { id: "A" },
|
||||
end: { id: "B" },
|
||||
label: { text: "LABEL" },
|
||||
},
|
||||
],
|
||||
{ ...opts, renderEnvironment },
|
||||
);
|
||||
const label = elements.find(
|
||||
(element) => element.type === "text",
|
||||
) as ExcalidrawTextElement;
|
||||
|
||||
expect(label.width).toBe(42);
|
||||
});
|
||||
|
||||
it("should size frames around bound arrows as placed by their bindings", () => {
|
||||
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: "arrow",
|
||||
x: 100,
|
||||
y: 10,
|
||||
points: [pointFrom<LocalPoint>(0, 0), pointFrom<LocalPoint>(400, 0)],
|
||||
start: { id: "A" },
|
||||
end: { id: "B" },
|
||||
// fits on one line along the arrow as given (280px), but wraps
|
||||
// along the arrow shortened by its binding gaps
|
||||
label: { text: "aaaa bbbb cccc dddd eeee fff" },
|
||||
},
|
||||
{ type: "frame", children: ["A", "B", "arrow"] },
|
||||
],
|
||||
opts,
|
||||
);
|
||||
const frame = elements.find((element) => element.type === "frame")!;
|
||||
const label = elements.find(
|
||||
(element) => element.type === "text",
|
||||
) as ExcalidrawTextElement;
|
||||
|
||||
expect(label.text).toContain("\n");
|
||||
const [, minY, , maxY] = getCommonBounds(
|
||||
elements.filter((element) => element.frameId === frame.id),
|
||||
);
|
||||
expect(frame.y).toBeLessThanOrEqual(minY);
|
||||
expect(frame.y + frame.height).toBeGreaterThanOrEqual(maxY);
|
||||
});
|
||||
});
|
||||
@@ -68,6 +68,7 @@ import {
|
||||
|
||||
import { isNonDeletedElement } from ".";
|
||||
|
||||
import type { RenderEnvironment } from "./renderEnvironment";
|
||||
import type { Scene } from "./Scene";
|
||||
|
||||
import type { ElementUpdate } from "./mutateElement";
|
||||
@@ -1325,6 +1326,8 @@ export const updateBoundElements = (
|
||||
/** see `getSimultaneouslyUpdatedElementIds` for how to pass it */
|
||||
simultaneouslyUpdated?: readonly NonDeletedExcalidrawElement[];
|
||||
changedElements?: Map<string, ExcalidrawElement>;
|
||||
/** measures the labels of the updated arrows */
|
||||
renderEnvironment?: RenderEnvironment;
|
||||
},
|
||||
) => {
|
||||
if (!isBindableElement(changedElement)) {
|
||||
@@ -1421,7 +1424,15 @@ export const updateBoundElements = (
|
||||
|
||||
const boundText = getBoundTextElement(element, elementsMap);
|
||||
if (boundText && !boundText.isDeleted) {
|
||||
handleBindTextResize(element, scene, false);
|
||||
handleBindTextResize(
|
||||
element,
|
||||
scene,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
options?.renderEnvironment,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -847,6 +847,31 @@ export const convertToExcalidrawElements = (
|
||||
}
|
||||
}
|
||||
|
||||
// Excalidraw places the ends of bound arrows itself whenever it updates
|
||||
// their bindings (e.g. on undo/redo, or moving what they're bound to), so
|
||||
// place them now, or they'd move then (before sizing frames around them)
|
||||
const boundScene = new Scene(elementStore.getElementsMap());
|
||||
// updating an arrow from either end updates both, so once is enough
|
||||
const updatedArrows: NonDeletedExcalidrawElement[] = [];
|
||||
for (const element of boundScene.getNonDeletedElements()) {
|
||||
const arrows = isBindableElement(element)
|
||||
? (element.boundElements ?? []).filter(({ type }) => type === "arrow")
|
||||
: [];
|
||||
if (arrows.length) {
|
||||
updateBoundElements(element, boundScene, {
|
||||
// a copy: the ids to skip are cached by array
|
||||
simultaneouslyUpdated: [...updatedArrows],
|
||||
renderEnvironment: opts?.renderEnvironment,
|
||||
});
|
||||
arrows.forEach(({ id }) => {
|
||||
const arrow = boundScene.getNonDeletedElement(id);
|
||||
if (arrow) {
|
||||
updatedArrows.push(arrow);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
// frame children are processed.
|
||||
@@ -917,29 +942,5 @@ export const convertToExcalidrawElements = (
|
||||
}
|
||||
}
|
||||
|
||||
// Excalidraw places the ends of bound arrows itself whenever it updates
|
||||
// their bindings (e.g. on undo/redo, or moving what they're bound to), so
|
||||
// place them now, or they'd move then
|
||||
const boundScene = new Scene(elementStore.getElementsMap());
|
||||
// updating an arrow from either end updates both, so once is enough
|
||||
const updatedArrows: NonDeletedExcalidrawElement[] = [];
|
||||
for (const element of boundScene.getNonDeletedElements()) {
|
||||
const arrows = isBindableElement(element)
|
||||
? (element.boundElements ?? []).filter(({ type }) => type === "arrow")
|
||||
: [];
|
||||
if (arrows.length) {
|
||||
updateBoundElements(element, boundScene, {
|
||||
// a copy: the ids to skip are cached by array
|
||||
simultaneouslyUpdated: [...updatedArrows],
|
||||
});
|
||||
arrows.forEach(({ id }) => {
|
||||
const arrow = boundScene.getNonDeletedElement(id);
|
||||
if (arrow) {
|
||||
updatedArrows.push(arrow);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return elementStore.getElements();
|
||||
};
|
||||
Reference in new issue
Block a user