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.

Refactor insert and reparent operations with selectors (#1283)

Bogdan Chadkinbac73301c880
diff --git a/apps/builder/app/builder/features/sidebar-left/panels/components/components.tsx b/apps/builder/app/builder/features/sidebar-left/panels/components/components.tsx
index 885eb75563..3a75cfca0d 100644
--- a/apps/builder/app/builder/features/sidebar-left/panels/components/components.tsx
+++ b/apps/builder/app/builder/features/sidebar-left/panels/components/components.tsx
@@ -17,8 +17,9 @@ import {
import { PlusIcon } from "@webstudio-is/icons";
import { findClosestDroppableTarget } from "~/shared/tree-utils";
import {
- instancesIndexStore,
+ instancesStore,
selectedInstanceSelectorStore,
+ selectedPageStore,
} from "~/shared/nano-states";
import { useSubscribe, type Publish } from "~/shared/pubsub";
import {
@@ -177,10 +178,16 @@ export const TabContent = ({ publish, onSetActiveTab }: TabContentProps) => {
<ComponentCard
onClick={() => {
onSetActiveTab("none");
+ const selectedPage = selectedPageStore.get();
+ if (selectedPage === undefined) {
+ return;
+ }
const dropTarget = findClosestDroppableTarget(
- instancesIndexStore.get(),
- // @todo accept instance Selector
- selectedInstanceSelectorStore.get()?.[0]
+ instancesStore.get(),
+ // fallback to root as drop target
+ selectedInstanceSelectorStore.get() ?? [
+ selectedPage.rootInstanceId,
+ ]
);
insertNewComponentInstance(component, dropTarget);
}}
diff --git a/apps/builder/app/builder/shared/navigator-tree.tsx b/apps/builder/app/builder/shared/navigator-tree.tsx
index 951dd06f61..7a87394b5a 100644
--- a/apps/builder/app/builder/shared/navigator-tree.tsx
+++ b/apps/builder/app/builder/shared/navigator-tree.tsx
@@ -25,8 +25,8 @@ export const NavigatorTree = () => {
itemSelector: InstanceSelector;
dropTarget: { itemSelector: InstanceSelector; position: number | "end" };
}) => {
- reparentInstance(payload.itemSelector[0], {
- parentId: payload.dropTarget.itemSelector[0],
+ reparentInstance(payload.itemSelector, {
+ parentSelector: payload.dropTarget.itemSelector,
position: payload.dropTarget.position,
});
},
diff --git a/apps/builder/app/canvas/shared/use-drag-drop.ts b/apps/builder/app/canvas/shared/use-drag-drop.ts
index 412fbbb73c..47834e3751 100644
--- a/apps/builder/app/canvas/shared/use-drag-drop.ts
+++ b/apps/builder/app/canvas/shared/use-drag-drop.ts
@@ -304,13 +304,13 @@ export const useDragAndDrop = () => {
if (dropTarget && dragPayload && isCanceled === false) {
if (dragPayload.type === "insert") {
insertNewComponentInstance(dragPayload.dragComponent, {
- parentId: dropTarget.itemSelector[0],
+ parentSelector: dropTarget.itemSelector,
position: dropTarget.indexWithinChildren,
});
}
if (dragPayload.type === "reparent") {
- reparentInstance(dragPayload.dragInstanceSelector[0], {
- parentId: dropTarget.itemSelector[0],
+ reparentInstance(dragPayload.dragInstanceSelector, {
+ parentSelector: dropTarget.itemSelector,
position: dropTarget.indexWithinChildren,
});
}
diff --git a/apps/builder/app/shared/copy-paste/plugin-instance.ts b/apps/builder/app/shared/copy-paste/plugin-instance.ts
index 04f9b22732..d3583b8b15 100644
--- a/apps/builder/app/shared/copy-paste/plugin-instance.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-instance.ts
@@ -14,11 +14,11 @@ import {
selectedInstanceSelectorStore,
styleSourceSelectionsStore,
styleSourcesStore,
- instancesIndexStore,
instancesStore,
selectedPageStore,
} from "../nano-states";
import {
+ type InstanceSelector,
findClosestDroppableTarget,
findSubtreeLocalStyleSources,
insertInstancesCopyMutable,
@@ -41,13 +41,13 @@ const InstanceData = z.object({
type InstanceData = z.infer<typeof InstanceData>;
-const getTreeData = (targetInstanceId: string) => {
- const rootInstanceId = selectedPageStore.get()?.rootInstanceId;
+const getTreeData = (targetInstanceSelector: InstanceSelector) => {
// @todo tell user they can't copy or cut root
- if (targetInstanceId === rootInstanceId) {
+ if (targetInstanceSelector.length === 1) {
return;
}
+ const [targetInstanceId] = targetInstanceSelector;
const instances = instancesStore.get();
const treeInstanceIds = findTreeInstanceIds(instances, targetInstanceId);
const styleSources = styleSourcesStore.get();
@@ -134,11 +134,9 @@ export const onPaste = (clipboardData: string) => {
const instanceSelector = selectedInstanceSelectorStore.get() ?? [
selectedPage.rootInstanceId,
];
- const [targetInstanceId] = instanceSelector;
const dropTarget = findClosestDroppableTarget(
- instancesIndexStore.get(),
- // @todo accept instance selector
- targetInstanceId
+ instancesStore.get(),
+ instanceSelector
);
store.createTransaction(
[
@@ -182,8 +180,7 @@ export const onCopy = () => {
if (selectedInstanceSelector === undefined) {
return;
}
- const [selectedInstanceId] = selectedInstanceSelector;
- const data = getTreeData(selectedInstanceId);
+ const data = getTreeData(selectedInstanceSelector);
if (data === undefined) {
return;
}
@@ -195,13 +192,11 @@ export const onCut = () => {
if (selectedInstanceSelector === undefined) {
return;
}
- const [selectedInstanceId] = selectedInstanceSelector;
- const rootInstanceId = selectedPageStore.get()?.rootInstanceId;
// @todo tell user they can't delete root
- if (selectedInstanceId === rootInstanceId) {
+ if (selectedInstanceSelector.length === 1) {
return;
}
- const data = getTreeData(selectedInstanceId);
+ const data = getTreeData(selectedInstanceSelector);
if (data === undefined) {
return;
}
diff --git a/apps/builder/app/shared/copy-paste/plugin-markdown.ts b/apps/builder/app/shared/copy-paste/plugin-markdown.ts
index d94cbc5722..7fc1b0578f 100644
--- a/apps/builder/app/shared/copy-paste/plugin-markdown.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-markdown.ts
@@ -13,7 +13,6 @@ import {
insertInstancesMutable,
} from "../tree-utils";
import {
- instancesIndexStore,
instancesStore,
propsStore,
selectedInstanceSelectorStore,
@@ -216,11 +215,9 @@ export const onPaste = (clipboardData: string) => {
const instanceSelector = selectedInstanceSelectorStore.get() ?? [
selectedPage.rootInstanceId,
];
- const [targetInstanceId] = instanceSelector;
const dropTarget = findClosestDroppableTarget(
- instancesIndexStore.get(),
- // @todo accept instance selector
- targetInstanceId
+ instancesStore.get(),
+ instanceSelector
);
store.createTransaction([instancesStore, propsStore], (instances, props) => {
insertInstancesMutable(instances, data.instances, data.rootIds, dropTarget);
diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.ts
index 58b2b915c9..d1760c4569 100644
--- a/apps/builder/app/shared/instance-utils.ts
+++ b/apps/builder/app/shared/instance-utils.ts
@@ -1,5 +1,5 @@
import store from "immerhin";
-import { findTreeInstanceIds, Instance } from "@webstudio-is/project-build";
+import { findTreeInstanceIds } from "@webstudio-is/project-build";
import {
propsStore,
stylesStore,
@@ -7,14 +7,12 @@ import {
styleSourceSelectionsStore,
styleSourcesStore,
instancesStore,
- selectedPageStore,
} from "./nano-states";
import {
type DroppableTarget,
type InstanceSelector,
createComponentInstance,
findSubtreeLocalStyleSources,
- getInstanceSelector,
insertInstancesMutable,
reparentInstanceMutable,
getAncestorInstanceSelector,
@@ -23,31 +21,26 @@ import { removeByMutable } from "./array-utils";
export const insertNewComponentInstance = (
component: string,
- dropTarget?: DroppableTarget
+ dropTarget: DroppableTarget
) => {
const instance = createComponentInstance(component);
store.createTransaction([instancesStore], (instances) => {
insertInstancesMutable(instances, [instance], [instance.id], dropTarget);
});
- selectedInstanceSelectorStore.set(
- getInstanceSelector(instancesStore.get(), instance.id)
- );
+ selectedInstanceSelectorStore.set([
+ instance.id,
+ ...dropTarget.parentSelector,
+ ]);
};
export const reparentInstance = (
- targetInstanceId: Instance["id"],
+ targetInstanceSelector: InstanceSelector,
dropTarget: DroppableTarget
) => {
store.createTransaction([instancesStore], (instances) => {
- reparentInstanceMutable(
- instances,
- getInstanceSelector(instances, targetInstanceId),
- dropTarget
- );
+ reparentInstanceMutable(instances, targetInstanceSelector, dropTarget);
});
- selectedInstanceSelectorStore.set(
- getInstanceSelector(instancesStore.get(), targetInstanceId)
- );
+ selectedInstanceSelectorStore.set(targetInstanceSelector);
};
export const deleteInstance = (instanceSelector: InstanceSelector) => {
@@ -112,11 +105,10 @@ export const deleteInstance = (instanceSelector: InstanceSelector) => {
export const deleteSelectedInstance = () => {
const selectedInstanceSelector = selectedInstanceSelectorStore.get();
- const rootInstanceId = selectedPageStore.get()?.rootInstanceId;
// @todo tell user they can't delete root
if (
selectedInstanceSelector === undefined ||
- selectedInstanceSelector[0] === rootInstanceId
+ selectedInstanceSelector.length === 1
) {
return;
}
diff --git a/apps/builder/app/shared/tree-utils.test.ts b/apps/builder/app/shared/tree-utils.test.ts
index 56502c8c7a..6b591b5d96 100644
--- a/apps/builder/app/shared/tree-utils.test.ts
+++ b/apps/builder/app/shared/tree-utils.test.ts
@@ -152,40 +152,60 @@ test("get instance selector", () => {
});
test("find closest droppable target", () => {
- const rootInstance = createInstance("root", "Body", [
- createInstance("box1", "Box", [
- createInstance("box11", "Box", []),
- createInstance("box12", "Box", []),
- createInstance("box13", "Box", []),
+ const instances = new Map([
+ createInstancePair("root", "Body", [
+ { type: "id", value: "box1" },
+ { type: "id", value: "box2" },
+ { type: "id", value: "box3" },
]),
- createInstance("box2", "Box", [
- createInstance("paragraph21", "Paragraph", [
- createInstance("bold", "Bold", []),
- ]),
- createInstance("box22", "Box", []),
+ createInstancePair("box1", "Box", [
+ { type: "id", value: "box11" },
+ { type: "id", value: "box12" },
+ { type: "id", value: "box13" },
]),
- createInstance("box3", "Box", [
- createInstance("box31", "Box", []),
- createInstance("box32", "Box", []),
- createInstance("box33", "Box", []),
+ createInstancePair("box11", "Box", []),
+ createInstancePair("box12", "Box", []),
+ createInstancePair("box13", "Box", []),
+ createInstancePair("box2", "Box", [
+ { type: "id", value: "paragraph21" },
+ { type: "id", value: "box22" },
+ ]),
+ createInstancePair("paragraph21", "Paragraph", [
+ { type: "id", value: "bold" },
+ ]),
+ createInstancePair("bold", "Bold", []),
+ createInstancePair("box22", "Box", []),
+ createInstancePair("box3", "Box", [
+ { type: "id", value: "box31" },
+ { type: "id", value: "box32" },
+ { type: "id", value: "box33" },
]),
+ createInstancePair("box31", "Box", []),
+ createInstancePair("box32", "Box", []),
+ createInstancePair("box33", "Box", []),
]);
- const instancesIndex = createInstancesIndex(rootInstance);
- expect(findClosestDroppableTarget(instancesIndex, "bold")).toEqual({
- parentId: "box2",
+ expect(
+ findClosestDroppableTarget(instances, [
+ "bold",
+ "paragraph21",
+ "box2",
+ "root",
+ ])
+ ).toEqual({
+ parentSelector: ["box2", "root"],
position: 1,
});
- expect(findClosestDroppableTarget(instancesIndex, "box3")).toEqual({
- parentId: "box3",
- position: 3,
+ expect(findClosestDroppableTarget(instances, ["box3", "root"])).toEqual({
+ parentSelector: ["box3", "root"],
+ position: "end",
});
- expect(findClosestDroppableTarget(instancesIndex, "root")).toEqual({
- parentId: "root",
- position: 3,
+ expect(findClosestDroppableTarget(instances, ["root"])).toEqual({
+ parentSelector: ["root"],
+ position: "end",
});
- expect(findClosestDroppableTarget(instancesIndex, undefined)).toEqual({
- parentId: "root",
- position: 3,
+ expect(findClosestDroppableTarget(instances, ["box4", "root"])).toEqual({
+ parentSelector: ["root"],
+ position: "end",
});
});
@@ -212,7 +232,7 @@ test("insert instances tree into target", () => {
],
["inserted1"],
{
- parentId: "box1",
+ parentSelector: ["box1", "root"],
position: 1,
}
);
@@ -243,7 +263,7 @@ test("insert instances tree into target", () => {
],
["inserted3"],
{
- parentId: "box1",
+ parentSelector: ["box1", "root"],
position: "end",
}
);
@@ -290,7 +310,7 @@ test("reparent instance into target", () => {
]);
reparentInstanceMutable(instances, ["target", "root"], {
- parentId: "box1",
+ parentSelector: ["box1", "root"],
position: 1,
});
expect(instances).toEqual(
@@ -314,7 +334,7 @@ test("reparent instance into target", () => {
);
reparentInstanceMutable(instances, ["target", "box1", "root"], {
- parentId: "box1",
+ parentSelector: ["box1", "root"],
position: 3,
});
expect(instances).toEqual(
@@ -338,7 +358,7 @@ test("reparent instance into target", () => {
);
reparentInstanceMutable(instances, ["target", "box1", "root"], {
- parentId: "root",
+ parentSelector: ["root"],
position: "end",
});
expect(instances).toEqual(
@@ -413,7 +433,7 @@ test("insert tree of instances copy and provide map from ids map", () => {
instances,
copiedInstances,
{
- parentId: "3",
+ parentSelector: ["3", "1"],
position: 0,
}
);
diff --git a/apps/builder/app/shared/tree-utils.ts b/apps/builder/app/shared/tree-utils.ts
index 03947ffd55..77441e9b7c 100644
--- a/apps/builder/app/shared/tree-utils.ts
+++ b/apps/builder/app/shared/tree-utils.ts
@@ -122,52 +122,55 @@ export const createInstancesIndex = (
};
};
-const isInstanceDroppable = (instance: Instance) => {
+const isInstanceDroppable = (instance: InstancesItem) => {
const meta = getComponentMeta(instance.component);
return meta?.type === "body" || meta?.type === "container";
};
export type DroppableTarget = {
- parentId: Instance["id"];
+ parentSelector: InstanceSelector;
position: number | "end";
};
export const findClosestDroppableTarget = (
- instancesIndex: InstancesIndex,
- instanceId: undefined | Instance["id"]
-): undefined | DroppableTarget => {
- const { instancesById, parentInstancesById } = instancesIndex;
-
- if (instancesIndex.rootInstanceId === undefined) {
- return;
- }
- // fallback to root instance
- let droppableInstance = instancesById.get(
- instanceId ?? instancesIndex.rootInstanceId
- );
- if (droppableInstance === undefined) {
- return;
- }
+ instances: Instances,
+ instanceSelector: InstanceSelector
+): DroppableTarget => {
+ // fallback to root as drop target when selector is stale
+ const rootDropTarget: DroppableTarget = {
+ parentSelector: [instanceSelector[instanceSelector.length - 1]],
+ position: "end",
+ };
let position = -1;
- while (isInstanceDroppable(droppableInstance) === false) {
- const parentInstance = parentInstancesById.get(droppableInstance.id);
- if (parentInstance === undefined) {
- break;
+ let lastChild: undefined | InstancesItem = undefined;
+ for (const instanceId of instanceSelector) {
+ const instance = instances.get(instanceId);
+ if (instance === undefined) {
+ return rootDropTarget;
+ }
+ // find the index of child from selector
+ if (lastChild) {
+ const lastChildId = lastChild.id;
+ position = instance.children.findIndex(
+ (child) => child.type === "id" && child.value === lastChildId
+ );
+ }
+ lastChild = instance;
+ if (isInstanceDroppable(instance)) {
+ const parentSelector = getAncestorInstanceSelector(
+ instanceSelector,
+ instance.id
+ );
+ if (parentSelector === undefined) {
+ return rootDropTarget;
+ }
+ return {
+ parentSelector: parentSelector,
+ position: position === -1 ? "end" : position + 1,
+ };
}
- // source of lookup in children
- const sourceInstanceId = droppableInstance.id;
- droppableInstance = parentInstance;
- position = droppableInstance.children.findIndex(
- (child) => child.type === "instance" && child.id === sourceInstanceId
- );
}
-
- return {
- parentId: droppableInstance.id,
- // put in the end when no position provided
- position:
- position === -1 ? droppableInstance.children.length : position + 1,
- };
+ return rootDropTarget;
};
export const reparentInstanceMutable = (
@@ -180,7 +183,7 @@ export const reparentInstanceMutable = (
parentInstanceId === undefined
? undefined
: instances.get(parentInstanceId);
- const nextParent = instances.get(dropTarget.parentId);
+ const nextParent = instances.get(dropTarget.parentSelector[0]);
const instance = instances.get(instanceId);
if (
prevParent === undefined ||
@@ -281,12 +284,9 @@ export const insertInstancesMutable = (
instances: Instances,
insertedInstances: InstancesItem[],
rootIds: Instance["id"][],
- dropTarget: undefined | DroppableTarget
+ dropTarget: DroppableTarget
) => {
- if (dropTarget === undefined) {
- return;
- }
- const parentInstance = instances.get(dropTarget.parentId);
+ const parentInstance = instances.get(dropTarget.parentSelector[0]);
if (parentInstance === undefined) {
return;
}
@@ -319,7 +319,7 @@ export const insertInstancesMutable = (
export const insertInstancesCopyMutable = (
instances: Instances,
copiedInstances: InstancesItem[],
- dropTarget: undefined | DroppableTarget
+ dropTarget: DroppableTarget
) => {
const copiedInstanceIds = new Map<Instance["id"], Instance["id"]>();
const copiedInstancesWithNewIds: InstancesItem[] = [];