diff --git a/packages/excalidraw/data/restore.ts b/packages/excalidraw/data/restore.ts index 0f61c5a0e4e6..c3739dd24207 100644 --- a/packages/excalidraw/data/restore.ts +++ b/packages/excalidraw/data/restore.ts @@ -1,6 +1,7 @@ import type { ExcalidrawElement, ExcalidrawElementType, + ExcalidrawLinearElement, ExcalidrawSelectionElement, ExcalidrawTextElement, FontFamilyValues, @@ -21,7 +22,11 @@ import { isInvisiblySmallElement, refreshTextDimensions, } from "../element"; -import { isTextElement, isUsingAdaptiveRadius } from "../element/typeChecks"; +import { + isLinearElement, + isTextElement, + isUsingAdaptiveRadius, +} from "../element/typeChecks"; import { randomId } from "../random"; import { DEFAULT_FONT_FAMILY, @@ -460,6 +465,21 @@ export const restoreElements = ( ), ); } + + if (isLinearElement(element)) { + if ( + element.startBinding && + !restoredElementsMap.has(element.startBinding.elementId) + ) { + (element as Mutable).startBinding = null; + } + if ( + element.endBinding && + !restoredElementsMap.has(element.endBinding.elementId) + ) { + (element as Mutable).endBinding = null; + } + } } return restoredElements; diff --git a/packages/excalidraw/element/binding.ts b/packages/excalidraw/element/binding.ts index 6b140512958a..4b2fe50fb682 100644 --- a/packages/excalidraw/element/binding.ts +++ b/packages/excalidraw/element/binding.ts @@ -188,10 +188,8 @@ const getOriginalBindingIfStillCloseOfLinearElementEdge = ( ? linearElement.startBinding?.elementId : linearElement.endBinding?.elementId; if (elementId) { - const element = elementsMap.get( - elementId, - ) as NonDeleted; - if (bindingBorderTest(element, coors, app)) { + const element = elementsMap.get(elementId); + if (isBindableElement(element) && bindingBorderTest(element, coors, app)) { return element; } } diff --git a/packages/excalidraw/element/typeChecks.ts b/packages/excalidraw/element/typeChecks.ts index e7dea3cef6a1..347f41ce6689 100644 --- a/packages/excalidraw/element/typeChecks.ts +++ b/packages/excalidraw/element/typeChecks.ts @@ -132,7 +132,7 @@ export const isBindingElementType = ( }; export const isBindableElement = ( - element: ExcalidrawElement | null, + element: ExcalidrawElement | null | undefined, includeLocked = true, ): element is ExcalidrawBindableElement => { return (