From 986c1956258a39e940c64c0b817807276a5bb038 Mon Sep 17 00:00:00 2001 From: dwelle <5153846+dwelle@users.noreply.github.com> Date: Mon, 2 Nov 2026 09:50:12 +0100 Subject: [PATCH] fix(editor): take arrows out of frames once neither of their ends is in them An arrow belongs to its frame only while one of its ends is in it: bound to an element in the frame, or, unbound, inside it. Checked when an arrow is finished (drawn, or an end dragged), when a drag ends (the dragged arrows and those bound to the dragged elements), and when a frame's children are recomputed (resize, grouping). An arrow bound only to elements outside its frame thus no longer gets cut, duplicated or exported with it. Claude Opus 5.5 (xhigh reasoning) --- dev-docs/docs/codebase/frames.mdx | 2 +- packages/element/src/frame.ts | 70 ++++++++++++++++++- packages/element/tests/frame.test.tsx | 44 ++++++++++++ packages/excalidraw/CHANGELOG.md | 2 +- .../excalidraw/actions/actionFinalize.tsx | 3 + .../components/App.selectionTool.ts | 3 + 6 files changed, 121 insertions(+), 3 deletions(-) diff --git a/dev-docs/docs/codebase/frames.mdx b/dev-docs/docs/codebase/frames.mdx index 4e3d461655..efab39edb5 100644 --- a/dev-docs/docs/codebase/frames.mdx +++ b/dev-docs/docs/codebase/frames.mdx @@ -25,4 +25,4 @@ If not ordered correctly, the editor will still function, but the elements may n Frame children are clipped to their frame, and can't be hit (selected, hovered, box- or lasso-selected) outside of it. -Arrows (with their labels) are the exception: they're never clipped, so that they can connect elements across and out of frames. They still belong to the frame they're drawn from, so they're moved, copied and deleted with it. When exporting a frame, arrows of other frames reaching into it are exported too, as are any other non-children overlapping it. When moving a frame (dragging it, with the arrow keys, or from the stats panel), its arrows bound to elements that don't move with it stay bound to them (see `moveFrameArrows`). +Arrows (with their labels) are the exception: they're never clipped, so that they can connect elements across and out of frames. They still belong to the frame they're drawn from, so they're moved, copied and deleted with it, but only while one of their ends is in it: bound to an element in the frame, or, unbound, inside it (see `removeArrowsWithoutEndInFrame`). An arrow bound only to elements outside the frame thus leaves it. When exporting a frame, arrows of other frames reaching into it are exported too, as are any other non-children overlapping it. When moving a frame (dragging it, with the arrow keys, or from the stats panel), its arrows bound to elements that don't move with it stay bound to them (see `moveFrameArrows`). diff --git a/packages/element/src/frame.ts b/packages/element/src/frame.ts index b6bd863929..d68fd8fa30 100644 --- a/packages/element/src/frame.ts +++ b/packages/element/src/frame.ts @@ -32,6 +32,7 @@ import { import { mutateElement } from "./mutateElement"; import { getBoundTextElement, getContainerElement } from "./textElement"; import { syncMovedIndices } from "./fractionalIndex"; +import { LinearElementEditor } from "./linearElementEditor"; import { isArrowElement, isFrameElement, @@ -46,6 +47,7 @@ import type { ExcalidrawElementsIncludingDeleted } from "./Scene"; import type { ElementsMap, ElementsMapOrArray, + ExcalidrawArrowElement, ExcalidrawElement, ExcalidrawFrameLikeElement, NonDeleted, @@ -719,6 +721,70 @@ export const removeElementsFromFrame = ( } }; +/** + * Whether one of the arrow's ends is in the frame: bound to an element in it, + * or, unbound, inside it. An arrow belongs to a frame only while one is. + */ +export const isArrowEndInFrame = ( + arrow: ExcalidrawArrowElement, + frame: ExcalidrawFrameLikeElement, + elementsMap: ElementsMap, +) => { + const [x1, y1, x2, y2] = getElementAbsoluteCoords(frame, elementsMap); + return [arrow.startBinding, arrow.endBinding].some((binding, index) => + binding + ? elementsMap.get(binding.elementId)?.frameId === frame.id + : isPointWithinBounds( + pointFrom(x1, y1), + LinearElementEditor.getPointAtIndexGlobalCoordinates( + arrow, + index ? -1 : 0, + elementsMap, + ), + pointFrom(x2, y2), + ), + ); +}; + +/** + * Takes the arrows among `elements`, and those bound to them, that have no end + * in their frame (see `isArrowEndInFrame()`) out of it. Call it once their ends + * (and the elements they're bound to) may have left it. Arrows in groups are + * left to their group. + */ +export const removeArrowsWithoutEndInFrame = ( + elements: Iterable, + elementsMap: ElementsMap, +) => { + const arrows = new Set(); + for (const element of elements) { + if (isArrowElement(element)) { + arrows.add(element); + } + for (const { id, type } of element.boundElements ?? []) { + const arrow = type === "arrow" ? elementsMap.get(id) : undefined; + if (isArrowElement(arrow)) { + arrows.add(arrow); + } + } + } + + const arrowsToRemove = []; + for (const arrow of arrows) { + const frame = arrow.frameId ? elementsMap.get(arrow.frameId) : undefined; + if ( + !arrow.isDeleted && + !arrow.groupIds.length && + frame && + isFrameLikeElement(frame) && + !isArrowEndInFrame(arrow, frame, elementsMap) + ) { + arrowsToRemove.push(arrow); + } + } + removeElementsFromFrame(arrowsToRemove, elementsMap); +}; + export const removeAllElementsFromFrame = ( allElements: readonly T[], frame: ExcalidrawFrameLikeElement, @@ -733,11 +799,13 @@ export const replaceAllElementsInFrame = ( nextElementsInFrame: ExcalidrawElement[], frame: ExcalidrawFrameLikeElement, ): T[] => { - return addElementsToFrame( + const nextElements = addElementsToFrame( removeAllElementsFromFrame(allElements, frame), nextElementsInFrame, frame, ).slice(); + removeArrowsWithoutEndInFrame(nextElementsInFrame, arrayToMap(nextElements)); + return nextElements; }; /** does not mutate elements, but returns new ones */ diff --git a/packages/element/tests/frame.test.tsx b/packages/element/tests/frame.test.tsx index 3158ace40b..9f4dee5302 100644 --- a/packages/element/tests/frame.test.tsx +++ b/packages/element/tests/frame.test.tsx @@ -1511,6 +1511,50 @@ describe("arrows in frames", () => { ).toEqual(endPoint); }); + it("should take an arrow out of its frame when neither of its ends is in it", () => { + const rectC = API.createElement({ + type: "rectangle", + x: -300, + y: 100, + width: 100, + height: 100, + backgroundColor: "red", + fillStyle: "solid", + }); + API.setElements([rectC, ...h.elements]); + // from inside frame A + const arrow = drawArrowBetweenFrames(pointFrom(250, 250)); + + // onto rectC, crossing frame A + mouse.downAt(250, 250); + mouse.moveTo(-250, 150); + mouse.up(); + + expect(arrow.startBinding?.elementId).toBe(rectC.id); + expect(arrow.frameId).toBe(null); + }); + + it("should take an arrow out of its frame when what it's bound to in it leaves it", () => { + const arrow = drawArrowBetweenFrames(); + + mouse.downAt(60, 110); + mouse.moveTo(-240, 110); + mouse.up(); + + expect(rectA.frameId).toBe(null); + expect(arrow.startBinding?.elementId).toBe(rectA.id); + expect(arrow.frameId).toBe(null); + }); + + it("should take an arrow out of its frame when resizing the frame leaves its ends out", () => { + const arrow = drawArrowBetweenFrames(); + + UI.resize(frameA, "w", [200, 0]); + + expect(rectA.frameId).toBe(null); + expect(arrow.frameId).toBe(null); + }); + it("should keep an elbow arrow's fixed segment when dragging its frame", () => { API.setAppState({ currentItemArrowType: "elbow" }); // routed around rectB, with its middle segment fixed diff --git a/packages/excalidraw/CHANGELOG.md b/packages/excalidraw/CHANGELOG.md index d9bd24283d..05aa2f97d8 100644 --- a/packages/excalidraw/CHANGELOG.md +++ b/packages/excalidraw/CHANGELOG.md @@ -15,7 +15,7 @@ Please add the latest change on the top under the correct section. ### Arrows aren't clipped to frames (2026-10-31) [#12516](https://github.com/excalidraw/excalidraw/pull/12516) -- Arrows and their labels are no longer clipped to the frame they belong to, so they can connect elements across and out of frames, and their part outside the frame can be selected. They still belong to their frame, so they're moved, copied and deleted with it. +- Arrows and their labels are no longer clipped to the frame they belong to, so they can connect elements across and out of frames, and their part outside the frame can be selected. They still belong to their frame, so they're moved, copied and deleted with it, but only while one of their ends is in it (bound to an element in the frame, or inside it). An arrow whose ends both leave its frame (moved, bound to elements outside it, or with the elements it's bound to) leaves the frame. - Exporting a frame (and `getElementsOverlappingFrame`) now includes other frames' arrows that reach into it, like other elements overlapping it. - Moving a frame (dragging it, with the arrow keys or from the stats panel) keeps its arrows bound to the elements that don't move with it, instead of unbinding them. diff --git a/packages/excalidraw/actions/actionFinalize.tsx b/packages/excalidraw/actions/actionFinalize.tsx index 69d208d40f..50b2e56a74 100644 --- a/packages/excalidraw/actions/actionFinalize.tsx +++ b/packages/excalidraw/actions/actionFinalize.tsx @@ -5,6 +5,7 @@ import { isValidPolygon, LinearElementEditor, newElementWith, + removeArrowsWithoutEndInFrame, } from "@excalidraw/element"; import { @@ -146,6 +147,8 @@ export const actionFinalize = register({ }); } } + // with its ends where they ended up + removeArrowsWithoutEndInFrame([element], elementsMap); if (linearElementEditor !== appState.selectedLinearElement) { // `handlePointerUp()` updated the linear element instance, diff --git a/packages/excalidraw/components/App.selectionTool.ts b/packages/excalidraw/components/App.selectionTool.ts index da95743f71..0fedf01d15 100644 --- a/packages/excalidraw/components/App.selectionTool.ts +++ b/packages/excalidraw/components/App.selectionTool.ts @@ -44,6 +44,7 @@ import { transformElements, uncropElementSides, updateBoundElements, + removeArrowsWithoutEndInFrame, updateFrameMembershipOfSelectedElements, } from "@excalidraw/element"; import { @@ -1217,6 +1218,8 @@ export class AppSelectionTool { this.app.state, this.app, ); + // the dragged arrows, and those bound to the dragged elements + removeArrowsWithoutEndInFrame(selectedElements, nextElements); this.app.scene.replaceAllElements(nextElements); };