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(