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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
insert _after_ the selected instance
Roman Pominovc4fd94544d8a
diff --git a/apps/designer/app/canvas/shared/instance.ts b/apps/designer/app/canvas/shared/instance.tsindex b6cfedbf46..aa2a1d3993 100644--- a/apps/designer/app/canvas/shared/instance.ts+++ b/apps/designer/app/canvas/shared/instance.ts@@ -19,7 +19,8 @@ import {insertInstanceMutable,findInstanceById,reparentInstanceMutable,- getInstancePath,+ getInstancePathWithPositions,+ type InstanceInsertionSpec,} from "~/shared/tree-utils";import store from "immerhin";import {@@ -50,20 +51,30 @@ export const usePopulateRootInstance = (tree: Tree) => {}};-const getClosestValidParent = (+const findInsertLocation = (rootInstance: Instance,- instanceId: Instance["id"] | undefined-) => {- if (instanceId === undefined) {- return rootInstance;+ selectedInstanceId: Instance["id"] | undefined+): InstanceInsertionSpec => {+ if (selectedInstanceId === undefined) {+ return { parentId: rootInstance.id, position: "end" };}- const path = getInstancePath(rootInstance, instanceId);+ const path = getInstancePathWithPositions(rootInstance, selectedInstanceId);path.reverse();- return (- path.find((instance) => components[instance.component].canAcceptChild()) ??- rootInstance++ const parentIndex = path.findIndex(({ instance }) =>+ components[instance.component].canAcceptChild());++ // Just in case selected Instance is not in the tree for some reason.+ if (parentIndex === -1) {+ return { parentId: rootInstance.id, position: "end" };+ }++ return {+ parentId: path[parentIndex].instance.id,+ position: parentIndex === 0 ? "end" : path[parentIndex - 1].position + 1,+ };};export const useInsertInstance = () => {@@ -73,7 +84,7 @@ export const useInsertInstance = () => {"insertInstance",{instance: Instance;- dropTarget?: { instanceId: Instance["id"]; position: number };+ dropTarget?: { parentId: Instance["id"]; position: number };props?: InstanceProps;}>("insertInstance", ({ instance, dropTarget, props }) => {@@ -85,12 +96,7 @@ export const useInsertInstance = () => {const hasInserted = insertInstanceMutable(rootInstance,populatedInstance,- {- parentId:- dropTarget?.instanceId ??- getClosestValidParent(rootInstance, selectedInstance?.id).id,- position: dropTarget === undefined ? "end" : dropTarget.position,- }+ dropTarget ?? findInsertLocation(rootInstance, selectedInstance?.id));if (hasInserted) {setSelectedInstance(instance);diff --git a/apps/designer/app/canvas/shared/use-drag-drop.ts b/apps/designer/app/canvas/shared/use-drag-drop.tsindex 752ea8b081..17094b6555 100644--- a/apps/designer/app/canvas/shared/use-drag-drop.ts+++ b/apps/designer/app/canvas/shared/use-drag-drop.ts@@ -280,14 +280,14 @@ export const useDragAndDrop = () => {"insertInstance",{instance: Instance;- dropTarget: { instanceId: Instance["id"]; position: number };+ dropTarget: { parentId: Instance["id"]; position: number };}>({type: "insertInstance",payload: {instance: createInstance({ component: dragItem.component }),dropTarget: {- instanceId: dropTarget.data.id,+ parentId: dropTarget.data.id,position: dropTarget.indexWithinChildren,},},diff --git a/apps/designer/app/shared/tree-utils/get-instance-path.test.ts b/apps/designer/app/shared/tree-utils/get-instance-path.test.tsindex f41a16dc91..93c1086919 100644--- a/apps/designer/app/shared/tree-utils/get-instance-path.test.ts+++ b/apps/designer/app/shared/tree-utils/get-instance-path.test.ts@@ -1,5 +1,8 @@import { type Instance } from "@webstudio-is/react-sdk";-import { getInstancePath } from "./get-instance-path";+import {+ getInstancePath,+ getInstancePathWithPositions,+} from "./get-instance-path";const getIds = (array: Array<{ id: string }>) => array.map((item) => item.id);@@ -69,3 +72,87 @@ describe("Get instance path", () => {expect(getIds(path2)).toEqual(["1", "2a", "3a"]);});});++const getIdAndPosition = (+ array: Array<{ instance: { id: string }; position: number }>+) => array.map((item) => [item.instance.id, item.position]);++describe("Get instance path with positions", () => {+ test("single possible path", () => {+ const instance: Instance = {+ component: "Box",+ id: "1",+ cssRules: [],+ children: [+ {+ component: "Box",+ id: "2",+ cssRules: [],+ children: [+ {+ component: "Box",+ id: "3",+ cssRules: [],+ children: [],+ },+ ],+ },+ ],+ };+ const path = getInstancePathWithPositions(instance, "3");+ expect(getIdAndPosition(path)).toEqual([+ ["1", 0],+ ["2", 0],+ ["3", 0],+ ]);+ });++ test("two possible paths", () => {+ const instance: Instance = {+ component: "Box",+ id: "1",+ cssRules: [],+ children: [+ {+ component: "Box",+ id: "2a",+ cssRules: [],+ children: [+ {+ component: "Box",+ id: "3a",+ cssRules: [],+ children: [],+ },+ ],+ },+ {+ component: "Box",+ id: "2b",+ cssRules: [],+ children: [+ {+ component: "Box",+ id: "3b",+ cssRules: [],+ children: [],+ },+ ],+ },+ ],+ };+ const path1 = getInstancePathWithPositions(instance, "3b");+ expect(getIdAndPosition(path1)).toEqual([+ ["1", 0],+ ["2b", 1],+ ["3b", 0],+ ]);++ const path2 = getInstancePathWithPositions(instance, "3a");+ expect(getIdAndPosition(path2)).toEqual([+ ["1", 0],+ ["2a", 0],+ ["3a", 0],+ ]);+ });+});diff --git a/apps/designer/app/shared/tree-utils/get-instance-path.ts b/apps/designer/app/shared/tree-utils/get-instance-path.tsindex 8de86bd075..44f0c4e58b 100644--- a/apps/designer/app/shared/tree-utils/get-instance-path.ts+++ b/apps/designer/app/shared/tree-utils/get-instance-path.ts@@ -24,3 +24,29 @@ export const getInstancePath = (return path.reverse();};++export const getInstancePathWithPositions = (+ instance: Instance,+ instanceId: Instance["id"]+) => {+ const path = [];++ const find = (instance: Instance) => {+ if (instance.id === instanceId) return true;+ for (let i = 0; i < instance.children.length; i++) {+ const child = instance.children[i];+ if (typeof child === "string") continue;+ const found = find(child);+ if (found) {+ path.push({ instance: child, position: i });+ return true;+ }+ }+ };++ if (find(instance)) {+ path.push({ instance, position: 0 });+ }++ return path.reverse();+};