diff --git a/packages/element/src/binding.ts b/packages/element/src/binding.ts index 9166bb977f..60625d72e8 100644 --- a/packages/element/src/binding.ts +++ b/packages/element/src/binding.ts @@ -1217,23 +1217,22 @@ export const isEndBoundBeyondBindingArea = ( }; // how far at least to extend an arrow's end segment looking for what it's -// bound to (see `extendBoundEndOntoElement`) +// bound to (see `getBoundEndExtension`) const MIN_BOUND_END_EXTENSION = 100; /** * Extends the arrow's bound end along its end segment, by the segment's length - * (at least MIN_BOUND_END_EXTENSION), and if that reaches the element it's - * bound to, moves the end there, off the element by the binding gap. This + * (at least MIN_BOUND_END_EXTENSION), to find where it would reach the element + * it's bound to, off the element by the binding gap. Moving the end there * restores bindings given to arrows that just fall short of their elements. * - * @returns whether the end was moved onto the element + * @returns where to move the end to, or null if it doesn't reach the element */ -export const extendBoundEndOntoElement = ( - arrow: NonDeleted, +export const getBoundEndExtension = ( + arrow: ExcalidrawArrowElement, startOrEnd: "start" | "end", - scene: Scene, -): boolean => { - const elementsMap = scene.getNonDeletedElementsMap(); + elementsMap: NonDeletedSceneElementsMap, +): GlobalPoint | null => { const binding = startOrEnd === "start" ? arrow.startBinding : arrow.endBinding; const bindableElement = binding && elementsMap.get(binding.elementId); @@ -1242,13 +1241,12 @@ export const extendBoundEndOntoElement = ( !isBindableElement(bindableElement) || arrow.points.length < 2 ) { - return false; + return null; } - const index = startOrEnd === "start" ? 0 : arrow.points.length - 1; const end = LinearElementEditor.getPointAtIndexGlobalCoordinates( arrow, - index, + startOrEnd === "start" ? 0 : -1, elementsMap, ); const adjacent = LinearElementEditor.getPointAtIndexGlobalCoordinates( @@ -1258,7 +1256,7 @@ export const extendBoundEndOntoElement = ( ); const segmentLength = pointDistance(adjacent, end); if (segmentLength < PRECISION) { - return false; + return null; } const extension = lineSegment( @@ -1271,34 +1269,15 @@ export const extendBoundEndOntoElement = ( end, ), ); - const nextEnd = intersectElementWithLineSegment( - bindableElement, - elementsMap, - extension, - getBindingGap(bindableElement), - ).sort((a, b) => pointDistanceSq(a, end) - pointDistanceSq(b, end))[0]; - if (!nextEnd) { - return false; - } - - LinearElementEditor.movePoints( - arrow, - scene, - new Map([ - [ - index, - { - point: LinearElementEditor.pointFromAbsoluteCoords( - arrow, - nextEnd, - elementsMap, - ), - }, - ], - ]), + return ( + intersectElementWithLineSegment( + bindableElement, + elementsMap, + extension, + getBindingGap(bindableElement), + ).sort((a, b) => pointDistanceSq(a, end) - pointDistanceSq(b, end))[0] ?? + null ); - - return true; }; export const unbindBindingElement = ( diff --git a/packages/element/src/transform.ts b/packages/element/src/transform.ts index 224eca9c74..ee9fc29426 100644 --- a/packages/element/src/transform.ts +++ b/packages/element/src/transform.ts @@ -33,7 +33,7 @@ import type { Zoom } from "@excalidraw/excalidraw/types"; import { bindBindingElement, - extendBoundEndOntoElement, + getBoundEndExtension, isEndBoundBeyondBindingArea, unbindBindingElement, updateBoundArrow, @@ -98,6 +98,7 @@ import type { NonDeletedExcalidrawElement, NonDeletedSceneElementsMap, Ordered, + PointsPositionUpdates, TextAlign, VerticalAlign, ExcalidrawStickyNoteElement, @@ -968,26 +969,49 @@ export const convertToExcalidrawElements = ( } // users can't (re)bind an end given (e.g. by a layout engine) beyond the // binding area of what it's bound to, so don't leave it bound there, - // unless it just falls short of it: then extend it onto it + // unless it just falls short of it: then extend it onto it. Decide for + // both ends as given, as moving either end of an elbow arrow reroutes it + // from both its bindings, which could move the other end onto its element + const extendedEnds: PointsPositionUpdates = new Map(); for (const startOrEnd of ["start", "end"] as const) { if ( - isEndBoundBeyondBindingArea( + !isEndBoundBeyondBindingArea( arrow, startOrEnd, placedElementsMap, DEFAULT_ZOOM, - ) && - !extendBoundEndOntoElement(arrow, startOrEnd, scene) + ) ) { - const elementId = unbindBindingElement(arrow, startOrEnd, scene); - if (elementId) { - unboundArrowIds.set(elementId, [ - ...(unboundArrowIds.get(elementId) ?? []), - arrow.id, - ]); - } continue; } + const extendedEnd = getBoundEndExtension( + arrow, + startOrEnd, + placedElementsMap, + ); + if (extendedEnd) { + extendedEnds.set(startOrEnd === "start" ? 0 : arrow.points.length - 1, { + point: LinearElementEditor.pointFromAbsoluteCoords( + arrow, + extendedEnd, + placedElementsMap, + ), + }); + continue; + } + const elementId = unbindBindingElement(arrow, startOrEnd, scene); + if (elementId) { + unboundArrowIds.set(elementId, [ + ...(unboundArrowIds.get(elementId) ?? []), + arrow.id, + ]); + } + } + // once unbound, so elbow arrows aren't rerouted to what they were bound to + if (extendedEnds.size) { + LinearElementEditor.movePoints(arrow, scene, extendedEnds); + } + for (const startOrEnd of ["start", "end"] as const) { // it was bound before what it's bound to was sized if that came later // (e.g. to fit its label), so its fixed point may be off (e.g. the // center of a 0x0 element): rebind it against the element as sized