Files
webstudio/app/shared/tree-utils/delete-instance.ts
T
2022-03-13 04:22:21 +00:00

27 lines
730 B
TypeScript

import produce from "immer";
import { type Instance } from "@webstudio-is/sdk";
export const deleteInstanceMutable = (
instance: Instance,
instanceId: Instance["id"]
): boolean => {
if (instance.id === instanceId) return true;
for (const child of instance.children) {
if (typeof child === "string") continue;
const shouldDelete = deleteInstanceMutable(child, instanceId);
if (shouldDelete === true) {
const index = instance.children.indexOf(child);
instance.children.splice(index, 1);
return false;
}
}
return false;
};
export const deleteInstance = produce(
(draftInstance: Instance, instanceId: Instance["id"]) => {
deleteInstanceMutable(draftInstance, instanceId);
}
);