fix: boundElementIds when arrows bound to elements are deleted (#5077)

* fix: boundElementIds when arrows bound to elements are deleted

* fix type checks and updating unrelated elements

Co-authored-by: dwelle <luzar.david@gmail.com>
This commit is contained in:
Aakansha Doshi 2022-04-21 18:57:06 +05:30 committed by GitHub
parent 86cf28f2b4
commit a66cfe2627
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -660,28 +660,47 @@ export const fixBindingsAfterDeletion = (
const deletedElementIds = new Set( const deletedElementIds = new Set(
deletedElements.map((element) => element.id), deletedElements.map((element) => element.id),
); );
// Non deleted and need an update // non-deleted which bindings need to be updated
const boundElementIds: Set<ExcalidrawElement["id"]> = new Set(); const affectedElements: Set<ExcalidrawElement["id"]> = new Set();
deletedElements.forEach((deletedElement) => { deletedElements.forEach((deletedElement) => {
if (isBindableElement(deletedElement)) { if (isBindableElement(deletedElement)) {
deletedElement.boundElements?.forEach((element) => { deletedElement.boundElements?.forEach((element) => {
if (!deletedElementIds.has(element.id)) { if (!deletedElementIds.has(element.id)) {
boundElementIds.add(element.id); affectedElements.add(element.id);
} }
}); });
} else if (isBindingElement(deletedElement)) {
if (deletedElement.startBinding) {
affectedElements.add(deletedElement.startBinding.elementId);
}
if (deletedElement.endBinding) {
affectedElements.add(deletedElement.endBinding.elementId);
}
} }
}); });
( sceneElements
sceneElements.filter(({ id }) => .filter(({ id }) => affectedElements.has(id))
boundElementIds.has(id), .forEach((element) => {
) as ExcalidrawLinearElement[] if (isBindableElement(element)) {
).forEach((element: ExcalidrawLinearElement) => { mutateElement(element, {
const { startBinding, endBinding } = element; boundElements: newBoundElementsAfterDeletion(
mutateElement(element, { element.boundElements,
startBinding: newBindingAfterDeletion(startBinding, deletedElementIds), deletedElementIds,
endBinding: newBindingAfterDeletion(endBinding, deletedElementIds), ),
});
} else if (isBindingElement(element)) {
mutateElement(element, {
startBinding: newBindingAfterDeletion(
element.startBinding,
deletedElementIds,
),
endBinding: newBindingAfterDeletion(
element.endBinding,
deletedElementIds,
),
});
}
}); });
});
}; };
const newBindingAfterDeletion = ( const newBindingAfterDeletion = (
@ -693,3 +712,13 @@ const newBindingAfterDeletion = (
} }
return binding; return binding;
}; };
const newBoundElementsAfterDeletion = (
boundElements: ExcalidrawElement["boundElements"],
deletedElementIds: Set<ExcalidrawElement["id"]>,
) => {
if (!boundElements) {
return null;
}
return boundElements.filter((ele) => !deletedElementIds.has(ele.id));
};