From 45276e6a94232ddbb4c91985de472803dd55f0a7 Mon Sep 17 00:00:00 2001 From: Mark Tolmacs Date: Fri, 23 Oct 2026 09:21:33 +0000 Subject: [PATCH] refactor(element): place skeleton bound arrows once each, per arrow Review finding 8. The placement pass in `convertToExcalidrawElements` simulated moving every bindable element (`updateBoundElements` per bindable) and repurposed `simultaneouslyUpdated` as an "already placed" ledger (a fresh copy per call to dodge the id-set cache), then re-collected the arrows each call touched for the label loop. Its correctness hinged on `updateBoundElements` internals: updating both ends from either one, the `doesNeedUpdate` stale `boundElements` filter, and the array-identity id cache. It already broke once (fdc8262d5). Reproduction / evidence: a scratch vitest dumped the output (minus seeds, nonces and timestamps, versions kept) of `convertToExcalidrawElements` for arrows bound across two shapes, to the same shape at both ends (2- and 4-point), elbow arrows (one and two ends bound), arrows in frames, six arrows on one labelled hub, text ends with ids referenced before the elements, and multi-point arrows, and spied on `movePoints`. Every bound arrow was placed exactly once. Failure case: none found. Skeleton bindings all go through `applyBinding`, which always lists the arrow on the bindable, so the ledger never skipped or double-placed an arrow. This is a maintainability fix. Fix: extract the per-arrow body of `updateBoundElements`' visitor into an exported `updateBoundArrow` (`updateBoundPoint` for each binding, then `movePoints` with `moveMidPointsWithElement` for arrows bound to one element at both ends, then `handleBindTextResize`), and call it from `updateBoundElements`. Dropped the visitor's per-binding `changedElement` check: after `doesNeedUpdate`, one end is always bound to `changedElement`, so it always held. The transform pass now iterates the arrows bound to elements present and places each once, re-deriving its label's `labelPosition` and x/y in the same loop. No ledger, no copies of it, no per-bindable calls. Outputs of the scratch dump, versions included, are byte-identical before and after; all existing tests and snapshots pass unchanged. --- packages/element/src/binding.ts | 147 +++++++++++++++--------------- packages/element/src/transform.ts | 60 +++++------- 2 files changed, 98 insertions(+), 109 deletions(-) diff --git a/packages/element/src/binding.ts b/packages/element/src/binding.ts index 76d594e418..dc6bc6ab88 100644 --- a/packages/element/src/binding.ts +++ b/packages/element/src/binding.ts @@ -1317,6 +1317,81 @@ export const reanchorBindingsToOutline = ( }); }; +/** + * Places both bound ends of `arrow` where its bindings put them on the elements + * in `elementsMap` they're bound to, and rewraps its label along it as placed. + * Leaves the label's x/y as they were (the editor derives arrow labels' x/y as + * it renders them), so callers returning the elements as data must store them + * (see `LinearElementEditor.getBoundTextElementPosition`). + */ +export const updateBoundArrow = ( + arrow: NonDeleted, + scene: Scene, + elementsMap: ElementsMap, + /** measures the arrow's label */ + renderEnvironment?: RenderEnvironment, +) => { + const startBindingElement = arrow.startBinding + ? elementsMap.get(arrow.startBinding.elementId) + : null; + const endBindingElement = arrow.endBinding + ? // PERF: If the arrow is bound to the same element on both ends. + startBindingElement?.id === arrow.endBinding.elementId + ? startBindingElement + : elementsMap.get(arrow.endBinding.elementId) + : null; + + const updates = bindableElementsVisitor( + elementsMap, + arrow, + (bindableElement, bindingProp) => { + if ( + bindableElement && + isBindableElement(bindableElement) && + (bindingProp === "startBinding" || bindingProp === "endBinding") + ) { + const point = updateBoundPoint( + arrow, + bindingProp, + arrow[bindingProp], + bindableElement, + elementsMap, + ); + + if (point) { + return [ + bindingProp === "startBinding" ? 0 : arrow.points.length - 1, + { point }, + ] as MapEntry; + } + } + + return null; + }, + ).filter( + (update): update is MapEntry => update !== null, + ); + + LinearElementEditor.movePoints(arrow, scene, new Map(updates), undefined, { + moveMidPointsWithElement: + !!startBindingElement && + startBindingElement?.id === endBindingElement?.id, + }); + + const boundText = getBoundTextElement(arrow, elementsMap); + if (boundText && !boundText.isDeleted) { + handleBindTextResize( + arrow, + scene, + false, + false, + false, + false, + renderEnvironment, + ); + } +}; + // Supports translating, rotating and scaling `changedElement` with bound // linear elements. export const updateBoundElements = ( @@ -1364,82 +1439,12 @@ export const updateBoundElements = ( return; } - // Check for intersections before updating bound elements incase connected elements overlap - const startBindingElement = element.startBinding - ? elementsMap.get(element.startBinding.elementId) - : null; - const endBindingElement = element.endBinding - ? // PERF: If the arrow is bound to the same element on both ends. - startBindingElement?.id === element.endBinding.elementId - ? startBindingElement - : elementsMap.get(element.endBinding.elementId) - : null; - // `linearElement` is being moved/scaled already, just update the binding if (simultaneouslyUpdatedElementIds.has(element.id)) { return; } - const updates = bindableElementsVisitor( - elementsMap, - element, - (bindableElement, bindingProp) => { - if ( - bindableElement && - isBindableElement(bindableElement) && - (bindingProp === "startBinding" || bindingProp === "endBinding") && - (changedElement.id === element[bindingProp]?.elementId || - changedElement.id === - element[ - bindingProp === "startBinding" ? "endBinding" : "startBinding" - ]?.elementId) - ) { - const point = updateBoundPoint( - element, - bindingProp, - element[bindingProp], - bindableElement, - elementsMap, - ); - - if (point) { - return [ - bindingProp === "startBinding" ? 0 : element.points.length - 1, - { point }, - ] as MapEntry; - } - } - - return null; - }, - ).filter( - (update): update is MapEntry => update !== null, - ); - - LinearElementEditor.movePoints( - element, - scene, - new Map(updates), - undefined, - { - moveMidPointsWithElement: - !!startBindingElement && - startBindingElement?.id === endBindingElement?.id, - }, - ); - - const boundText = getBoundTextElement(element, elementsMap); - if (boundText && !boundText.isDeleted) { - handleBindTextResize( - element, - scene, - false, - false, - false, - false, - options?.renderEnvironment, - ); - } + updateBoundArrow(element, scene, elementsMap, options?.renderEnvironment); }; boundElementsVisitor(elementsMap, changedElement, visitor); diff --git a/packages/element/src/transform.ts b/packages/element/src/transform.ts index 28c8c62b58..234a8352e7 100644 --- a/packages/element/src/transform.ts +++ b/packages/element/src/transform.ts @@ -31,7 +31,7 @@ import { import type { MarkOptional } from "@excalidraw/common/utility-types"; import type { Zoom } from "@excalidraw/excalidraw/types"; -import { bindBindingElement, updateBoundElements } from "./binding"; +import { bindBindingElement, updateBoundArrow } from "./binding"; import { newArrowElement, newElement, @@ -49,11 +49,7 @@ import { } from "./newElement"; import { normalizeStickyNoteStrokeColor } from "./stickyNote"; import { measureText, normalizeText } from "./textMeasurements"; -import { - isArrowElement, - isBindableElement, - isStickyNoteElement, -} from "./typeChecks"; +import { isArrowElement, isStickyNoteElement } from "./typeChecks"; import { syncInvalidIndices } from "./fractionalIndex"; @@ -917,41 +913,29 @@ export const convertToExcalidrawElements = ( // 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); - // only those actually bound to it, which it updated - if ( - arrow && - isArrowElement(arrow) && - (arrow.startBinding?.elementId === element.id || - arrow.endBinding?.elementId === element.id) - ) { - updatedArrows.push(arrow); - } - }); - } - } - - // 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 placedElementsMap = boundScene.getNonDeletedElementsMap(); - for (const arrow of updatedArrows) { - const label = getBoundTextElement(arrow, placedElementsMap); - if (!label || !isArrowElement(arrow)) { + for (const arrow of boundScene.getNonDeletedElements()) { + if ( + !isArrowElement(arrow) || + ![arrow.startBinding, arrow.endBinding].some( + (binding) => binding && placedElementsMap.has(binding.elementId), + ) + ) { continue; } + updateBoundArrow( + arrow, + boundScene, + placedElementsMap, + opts?.renderEnvironment, + ); + + const label = getBoundTextElement(arrow, placedElementsMap); + if (!label) { + continue; + } + // 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); if (labelPoint) { const labelPosition = LinearElementEditor.getPathParameterAtPoint(