fix(element): clamp skeleton arrow label positions as restore does

Review finding 3.

Reproduction steps:
- Call `convertToExcalidrawElements` with arrows whose
  `label.labelPosition` is 5, -1, NaN, Infinity or -Infinity, both
  unbound and bound to two rectangles via `start`/`end`.
- Read `labelPosition` off the resulting text elements.

Failure case:
- Unbound arrows: 5 -> stored 5, -1 -> stored -1 (expected 1 and 0,
  what `restore` would produce via `clamp(v, 0, 1)`).
- Bound arrows: already 1 and 0, only because the placement pass
  re-derives `labelPosition` from the placed path.
- NaN/Infinity/-Infinity: 0.5 on both, since `cloneJSON` of the skeleton
  turns them into `null`, which falls back to the default.

Fix: `bindTextToContainer` now normalizes an arrow label's
`labelPosition` the way `restore` does: finite values are clamped to
[0,1], anything else falls back to DEFAULT_BOUND_TEXT_LABEL_POSITION.
This covers every arrow label at creation, independent of the cloneJSON
side effect and of whether the placement pass touches the arrow. The
`labelPoints` lookup is unaffected, as `getPointAtPathParameter` already
clamps and rejects non-finite values.
This commit is contained in:
Mark Tolmacs committed 2026-10-23 15:09:57 +00:00
1 parent ee5d106840
commit 6cf1c3c71a
2 files changed
+23 -1

No files matched your search

@@ -1235,6 +1235,24 @@ describe("Test Transform", () => {
expect(label.x + label.width / 2).toBeCloseTo(150);
});
it("should clamp out-of-range arrow label positions as restore does", () => {
const elements = convertToExcalidrawElements(
[-1, 5].map((labelPosition, i) => ({
type: "arrow" as const,
x: 100,
y: 100 * i,
points: [pointFrom<LocalPoint>(0, 0), pointFrom<LocalPoint>(200, 0)],
label: { text: "LABEL", labelPosition },
})),
opts,
);
const labels = elements.filter(
(element) => element.type === "text",
) as ExcalidrawTextElement[];
expect(labels.map((label) => label.labelPosition)).toEqual([0, 1]);
});
it("should measure bound arrow labels with the given render environment", () => {
const renderEnvironment: RenderEnvironment = {
createCanvas: () => document.createElement("canvas"),
+5 -1
View File
@@ -1,5 +1,6 @@
import {
clamp,
isFiniteNumber,
pointFrom,
vectorFromPoint,
vectorNormalize,
@@ -299,8 +300,11 @@ const bindTextToContainer = (
...textProps,
containerId: container.id,
strokeColor: stickyInk ?? (textProps.strokeColor || container.strokeColor),
// normalized as `restore` does
labelPosition: isArrowElement(container)
? textProps.labelPosition ?? DEFAULT_BOUND_TEXT_LABEL_POSITION
? isFiniteNumber(textProps.labelPosition)
? clamp(textProps.labelPosition, 0, 1)
: DEFAULT_BOUND_TEXT_LABEL_POSITION
: null,
renderEnvironment,
});