Files
webstudio/apps/designer/app/shared/tree-utils/insert-instance.ts
T
2022-07-14 11:18:16 +02:00

36 lines
903 B
TypeScript

import { type Instance } from "@webstudio-is/react-sdk";
export type InstanceInsertionSpec = {
parentId: Instance["id"];
position: number | "end";
};
export const insertInstanceMutable = (
rootInstance: Instance,
instance: Instance,
spec: InstanceInsertionSpec
): boolean => {
if (spec.parentId !== rootInstance.id) {
for (const child of rootInstance.children) {
if (typeof child === "string") continue;
const hasInserted = insertInstanceMutable(child, instance, spec);
if (hasInserted === true) return true;
}
return false;
}
// Inserting inside the selected/hovered instance.
if (typeof spec.position === "number") {
rootInstance.children.splice(spec.position, 0, instance);
return true;
}
// Inserting after all children
if (spec.position === "end") {
rootInstance.children.push(instance);
return true;
}
return true;
};