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:
parent
86cf28f2b4
commit
a66cfe2627
@ -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));
|
||||||
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user