Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

Migrate delete normalized instances (#1163)

Bogdan Chadkin22c13b89723b
diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.ts
index efcb947f46..75dfcb20cc 100644
--- a/apps/builder/app/shared/instance-utils.ts
+++ b/apps/builder/app/shared/instance-utils.ts
@@ -9,12 +9,14 @@ import {
styleSourcesStore,
instancesStore,
patchInstancesMutable,
+ selectedPageStore,
} from "./nano-states";
import {
createInstancesIndex,
DroppableTarget,
- findSubtree,
+ findParentInstance,
findSubtreeLocalStyleSources,
+ findTreeInstances,
insertInstanceMutable,
reparentInstanceMutable,
} from "./tree-utils";
@@ -47,7 +49,12 @@ export const reparentInstance = (
};
export const deleteInstance = (targetInstanceId: Instance["id"]) => {
- const rootInstance = rootInstanceContainer.get();
+ const rootInstanceId = selectedPageStore.get()?.rootInstanceId;
+ // @todo tell user they can't delete root
+ if (targetInstanceId === rootInstanceId) {
+ return;
+ }
+
store.createTransaction(
[
instancesStore,
@@ -57,17 +64,8 @@ export const deleteInstance = (targetInstanceId: Instance["id"]) => {
stylesStore,
],
(instances, props, styleSourceSelections, styleSources, styles) => {
- if (rootInstance === undefined) {
- return;
- }
- // @todo tell user they can't delete root
- if (targetInstanceId === rootInstance?.id) {
- return;
- }
- const { parentInstance, subtreeIds } = findSubtree(
- rootInstance,
- targetInstanceId
- );
+ const parentInstance = findParentInstance(instances, targetInstanceId);
+ const subtreeIds = findTreeInstances(instances, targetInstanceId);
const subtreeLocalStyleSourceIds = findSubtreeLocalStyleSources(
subtreeIds,
styleSources,
@@ -79,8 +77,11 @@ export const deleteInstance = (targetInstanceId: Instance["id"]) => {
removeByMutable(
parentInstance.children,
- (child) => child.type === "instance" && child.id === targetInstanceId
+ (child) => child.type === "id" && child.value === targetInstanceId
);
+ for (const instanceId of subtreeIds) {
+ instances.delete(instanceId);
+ }
// delete props and styles of deleted instance and its descendants
for (const prop of props.values()) {
if (subtreeIds.has(prop.instanceId)) {
@@ -100,7 +101,6 @@ export const deleteInstance = (targetInstanceId: Instance["id"]) => {
}
selectedInstanceIdStore.set(parentInstance.id);
- patchInstancesMutable(rootInstance, instances);
}
);
};
diff --git a/apps/builder/app/shared/nano-states/nano-states.ts b/apps/builder/app/shared/nano-states/nano-states.ts
index 008b4ca828..133a6321c7 100644
--- a/apps/builder/app/shared/nano-states/nano-states.ts
+++ b/apps/builder/app/shared/nano-states/nano-states.ts
@@ -69,9 +69,7 @@ export const patchInstancesMutable = (
instances: Instances
) => {
const oldInstancesIndex = createInstancesIndex(rootInstance);
- const deletedInstanceIds = new Set<Instance["id"]>(instances.keys());
for (const oldInstance of oldInstancesIndex.instancesById.values()) {
- deletedInstanceIds.delete(oldInstance.id);
const instance = instances.get(oldInstance.id);
const convertedOldInstance: InstancesItem = {
type: "instance",
@@ -92,9 +90,6 @@ export const patchInstancesMutable = (
}
instances.set(oldInstance.id, convertedOldInstance);
}
- for (const deletedId of deletedInstanceIds) {
- instances.delete(deletedId);
- }
};
// @todo will be removed soon
diff --git a/apps/builder/app/shared/tree-utils.test.ts b/apps/builder/app/shared/tree-utils.test.ts
index 57038e3200..b9c29597d0 100644
--- a/apps/builder/app/shared/tree-utils.test.ts
+++ b/apps/builder/app/shared/tree-utils.test.ts
@@ -1,6 +1,8 @@
import { test, expect } from "@jest/globals";
import type {
Instance,
+ Instances,
+ InstancesItem,
Prop,
StyleDecl,
Styles,
@@ -16,8 +18,10 @@ import {
createInstancesIndex,
findClosestDroppableTarget,
findClosestRichTextInstance,
+ findParentInstance,
findSubtree,
findSubtreeLocalStyleSources,
+ findTreeInstances,
getInstanceAncestorsAndSelf,
insertInstanceMutable,
reparentInstanceMutable,
@@ -38,6 +42,22 @@ const createInstance = (
};
};
+const createInstancePair = (
+ id: Instance["id"],
+ component: string,
+ children: InstancesItem["children"]
+): [Instance["id"], InstancesItem] => {
+ return [
+ id,
+ {
+ type: "instance",
+ id,
+ component,
+ children,
+ },
+ ];
+};
+
const createProp = (id: string, instanceId: string): Prop => {
return {
id,
@@ -350,6 +370,50 @@ test("find subtree with all descendants and parent instance", () => {
expect(findSubtree(rootInstance, "root").parentInstance).toEqual(undefined);
});
+test("find parent instance", () => {
+ const instances: Instances = new Map([
+ createInstancePair("1", "Body", [{ type: "id", value: "3" }]),
+ // this is outside of subtree
+ createInstancePair("2", "Box", []),
+ // these should be matched
+ createInstancePair("3", "Box", [
+ { type: "id", value: "4" },
+ { type: "id", value: "5" },
+ ]),
+ createInstancePair("4", "Box", []),
+ createInstancePair("5", "Box", []),
+ // this one is from other tree
+ createInstancePair("6", "Box", []),
+ ]);
+ expect(findParentInstance(instances, "4")).toEqual({
+ type: "instance",
+ id: "3",
+ component: "Box",
+ children: [
+ { type: "id", value: "4" },
+ { type: "id", value: "5" },
+ ],
+ });
+});
+
+test("find all tree instances", () => {
+ const instances: Instances = new Map([
+ createInstancePair("1", "Body", [{ type: "id", value: "3" }]),
+ // this is outside of subtree
+ createInstancePair("2", "Box", []),
+ // these should be matched
+ createInstancePair("3", "Box", [
+ { type: "id", value: "4" },
+ { type: "id", value: "5" },
+ ]),
+ createInstancePair("4", "Box", []),
+ createInstancePair("5", "Box", []),
+ // this one is from other tree
+ createInstancePair("6", "Box", []),
+ ]);
+ expect(findTreeInstances(instances, "3")).toEqual(new Set(["3", "4", "5"]));
+});
+
test("clone instance tree and provide cloned ids map", () => {
const instance = createInstance("box", "Box", [
createInstance("child1", "Box", []),
diff --git a/apps/builder/app/shared/tree-utils.ts b/apps/builder/app/shared/tree-utils.ts
index 50baee186c..9257751f5c 100644
--- a/apps/builder/app/shared/tree-utils.ts
+++ b/apps/builder/app/shared/tree-utils.ts
@@ -2,6 +2,8 @@ import { nanoid } from "nanoid";
import produce from "immer";
import type {
Instance,
+ Instances,
+ InstancesItem,
Prop,
Props,
StyleDecl,
@@ -217,6 +219,47 @@ export const findSubtree = (
};
};
+export const findParentInstance = (
+ instances: Instances,
+ instanceId: Instance["id"]
+) => {
+ for (const instance of instances.values()) {
+ for (const child of instance.children) {
+ if (child.type === "id" && child.value === instanceId) {
+ return instance;
+ }
+ }
+ }
+};
+
+const traverseInstancesMap = (
+ instances: Map<Instance["id"], InstancesItem>,
+ instanceId: Instance["id"],
+ callback: (instance: InstancesItem) => void
+) => {
+ const instance = instances.get(instanceId);
+ if (instance === undefined) {
+ return;
+ }
+ callback(instance);
+ for (const child of instance.children) {
+ if (child.type === "id") {
+ traverseInstancesMap(instances, child.value, callback);
+ }
+ }
+};
+
+export const findTreeInstances = (
+ instances: Instances,
+ rootInstanceId: Instance["id"]
+) => {
+ const subtreeIds = new Set<Instance["id"]>();
+ traverseInstancesMap(instances, rootInstanceId, (instance) => {
+ subtreeIds.add(instance.id);
+ });
+ return subtreeIds;
+};
+
export const cloneInstance = (targetInstance: Instance) => {
const clonedInstanceIds = new Map<Instance["id"], Instance["id"]>();
const clonedInstance = produce((targetInstance) => {