fix: missing edge cases

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>
This commit is contained in:
Mark Tolmacs committed 2026-09-30 09:55:54 +02:00
1 parent 55204381fe
commit 75706d02b4
3 files changed
+426 -198

No files matched your search

+84 -31
View File
@@ -47,7 +47,8 @@ export type BindingRepairAction =
// is deleted: drop the binding on both sides.
| "unbindDangling"
// `boundElements` records on a bindable element that do not resolve to a live
// element, or whose reverse half no longer points back: drop the record.
// element, or whose reverse half no longer points back: drop the record. A
// record with a valid back-link but the wrong `type` gets its type fixed.
| "pruneBoundElements"
// duplicate `boundElements` entries sharing an id: keep the first one, so the
// surviving order is the order the entries were first listed in.
@@ -244,9 +245,9 @@ const resolveTarget = (
/**
* Whether `element` records a binding onto `bindableElementId`. Checks only the
* id and the reverse half; the record's `type` field is validated separately by
* {@link hasBoundElementType}, since a wrong type is a distinct defect that the
* reverse half alone would not catch.
* id and the reverse half; the record's `type` field is checked separately
* against {@link getBoundElementType}, since a wrong type is a distinct defect
* that the reverse half alone would not catch.
*/
const isBoundTo = (
element: ExcalidrawElement,
@@ -267,25 +268,24 @@ const isBoundTo = (
};
/**
* Whether a `boundElements` record's `type` matches the element it points at.
* The `type` a `boundElements` record pointing at `bound` must carry, or null
* when `bound` can never be a bound element. Records may only be
* `{ id, type: "arrow" }` or `{ id, type: "text" }`, and
* `getBoundTextElementId` looks bound text up by `type === "text"`, so a text
* element recorded as `type: "arrow"` would silently stop resolving; the
* reverse is equally a defect. Records may only be `{ id, type: "arrow" }` or
* `{ id, type: "text" }`.
* element recorded as `type: "arrow"` would silently stop resolving.
*/
const hasBoundElementType = (
const getBoundElementType = (
bound: ExcalidrawElement,
record: NonNullable<ExcalidrawElement["boundElements"]>[number],
): boolean => {
): "arrow" | "text" | null => {
if (isArrowElement(bound)) {
return record.type === "arrow";
return "arrow";
}
if (isTextElement(bound)) {
return record.type === "text";
return "text";
}
return false;
return null;
};
/**
@@ -321,6 +321,11 @@ const unbindDangling = (state: RepairState): void => {
* reverse half no longer points back at the owner. This is the container-side
* counterpart of {@link unbindDangling} and also covers containers still listing
* a deleted arrow.
*
* A record whose id and reverse half are valid but whose `type` is wrong (e.g.
* a label recorded as `type: "arrow"`) is rewritten rather than dropped:
* dropping it would leave the text's `containerId` pointing at a container
* that no longer lists it, i.e. bound on one side only.
*/
const pruneBoundElements = (state: RepairState): void => {
for (const bindable of state.bindables()) {
@@ -328,33 +333,49 @@ const pruneBoundElements = (state: RepairState): void => {
continue;
}
const kept = bindable.boundElements.filter((boundElement) => {
let changed = false;
const kept: NonNullable<ExcalidrawElement["boundElements"]>[number][] = [];
for (const boundElement of bindable.boundElements) {
const bound = state.elementsMap.get(boundElement.id);
const expectedType = bound ? getBoundElementType(bound) : null;
const reason = !bound
? "is missing"
: bound.isDeleted
? "is deleted"
: !hasBoundElementType(bound, boundElement)
? `is recorded as type "${boundElement.type}" but is a ` +
`"${bound.type}"`
: !expectedType
? `is a "${bound.type}", which cannot be bound`
: !isBoundTo(bound, bindable.id)
? "no longer binds back"
: null;
if (!reason) {
return true;
if (reason) {
report(
state,
`bindable "${bindable.id}" lists bound element ` +
`"${boundElement.id}", which ${reason}; dropping record`,
);
changed = true;
continue;
}
report(
state,
`bindable "${bindable.id}" lists bound element ` +
`"${boundElement.id}", which ${reason}; dropping record`,
);
return false;
});
if (boundElement.type !== expectedType) {
report(
state,
`bindable "${bindable.id}" records bound element ` +
`"${boundElement.id}" as type "${boundElement.type}" but it is ` +
`a "${expectedType}"; fixing record type`,
);
kept.push({ ...boundElement, type: expectedType! });
changed = true;
continue;
}
if (kept.length !== bindable.boundElements.length) {
kept.push(boundElement);
}
if (changed) {
state.scene.mutateElement(bindable, { boundElements: kept });
}
}
@@ -443,7 +464,13 @@ const isSameBinding = (a: FixedPointBinding, b: FixedPointBinding): boolean =>
a.fixedPoint[0] === b.fixedPoint[0] &&
a.fixedPoint[1] === b.fixedPoint[1];
/** Re-derives a binding's `fixedPoint` against its (possibly moved) target. */
const isBindMode = (mode: unknown): mode is BindMode =>
mode === "inside" || mode === "orbit" || mode === "skip";
/**
* Re-derives a binding's `fixedPoint` against its (possibly moved) target, and
* its `mode` when that is missing or unknown.
*/
const reanchorBinding = (
state: RepairState,
arrow: NonDeleted<ExcalidrawArrowElement>,
@@ -478,6 +505,16 @@ const reanchorBinding = (
return {
...binding,
// a missing or unknown `mode` is inferred from where the endpoint sits now,
// the same way `inferMissingBindings` picks it for a fresh binding
mode: isBindMode(binding.mode)
? binding.mode
: getInferredBindMode(
arrow,
target,
endpointGlobalPoint(arrow, endpoint, state.nonDeletedMap),
state.nonDeletedMap,
),
...calculateFixedPointForNonElbowArrowBinding(
arrow,
target,
@@ -491,7 +528,7 @@ const reanchorBinding = (
/**
* Re-derives `mode`/`fixedPoint` for every binding so endpoints follow their
* targets. Covers both a moved target and a binding the generator wrote
* incompletely (missing `fixedPoint`).
* incompletely (missing `fixedPoint` or `mode`).
*/
const reanchorBindings = (state: RepairState): void => {
forEachBinding(state, (arrow, endpoint, binding) => {
@@ -614,7 +651,8 @@ const findBindTarget = (
/**
* Creates bindings for endpoints that geometrically touch a bindable element
* but carry no binding.
* but carry no binding. An endpoint is never bound to the interior of a shape
* that also contains the arrow's other endpoint.
*/
const inferMissingBindings = (state: RepairState): void => {
for (const arrow of state.arrows()) {
@@ -650,6 +688,21 @@ const inferMissingBindings = (state: RepairState): void => {
state.nonDeletedMap,
);
if (
mode === "inside" &&
isPointInElement(
endpointGlobalPoint(
arrow,
endpoint === ENDPOINTS.start ? ENDPOINTS.end : ENDPOINTS.start,
state.nonDeletedMap,
),
target,
state.nonDeletedMap,
)
) {
continue;
}
bindBindingElement(
arrow,
target,
+4 -1
View File
@@ -507,7 +507,10 @@ export {
export { elementsOverlappingBBox } from "@excalidraw/element";
export { repairBindings } from "@excalidraw/element";
export {
ALL_BINDING_REPAIR_ACTIONS,
repairBindings,
} from "@excalidraw/element";
export type {
BindingRepairAction,
RepairBindingsOptions,
+338 -166
View File
@@ -6,11 +6,14 @@ import {
updateBoundElements,
} from "@excalidraw/element";
import { pointFrom } from "@excalidraw/math";
import type {
ExcalidrawArrowElement,
ExcalidrawBindableElement,
ExcalidrawElement,
ExcalidrawElbowArrowElement,
ExcalidrawTextElement,
} from "@excalidraw/element/types";
const REPAIR_ALL = { warn: false } as const;
@@ -42,10 +45,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "missing",
fixedPoint: [0.5, 0.5],
@@ -78,10 +78,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "missing",
fixedPoint: [0.5, 0.5],
@@ -98,7 +95,9 @@ describe("repairBindings", () => {
...REPAIR_ALL,
actions: ["unbindDangling"],
});
const fixedArrow = fixed.find((el) => el.id === "arrow")!;
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
const fixedRect = fixed.find((el) => el.id === "rect")!;
expect(fixedArrow.startBinding).toBeNull();
@@ -122,10 +121,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "rect",
fixedPoint: [0.5, 0.5],
@@ -137,7 +133,9 @@ describe("repairBindings", () => {
// a deleted target is absent from the non-deleted map, so this used to
// throw inside `unbindBindingElement` rather than clear the binding
const fixed = repairBindings([rect, arrow], REPAIR_ALL);
const fixedArrow = fixed.find((el) => el.id === "arrow")!;
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
const fixedRect = fixed.find((el) => el.id === "rect")!;
expect(fixedArrow.startBinding).toBeNull();
@@ -162,10 +160,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "missing",
fixedPoint: [0.5, 0.5],
@@ -175,7 +170,9 @@ describe("repairBindings", () => {
});
const fixed = repairBindings([rect, arrow], REPAIR_ALL);
const fixedArrow = fixed.find((el) => el.id === "arrow")!;
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
expect(fixedArrow.startBinding?.elementId).toBe("rect");
});
@@ -195,10 +192,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "nope",
fixedPoint: [0.5, 0.5],
@@ -215,7 +209,9 @@ describe("repairBindings", () => {
...REPAIR_ALL,
actions: ["unbindDangling"],
});
const fixedArrow = fixed.find((el) => el.id === "arrow")!;
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
const fixedRect = fixed.find((el) => el.id === "rect")!;
expect(fixedArrow.startBinding).toBeNull();
@@ -230,10 +226,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 0,
y: 0,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "missing",
fixedPoint: [0.5, 0.5],
@@ -247,7 +240,9 @@ describe("repairBindings", () => {
actions: ["unbindDangling"],
arrowIds: ["some-other-arrow"],
});
const fixedArrow = fixed.find((el) => el.id === "arrow")!;
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
// out of scope, so the dangling binding is left untouched
expect(fixedArrow.startBinding?.elementId).toBe("missing");
@@ -273,10 +268,7 @@ describe("repairBindings", () => {
id: "present",
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "rect",
fixedPoint: [0.5, 0.5],
@@ -309,10 +301,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 500,
y: 500,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: null,
endBinding: null,
});
@@ -326,7 +315,7 @@ describe("repairBindings", () => {
expect(fixedRect.boundElements).toEqual([]);
});
it("drops a record whose type disagrees with the element it points at", () => {
it("fixes the type of a record that otherwise binds back", () => {
const rect = API.createElement({
type: "rectangle",
id: "rect",
@@ -349,6 +338,42 @@ describe("repairBindings", () => {
containerId: "rect",
});
const fixed = repairBindings([rect, text], {
...REPAIR_ALL,
actions: ["pruneBoundElements"],
});
const fixedRect = fixed.find((el) => el.id === "rect")!;
const fixedText = fixed.find(
(el) => el.id === "text",
) as ExcalidrawTextElement;
// the record is corrected rather than dropped, so the label stays bound
// on both sides
expect(fixedRect.boundElements).toEqual([{ id: "text", type: "text" }]);
expect(fixedText.containerId).toBe("rect");
});
it("drops a mistyped record that does not bind back", () => {
const rect = API.createElement({
type: "rectangle",
id: "rect",
x: 0,
y: 0,
width: 100,
height: 100,
boundElements: [{ id: "text", type: "arrow" }],
});
const text = API.createElement({
type: "text",
id: "text",
x: 0,
y: 0,
width: 50,
height: 20,
text: "hi",
containerId: null,
});
const fixed = repairBindings([rect, text], {
...REPAIR_ALL,
actions: ["pruneBoundElements"],
@@ -405,10 +430,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "rect",
fixedPoint: [0.5, 0.5],
@@ -433,10 +455,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 0,
y: 0,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "missing",
fixedPoint: [0.5, 0.5],
@@ -468,10 +487,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "rect",
fixedPoint: [0.5, 0.5],
@@ -505,11 +521,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 0,
y: 0,
points: [
[0, 50],
[50, 0],
[100, 50],
],
points: [pointFrom(0, 50), pointFrom(50, 0), pointFrom(100, 50)],
startBinding: {
elementId: "rect",
fixedPoint: [0.5, 0.5],
@@ -546,10 +558,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "rect",
fixedPoint: [0.5, 0.5],
@@ -583,10 +592,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "rect",
fixedPoint: [0.5, 0.5],
@@ -607,7 +613,7 @@ describe("repairBindings", () => {
// what a drag does: move the container, then run the bound-element update
movedRect.x = 500;
updateBoundElements(movedRect, scene, {} as any, new Map());
updateBoundElements(movedRect, scene);
const afterArrow = scene
.getElementsMapIncludingDeleted()
@@ -666,16 +672,15 @@ describe("repairBindings", () => {
id: "arrow",
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: null,
endBinding: null,
});
const fixed = repairBindings([rect, arrow], REPAIR_ALL);
const fixedArrow = fixed.find((el) => el.id === "arrow")!;
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
const fixedRect = fixed.find((el) => el.id === "rect")!;
expect(fixedArrow.startBinding).toEqual(
@@ -684,6 +689,137 @@ describe("repairBindings", () => {
expect(fixedRect.boundElements).toEqual([{ id: "arrow", type: "arrow" }]);
});
describe("see-through group boxes", () => {
const groupBox = () =>
API.createElement({
type: "rectangle",
id: "group",
x: 0,
y: 0,
width: 500,
height: 500,
backgroundColor: "transparent",
});
const innerRect = () =>
API.createElement({
type: "rectangle",
id: "inner",
x: 300,
y: 200,
width: 100,
height: 100,
});
it("does not bind an arrow lying entirely inside the box", () => {
const arrow = API.createElement({
type: "arrow",
id: "arrow",
x: 100,
y: 250,
points: [pointFrom(0, 0), pointFrom(100, 0)],
startBinding: null,
endBinding: null,
});
const fixed = repairBindings([groupBox(), arrow], REPAIR_ALL);
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
const fixedGroup = fixed.find((el) => el.id === "group")!;
expect(fixedArrow.startBinding).toBeNull();
expect(fixedArrow.endBinding).toBeNull();
expect(fixedGroup.boundElements ?? []).toEqual([]);
});
it("binds the end that reaches an inner shape, not the box", () => {
const arrow = API.createElement({
type: "arrow",
id: "arrow",
x: 100,
y: 250,
// end sits on the inner rect's left edge, start falls short of
// anything
points: [pointFrom(0, 0), pointFrom(200, 0)],
startBinding: null,
endBinding: null,
});
const fixed = repairBindings(
[groupBox(), innerRect(), arrow],
REPAIR_ALL,
);
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
expect(fixedArrow.startBinding).toBeNull();
expect(fixedArrow.endBinding?.elementId).toBe("inner");
});
it("still binds inside when the arrow points in from outside", () => {
const arrow = API.createElement({
type: "arrow",
id: "arrow",
x: -200,
y: 250,
points: [pointFrom(0, 0), pointFrom(300, 0)],
startBinding: null,
endBinding: null,
});
const fixed = repairBindings([groupBox(), arrow], REPAIR_ALL);
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
expect(fixedArrow.startBinding).toBeNull();
expect(fixedArrow.endBinding).toEqual(
expect.objectContaining({ elementId: "group", mode: "inside" }),
);
});
it("still binds center-to-center arrows between separate boxes", () => {
const a = API.createElement({
type: "rectangle",
id: "a",
x: 0,
y: 0,
width: 100,
height: 100,
});
const b = API.createElement({
type: "rectangle",
id: "b",
x: 300,
y: 0,
width: 100,
height: 100,
});
const arrow = API.createElement({
type: "arrow",
id: "arrow",
x: 50,
y: 50,
points: [pointFrom(0, 0), pointFrom(300, 0)],
startBinding: null,
endBinding: null,
});
const fixed = repairBindings([a, b, arrow], REPAIR_ALL);
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
expect(fixedArrow.startBinding).toEqual(
expect.objectContaining({ elementId: "a", mode: "inside" }),
);
expect(fixedArrow.endBinding).toEqual(
expect.objectContaining({ elementId: "b", mode: "inside" }),
);
});
});
it("leaves an endpoint alone when no bindable element is in reach", () => {
const rect = API.createElement({
type: "rectangle",
@@ -698,16 +834,15 @@ describe("repairBindings", () => {
id: "arrow",
x: 1000,
y: 1000,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: null,
endBinding: null,
});
const fixed = repairBindings([rect, arrow], REPAIR_ALL);
const fixedArrow = fixed.find((el) => el.id === "arrow")!;
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
const fixedRect = fixed.find((el) => el.id === "rect")!;
expect(fixedArrow.startBinding).toBeNull();
@@ -729,10 +864,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: null,
endBinding: null,
});
@@ -741,7 +873,9 @@ describe("repairBindings", () => {
...REPAIR_ALL,
arrowIds: ["some-other-arrow"],
});
const fixedArrow = fixed.find((el) => el.id === "arrow")!;
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
expect(fixedArrow.startBinding).toBeNull();
});
@@ -761,16 +895,15 @@ describe("repairBindings", () => {
id: "arrow",
x: 300,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: null,
endBinding: null,
});
const fixed = repairBindings([rect, arrow], REPAIR_ALL);
const fixedArrow = fixed.find((el) => el.id === "arrow")!;
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
expect(fixedArrow.startBinding).toBeNull();
});
@@ -790,23 +923,20 @@ describe("repairBindings", () => {
id: "arrow",
x: 120,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: null,
endBinding: null,
});
const withoutTolerance = repairBindings([rect, arrow], REPAIR_ALL).find(
(el) => el.id === "arrow",
)!;
) as ExcalidrawArrowElement;
expect(withoutTolerance.startBinding).toBeNull();
const withTolerance = repairBindings([rect, arrow], {
...REPAIR_ALL,
tolerance: 30,
}).find((el) => el.id === "arrow")!;
}).find((el) => el.id === "arrow") as ExcalidrawArrowElement;
expect(withTolerance.startBinding).toEqual(
expect.objectContaining({ elementId: "rect" }),
);
@@ -820,10 +950,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "rect",
// stale: rect now sits at x=200
@@ -846,7 +973,9 @@ describe("repairBindings", () => {
...REPAIR_ALL,
actions: ["reanchorBindings"],
});
const fixedArrow = fixed.find((el) => el.id === "arrow")!;
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
expect(fixedArrow.startBinding?.elementId).toBe("rect");
expect(fixedArrow.startBinding?.fixedPoint).toEqual(
@@ -855,6 +984,83 @@ describe("repairBindings", () => {
expect(fixedArrow.startBinding?.mode).toBe("orbit");
});
it.each([
// endpoint just outside the rect's left edge
{ x: 95, expected: "orbit" },
// endpoint well inside the rect
{ x: 150, expected: "inside" },
])("infers a missing mode as $expected", ({ x, expected }) => {
const rect = API.createElement({
type: "rectangle",
id: "rect",
x: 100,
y: 0,
width: 100,
height: 100,
boundElements: [{ id: "arrow", type: "arrow" }],
});
const arrow = API.createElement({
type: "arrow",
id: "arrow",
x,
y: 50,
points: [pointFrom(0, 0), pointFrom(-50, 0)],
// generator omitted `mode`
startBinding: {
elementId: "rect",
fixedPoint: [0.5, 0.5],
} as any,
endBinding: null,
});
const fixed = repairBindings([rect, arrow], {
...REPAIR_ALL,
actions: ["reanchorBindings"],
});
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
expect(fixedArrow.startBinding?.elementId).toBe("rect");
expect(fixedArrow.startBinding?.mode).toBe(expected);
});
it("keeps a valid explicit mode", () => {
const rect = API.createElement({
type: "rectangle",
id: "rect",
x: 100,
y: 0,
width: 100,
height: 100,
boundElements: [{ id: "arrow", type: "arrow" }],
});
const arrow = API.createElement({
type: "arrow",
id: "arrow",
// endpoint inside the rect, which would infer "inside"
x: 150,
y: 50,
points: [pointFrom(0, 0), pointFrom(-100, 0)],
startBinding: {
elementId: "rect",
fixedPoint: [0.5, 0.5],
mode: "orbit",
},
endBinding: null,
});
const fixed = repairBindings([rect, arrow], {
...REPAIR_ALL,
actions: ["reanchorBindings"],
});
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
expect(fixedArrow.startBinding?.mode).toBe("orbit");
});
it("restores a missing fixedPoint", () => {
const rect = API.createElement({
type: "rectangle",
@@ -870,10 +1076,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "rect",
fixedPoint: undefined as unknown as [number, number],
@@ -886,7 +1089,9 @@ describe("repairBindings", () => {
...REPAIR_ALL,
actions: ["reanchorBindings"],
});
const fixedArrow = fixed.find((el) => el.id === "arrow")!;
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
expect(fixedArrow.startBinding?.fixedPoint).toEqual(
expect.arrayContaining([expect.any(Number), expect.any(Number)]),
@@ -911,10 +1116,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 40,
y: 50,
points: [
[0, 0],
[60, 0],
],
points: [pointFrom(0, 0), pointFrom(60, 0)],
startBinding: {
elementId: "rect",
fixedPoint: [1, 0.5],
@@ -956,10 +1158,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "missing",
fixedPoint: [0.5, 0.5],
@@ -973,7 +1172,9 @@ describe("repairBindings", () => {
...REPAIR_ALL,
actions: ["inferMissingBindings"],
});
const fixedArrow = fixed.find((el) => el.id === "arrow")!;
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
expect(fixedArrow.startBinding?.elementId).toBe("missing");
});
@@ -994,10 +1195,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: null,
endBinding: null,
});
@@ -1029,10 +1227,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 0,
y: 0,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "missing",
fixedPoint: [0.5, 0.5],
@@ -1059,10 +1254,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 0,
y: 0,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "missing",
fixedPoint: [0.5, 0.5],
@@ -1098,10 +1290,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "stale",
fixedPoint: [0.5, 0.5],
@@ -1111,7 +1300,9 @@ describe("repairBindings", () => {
});
const fixed = repairBindings([rect, arrow], { warn: true });
const fixedArrow = fixed.find((el) => el.id === "arrow")!;
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
// the repair succeeded, so nothing is reported
expect(fixedArrow.startBinding?.elementId).toBe("rect");
@@ -1137,10 +1328,7 @@ describe("repairBindings", () => {
x: 100,
y: 50,
index: null,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: null,
endBinding: null,
});
@@ -1189,16 +1377,15 @@ describe("repairBindings", () => {
x: 100,
y: 50,
locked: true,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: null,
endBinding: null,
});
const fixed = repairBindings([rect, arrow], REPAIR_ALL);
const fixedArrow = fixed.find((el) => el.id === "arrow")!;
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
expect(fixedArrow.startBinding?.elementId).toBe("rect");
});
@@ -1219,10 +1406,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "rect",
fixedPoint: [1, 0.5],
@@ -1232,7 +1416,9 @@ describe("repairBindings", () => {
});
const fixed = repairBindings([rect, arrow], REPAIR_ALL);
const fixedArrow = fixed.find((el) => el.id === "arrow")!;
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
const fixedRect = fixed.find((el) => el.id === "rect")!;
expect(fixedArrow.startBinding?.elementId).toBe("rect");
@@ -1254,16 +1440,15 @@ describe("repairBindings", () => {
id: "arrow",
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: null,
endBinding: null,
});
const fixed = repairBindings([rect, arrow], REPAIR_ALL);
const fixedArrow = fixed.find((el) => el.id === "arrow")!;
const fixedArrow = fixed.find(
(el) => el.id === "arrow",
) as ExcalidrawArrowElement;
expect(fixedArrow.startBinding).toBeNull();
});
@@ -1319,10 +1504,7 @@ describe("repairBindings", () => {
elbowed: true,
x: 100,
y: 50,
points: [
[0, 0],
[50, 40],
],
points: [pointFrom(0, 0), pointFrom(50, 40)],
startBinding: {
elementId: "rect",
fixedPoint: [0.5, 0.5],
@@ -1354,14 +1536,10 @@ describe("repairBindings", () => {
elbowed: true,
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
[100, 40],
],
points: [pointFrom(0, 0), pointFrom(50, 0), pointFrom(100, 40)],
fixedSegments: [
{ start: [0, 0], end: [10, 10], index: 99 },
{ start: [5, 5], end: [999, 999], index: -3 },
{ start: pointFrom(0, 0), end: pointFrom(10, 10), index: 99 },
{ start: pointFrom(5, 5), end: pointFrom(999, 999), index: -3 },
],
startBinding: {
elementId: "rect",
@@ -1390,10 +1568,7 @@ describe("repairBindings", () => {
elbowed: true,
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "rect",
fixedPoint: [0.5, 0.5],
@@ -1419,10 +1594,7 @@ describe("repairBindings", () => {
id: "arrow",
x: 100,
y: 50,
points: [
[0, 0],
[50, 0],
],
points: [pointFrom(0, 0), pointFrom(50, 0)],
startBinding: {
elementId: "rect",
fixedPoint: [0.5, 0.5],
@@ -1446,11 +1618,11 @@ describe("repairBindings", () => {
describe("repairBindings types", () => {
it("accepts a typed bindable array", () => {
const rect: ExcalidrawBindableElement = API.createElement({
const rect = API.createElement({
type: "rectangle",
x: 0,
y: 0,
});
}) as ExcalidrawBindableElement;
const fixed: readonly ExcalidrawElement[] = repairBindings([rect], {
actions: ["pruneBoundElements"],