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.

select next best instance after deletion #12

Oleg Isonen09600bcbb405
diff --git a/app/canvas/shared/tree.ts b/app/canvas/shared/tree.ts
index d855bc0c7c..8d5b415b92 100644
--- a/app/canvas/shared/tree.ts
+++ b/app/canvas/shared/tree.ts
@@ -14,6 +14,8 @@ import {
} from "./nano-values";
import { useSubscribe } from "./pubsub";
import { createTransaction } from "~/lib/sync-engine";
+import { findParentInstance } from "~/shared/tree-utils/find-parent-instance";
+import { findClosestSiblingInstance } from "~/shared/tree-utils/find-closest-sibling-instance";
export const usePopulateRootInstance = (tree: Tree) => {
const [, setRootInstance] = useRootInstance();
@@ -83,10 +85,21 @@ export const useReparentInstance = ({
};
export const useDeleteInstance = () => {
- const [, setSelectedInstance] = useSelectedInstance();
+ const [rootInstance] = useRootInstance();
+ const [selectedInstance, setSelectedInstance] = useSelectedInstance();
useSubscribe<"deleteInstance", { id: Instance["id"] }>(
"deleteInstance",
({ id }) => {
+ if (rootInstance !== undefined && selectedInstance !== undefined) {
+ const parentInstance = findParentInstance(rootInstance, id);
+ if (parentInstance !== undefined) {
+ const siblingInstance = findClosestSiblingInstance(
+ parentInstance,
+ id
+ );
+ setSelectedInstance(siblingInstance || parentInstance);
+ }
+ }
// @todo deleting instance should involve also deleting it's props
// If we don't delete them - they just live both on client and db
// Pros:
@@ -101,7 +114,6 @@ export const useDeleteInstance = () => {
deleteInstanceMutable(rootInstance, id);
}
});
- setSelectedInstance(undefined);
}
);
};
diff --git a/app/shared/tree-utils/find-closest-sibling-instance.test.ts b/app/shared/tree-utils/find-closest-sibling-instance.test.ts
new file mode 100644
index 0000000000..25e9ff7385
--- /dev/null
+++ b/app/shared/tree-utils/find-closest-sibling-instance.test.ts
@@ -0,0 +1,42 @@
+import { type Instance } from "@webstudio-is/sdk";
+import { findClosestSiblingInstance } from "./find-closest-sibling-instance";
+
+describe("Find closest sibling instance", () => {
+ test("find", () => {
+ const rootInstance: Instance = {
+ component: "Box",
+ id: "1",
+ style: {},
+ children: [
+ {
+ component: "Box",
+ id: "3",
+ style: {},
+ children: [],
+ },
+ "a",
+ {
+ component: "Box",
+ id: "4",
+ style: {},
+ children: [],
+ },
+ "b",
+ {
+ component: "Box",
+ id: "5",
+ style: {},
+ children: [],
+ },
+ ],
+ };
+
+ expect(
+ findClosestSiblingInstance(rootInstance, "notFound")
+ ).toBeUndefined();
+ expect(findClosestSiblingInstance(rootInstance, "1")).toBeUndefined();
+ expect(findClosestSiblingInstance(rootInstance, "3")?.id).toBe("4");
+ expect(findClosestSiblingInstance(rootInstance, "4")?.id).toBe("5");
+ expect(findClosestSiblingInstance(rootInstance, "5")?.id).toBe("4");
+ });
+});
diff --git a/app/shared/tree-utils/find-closest-sibling-instance.ts b/app/shared/tree-utils/find-closest-sibling-instance.ts
new file mode 100644
index 0000000000..61238ae385
--- /dev/null
+++ b/app/shared/tree-utils/find-closest-sibling-instance.ts
@@ -0,0 +1,17 @@
+import { type Instance } from "@webstudio-is/sdk";
+
+export const findClosestSiblingInstance = (
+ instance: Instance,
+ instanceId: Instance["id"]
+): Instance | undefined => {
+ if (instance.children.length === 0) return;
+ const children = instance.children.filter(
+ (instance) => typeof instance === "object"
+ ) as Array<Instance>;
+ const index = children.findIndex((instance) => instance.id === instanceId);
+ if (index === -1) return;
+ const nextInstance = children[index + 1];
+ if (nextInstance !== undefined) return nextInstance;
+ const previousInstance = children[index - 1];
+ return previousInstance;
+};
diff --git a/app/shared/tree-utils/find-parent-instance.test.ts b/app/shared/tree-utils/find-parent-instance.test.ts
new file mode 100644
index 0000000000..4391227455
--- /dev/null
+++ b/app/shared/tree-utils/find-parent-instance.test.ts
@@ -0,0 +1,31 @@
+import { type Instance } from "@webstudio-is/sdk";
+import { findParentInstance } from "./find-parent-instance";
+
+describe("Find parent instance", () => {
+ test("find", () => {
+ const rootInstance: Instance = {
+ component: "Box",
+ id: "1",
+ style: {},
+ children: [
+ {
+ component: "Box",
+ id: "2",
+ style: {},
+ children: [
+ {
+ component: "Box",
+ id: "3",
+ style: {},
+ children: [],
+ },
+ ],
+ },
+ ],
+ };
+
+ expect(findParentInstance(rootInstance, "3")?.id).toBe("2");
+ expect(findParentInstance(rootInstance, "2")?.id).toBe("1");
+ expect(findParentInstance(rootInstance, "1")).toBe(undefined);
+ });
+});
diff --git a/app/shared/tree-utils/find-parent-instance.ts b/app/shared/tree-utils/find-parent-instance.ts
new file mode 100644
index 0000000000..47c857f6ed
--- /dev/null
+++ b/app/shared/tree-utils/find-parent-instance.ts
@@ -0,0 +1,19 @@
+import { type Instance } from "@webstudio-is/sdk";
+
+export const findParentInstance = (
+ instance: Instance,
+ instanceId: Instance["id"]
+): Instance | undefined => {
+ const find = (
+ childInstance: Instance,
+ parentInstance?: Instance
+ ): Instance | undefined => {
+ if (childInstance.id === instanceId) return parentInstance;
+ for (const child of childInstance.children) {
+ if (typeof child === "string") continue;
+ const foundInstance = find(child, childInstance);
+ if (foundInstance !== undefined) return foundInstance;
+ }
+ };
+ return find(instance);
+};
diff --git a/app/shared/tree-utils/get-instance-path.test.ts b/app/shared/tree-utils/get-instance-path.test.ts
index 3283e6fdbe..4561625582 100644
--- a/app/shared/tree-utils/get-instance-path.test.ts
+++ b/app/shared/tree-utils/get-instance-path.test.ts
@@ -3,7 +3,7 @@ import { getInstancePath } from "./get-instance-path";
const getIds = (array: Array<{ id: string }>) => array.map((item) => item.id);
-describe.only("Get instance path", () => {
+describe("Get instance path", () => {
test("single possible path", () => {
const instance: Instance = {
component: "Box",