feat: remove bound-arrows from frames (#7157)

This commit is contained in:
David Luzar 2023-10-17 18:18:20 +02:00 committed by GitHub
parent 5b94cffc74
commit dde3dac931
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 81 additions and 16 deletions

View file

@ -43,6 +43,7 @@ import {
measureBaseline,
} from "../element/textElement";
import { normalizeLink } from "./url";
import { isValidFrameChild } from "../frame";
type RestoredAppState = Omit<
AppState,
@ -396,7 +397,7 @@ const repairBoundElement = (
};
/**
* Remove an element's frameId if its containing frame is non-existent
* resets `frameId` if no longer applicable.
*
* NOTE mutates elements.
*/
@ -404,12 +405,16 @@ const repairFrameMembership = (
element: Mutable<ExcalidrawElement>,
elementsMap: Map<string, Mutable<ExcalidrawElement>>,
) => {
if (element.frameId) {
const containingFrame = elementsMap.get(element.frameId);
if (!element.frameId) {
return;
}
if (!containingFrame) {
element.frameId = null;
}
if (
!isValidFrameChild(element) ||
// target frame not exists
!elementsMap.get(element.frameId)
) {
element.frameId = null;
}
};
@ -453,6 +458,8 @@ export const restoreElements = (
// repair binding. Mutates elements.
const restoredElementsMap = arrayToMap(restoredElements);
for (const element of restoredElements) {
// repair frame membership *after* bindings we do in restoreElement()
// since we rely on bindings to be correct
if (element.frameId) {
repairFrameMembership(element, restoredElementsMap);
}