Files
webstudio/packages/project/src/shared/tree-utils/insert-instance.ts
T
Bogdan Chadkin 052d049073 Refactor closest droppable with instances index (#945)
Ref https://github.com/webstudio-is/webstudio-designer/issues/696

A lot of our utilities can reuse indexed instances instead of
retraversing tree. This will also simplify migration to normalized
instances.

Here I refactored droppable instance lookup.
2023-02-06 14:16:52 +03:00

43 lines
1001 B
TypeScript

import type { Instance } from "@webstudio-is/project-build";
export type InstanceInsertionSpec = {
parentId: Instance["id"];
position: number | "end";
};
export const insertInstanceMutable = (
rootInstance: Instance,
instance: Instance,
spec: undefined | InstanceInsertionSpec
): boolean => {
if (spec === undefined) {
return false;
}
if (spec.parentId !== rootInstance.id) {
for (const child of rootInstance.children) {
if (child.type === "text") {
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;
};