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
Refactor instance reparenting with instances index (#977)
Bogdan Chadkinabdfbf2384c0
diff --git a/apps/designer/app/canvas/canvas.tsx b/apps/designer/app/canvas/canvas.tsxindex a0d54ee2cf..45530178ea 100644--- a/apps/designer/app/canvas/canvas.tsx+++ b/apps/designer/app/canvas/canvas.tsx@@ -18,7 +18,6 @@ import { useShortcuts } from "./shared/use-shortcuts";import {useInsertInstance,usePublishTextEditingInstanceId,- useReparentInstance,} from "./shared/instance";import { useManageDesignModeStyles, GlobalStyles } from "./shared/styles";import { useTrackSelectedElement } from "./shared/use-track-selected-element";@@ -89,7 +88,6 @@ const useElementsTree = () => {const DesignMode = () => {useManageDesignModeStyles();useInsertInstance();- useReparentInstance();useTrackSelectedElement();usePublishScrollState();useSubscribeScrollState();diff --git a/apps/designer/app/canvas/features/wrapper-component/selected-instance-connector.ts b/apps/designer/app/canvas/features/wrapper-component/selected-instance-connector.tsindex fb6b805f10..91618761fc 100644--- a/apps/designer/app/canvas/features/wrapper-component/selected-instance-connector.ts+++ b/apps/designer/app/canvas/features/wrapper-component/selected-instance-connector.ts@@ -1,12 +1,16 @@import { useEffect } from "react";+import { useStore } from "@nanostores/react";import type { Instance, Prop, Styles } from "@webstudio-is/project-build";import { getBrowserStyle } from "@webstudio-is/react-sdk";-import { publish, subscribe, subscribeAll } from "~/shared/pubsub";+import { publish, subscribe } from "~/shared/pubsub";import {subscribeScrollState,subscribeWindowResize,} from "~/shared/dom-hooks";-import { selectedInstanceBrowserStyleStore } from "~/shared/nano-states";+import {+ rootInstanceContainer,+ selectedInstanceBrowserStyleStore,+} from "~/shared/nano-states";declare module "~/shared/pubsub" {export interface PubsubMap {@@ -56,6 +60,7 @@ export const SelectedInstanceConnector = ({instanceStyles: Styles;instanceProps: undefined | Prop[];}) => {+ const rootInstance = useStore(rootInstanceContainer);useEffect(() => {const element = instanceElementRef.current;if (element === undefined) {@@ -66,9 +71,6 @@ export const SelectedInstanceConnector = ({// so actual state is always provided hereshowOutline(element);- // ResizeObserver does not work for inline elements- const canObserve = getComputedStyle(element).display !== "inline";-const resizeObserver = new ResizeObserver(() => {// contentRect has wrong x/y values for absolutely positioned element.// getBoundingClientRect is used instead.@@ -86,16 +88,6 @@ export const SelectedInstanceConnector = ({mutationObserver.observe(parent, { childList: true });}- let unsubscribeTreeChange: undefined | (() => void);- if (canObserve === false) {- // recompute inline elements on tree changes- unsubscribeTreeChange = subscribeAll((type) => {- if (type === "insertInstance" || type === "reparentInstance") {- updateOutlineRect(element);- }- });- }-// hide rect when preview style is send// new rect will be send when new styles// will be written to instance css rules@@ -127,14 +119,19 @@ export const SelectedInstanceConnector = ({return () => {resizeObserver.disconnect();mutationObserver.disconnect();- unsubscribeTreeChange?.();unsubscribePreviewStyle();unsubscribeScrollState();unsubscribeWindowResize();};-+ }, [+ instanceElementRef,+ instance,+ instanceStyles,// instance props may change dom element- }, [instanceElementRef, instance, instanceStyles, instanceProps]);+ instanceProps,+ // update on all changes in the tree in case ResizeObserver does ont work+ rootInstance,+ ]);return null;};diff --git a/apps/designer/app/canvas/shared/instance.ts b/apps/designer/app/canvas/shared/instance.tsindex 2006e3ca79..05019cc382 100644--- a/apps/designer/app/canvas/shared/instance.ts+++ b/apps/designer/app/canvas/shared/instance.ts@@ -32,29 +32,6 @@ export const useInsertInstance = () => {});};-export const useReparentInstance = () => {- useSubscribe("reparentInstance", ({ instanceId, dropTarget }) => {- const selectedInstanceId = selectedInstanceIdStore.get();- store.createTransaction([rootInstanceContainer], (rootInstance) => {- if (rootInstance === undefined) {- return;- }- utils.tree.reparentInstanceMutable(- rootInstance,- instanceId,- dropTarget.instanceId,- dropTarget.position- );- });-- const rootInstance = rootInstanceContainer.get();- // Make the drag item the selected instance- if (selectedInstanceId !== instanceId && rootInstance !== undefined) {- selectedInstanceIdStore.set(instanceId);- }- });-};-export const usePublishTextEditingInstanceId = () => {const [editingInstanceId] = useTextEditingInstanceId();useEffect(() => {diff --git a/apps/designer/app/canvas/shared/use-drag-drop.ts b/apps/designer/app/canvas/shared/use-drag-drop.tsindex b4a03de879..fe7638ebb0 100644--- a/apps/designer/app/canvas/shared/use-drag-drop.ts+++ b/apps/designer/app/canvas/shared/use-drag-drop.ts@@ -1,14 +1,5 @@import { useLayoutEffect, useRef } from "react";-import {- useRootInstance,- useTextEditingInstanceId,-} from "~/shared/nano-states";import { utils } from "@webstudio-is/project";-import {- findInstanceByElement,- getInstanceElementById,- getInstanceIdFromElement,-} from "~/shared/dom-utils";import {type DropTarget,type Point,@@ -22,7 +13,17 @@ import {toBaseInstance,} from "@webstudio-is/project-build";import { getComponentMeta } from "@webstudio-is/react-sdk";+import {+ useRootInstance,+ useTextEditingInstanceId,+} from "~/shared/nano-states";+import {+ findInstanceByElement,+ getInstanceElementById,+ getInstanceIdFromElement,+} from "~/shared/dom-utils";import { publish, useSubscribe } from "~/shared/pubsub";+import { reparentInstance } from "~/shared/instance-utils";declare module "~/shared/pubsub" {export interface PubsubMap {@@ -226,15 +227,9 @@ export const useDragAndDrop = () => {const { dropTarget, dragItem } = state.current;if (dropTarget && dragItem && isCanceled === false) {- publish({- type: "reparentInstance",- payload: {- instanceId: dragItem.id,- dropTarget: {- instanceId: dropTarget.data.id,- position: dropTarget.indexWithinChildren,- },- },+ reparentInstance(dragItem.id, {+ parentId: dropTarget.data.id,+ position: dropTarget.indexWithinChildren,});}diff --git a/apps/designer/app/designer/designer.tsx b/apps/designer/app/designer/designer.tsxindex e26039f64a..6cc1b10a1f 100644--- a/apps/designer/app/designer/designer.tsx+++ b/apps/designer/app/designer/designer.tsx@@ -217,9 +217,9 @@ const ChromeWrapper = ({ children, isPreviewMode }: ChromeWrapperProps) => {);};-type NavigatorPanelProps = { publish: Publish; isPreviewMode: boolean };+type NavigatorPanelProps = { isPreviewMode: boolean };-const NavigatorPanel = ({ publish, isPreviewMode }: NavigatorPanelProps) => {+const NavigatorPanel = ({ isPreviewMode }: NavigatorPanelProps) => {const navigatorLayout = useNavigatorLayout();if (navigatorLayout === "docked") {@@ -235,7 +235,7 @@ const NavigatorPanel = ({ publish, isPreviewMode }: NavigatorPanelProps) => {height: "100%",}}>- <Navigator publish={publish} isClosable={false} />+ <Navigator isClosable={false} /></Box></SidePanel>);@@ -347,7 +347,7 @@ export const Designer = ({<SidePanel gridArea="sidebar" isPreviewMode={isPreviewMode}><SidebarLeft publish={publish} /></SidePanel>- <NavigatorPanel publish={publish} isPreviewMode={isPreviewMode} />+ <NavigatorPanel isPreviewMode={isPreviewMode} /><SidePanelgridArea="inspector"isPreviewMode={isPreviewMode}diff --git a/apps/designer/app/designer/features/sidebar-left/navigator/navigator.tsx b/apps/designer/app/designer/features/sidebar-left/navigator/navigator.tsxindex 9423331922..69311576ac 100644--- a/apps/designer/app/designer/features/sidebar-left/navigator/navigator.tsx+++ b/apps/designer/app/designer/features/sidebar-left/navigator/navigator.tsx@@ -2,31 +2,21 @@ import { useCallback } from "react";import { useStore } from "@nanostores/react";import { Flex } from "@webstudio-is/design-system";import type { Instance } from "@webstudio-is/project-build";-import { type Publish } from "~/shared/pubsub";import {selectedInstanceIdStore,hoveredInstanceIdStore,useRootInstance,} from "~/shared/nano-states";import { InstanceTree } from "~/designer/shared/tree";+import { reparentInstance } from "~/shared/instance-utils";import { Header, CloseButton } from "../header";-declare module "~/shared/pubsub" {- export interface PubsubMap {- reparentInstance: {- instanceId: Instance["id"];- dropTarget: { instanceId: Instance["id"]; position: number | "end" };- };- }-}-type NavigatorProps = {- publish: Publish;isClosable?: boolean;onClose?: () => void;};-export const Navigator = ({ publish, isClosable, onClose }: NavigatorProps) => {+export const Navigator = ({ isClosable, onClose }: NavigatorProps) => {const selectedInstanceId = useStore(selectedInstanceIdStore);const [rootInstance] = useRootInstance();@@ -39,18 +29,12 @@ export const Navigator = ({ publish, isClosable, onClose }: NavigatorProps) => {itemId: string;dropTarget: { itemId: string; position: number | "end" };}) => {- publish({- type: "reparentInstance",- payload: {- instanceId: payload.itemId,- dropTarget: {- instanceId: payload.dropTarget.itemId,- position: payload.dropTarget.position,- },- },+ reparentInstance(payload.itemId, {+ parentId: payload.dropTarget.itemId,+ position: payload.dropTarget.position,});},- [publish]+ []);const handleHover = useCallback((instance: Instance | undefined) => {diff --git a/apps/designer/app/designer/features/sidebar-left/panels/navigator/navigator.tsx b/apps/designer/app/designer/features/sidebar-left/panels/navigator/navigator.tsxindex b9fc76df8e..ded4d4773b 100644--- a/apps/designer/app/designer/features/sidebar-left/panels/navigator/navigator.tsx+++ b/apps/designer/app/designer/features/sidebar-left/panels/navigator/navigator.tsx@@ -1,17 +1,14 @@import { ListNestedIcon } from "@webstudio-is/icons";-import { type Publish } from "~/shared/pubsub";import { Navigator } from "../../navigator";import { TabName } from "../../types";type TabContentProps = {- publish: Publish;onSetActiveTab: (tabName: TabName) => void;};-export const TabContent = ({ publish, onSetActiveTab }: TabContentProps) => {+export const TabContent = ({ onSetActiveTab }: TabContentProps) => {return (<Navigator- publish={publish}onClose={() => {onSetActiveTab("none");}}diff --git a/apps/designer/app/designer/features/tree-preview/tree-preview.tsx b/apps/designer/app/designer/features/tree-preview/tree-preview.tsxindex fbca323ee0..7f40238de1 100644--- a/apps/designer/app/designer/features/tree-preview/tree-preview.tsx+++ b/apps/designer/app/designer/features/tree-preview/tree-preview.tsx@@ -1,11 +1,14 @@import produce from "immer";-import type { Instance } from "@webstudio-is/project-build";import { useMemo } from "react";-import { Flex } from "@webstudio-is/design-system";+import type { Instance } from "@webstudio-is/project-build";+import { theme, Flex } from "@webstudio-is/design-system";+import { utils } from "@webstudio-is/project";import { useRootInstance, useDragAndDropState } from "~/shared/nano-states";import { InstanceTreeNode } from "~/designer/shared/tree";-import { utils } from "@webstudio-is/project";-import { theme } from "@webstudio-is/design-system";+import {+ createInstancesIndex,+ reparentInstanceMutable,+} from "~/shared/tree-utils";export const TreePrevew = () => {const [rootInstance] = useRootInstance();@@ -29,7 +32,8 @@ export const TreePrevew = () => {utils.tree.findInstanceById(rootInstance, dragItemInstance.id) ===undefined;- const instance: Instance = produce((draft) => {+ const instance: Instance = produce<Instance>((draft) => {+ const instancesIndex = createInstancesIndex(draft);if (isNew) {utils.tree.insertInstanceMutable(draft,@@ -40,12 +44,10 @@ export const TreePrevew = () => {});} else {- utils.tree.reparentInstanceMutable(- draft,- dragItemInstance.id,- dropTargetInstanceId,- dropTargetPosition- );+ reparentInstanceMutable(instancesIndex, dragItemInstance.id, {+ parentId: dropTargetInstanceId,+ position: dropTargetPosition,+ });}})(rootInstance);diff --git a/apps/designer/app/shared/instance-utils.ts b/apps/designer/app/shared/instance-utils.tsindex 7369cca550..8a2e5998b2 100644--- a/apps/designer/app/shared/instance-utils.ts+++ b/apps/designer/app/shared/instance-utils.ts@@ -8,9 +8,26 @@ import {styleSourceSelectionsStore,styleSourcesStore,} from "./nano-states";-import { findSubtree, findSubtreeLocalStyleSources } from "./tree-utils";+import {+ createInstancesIndex,+ DroppableTarget,+ findSubtree,+ findSubtreeLocalStyleSources,+ reparentInstanceMutable,+} from "./tree-utils";import { removeByMutable } from "./array-utils";+export const reparentInstance = (+ targetInstanceId: Instance["id"],+ dropTarget: DroppableTarget+) => {+ store.createTransaction([rootInstanceContainer], (rootInstance) => {+ const instancesIndex = createInstancesIndex(rootInstance);+ reparentInstanceMutable(instancesIndex, targetInstanceId, dropTarget);+ });+ selectedInstanceIdStore.set(targetInstanceId);+};+export const deleteInstance = (targetInstanceId: Instance["id"]) => {store.createTransaction([diff --git a/apps/designer/app/shared/tree-utils.test.ts b/apps/designer/app/shared/tree-utils.test.tsindex 6b2a8e18ad..63a49ffc24 100644--- a/apps/designer/app/shared/tree-utils.test.ts+++ b/apps/designer/app/shared/tree-utils.test.ts@@ -16,6 +16,7 @@ import {findClosestDroppableTarget,findSubtree,findSubtreeLocalStyleSources,+ reparentInstanceMutable,} from "./tree-utils";const expectString = expect.any(String) as unknown as string;@@ -121,6 +122,68 @@ test("find closest droppable target", () => {});});+test("reparent instance into target", () => {+ const rootInstance = createInstance("root", "Body", [+ createInstance("target", "Box", []),+ createInstance("box1", "Box", [+ createInstance("box11", "Box", []),+ createInstance("box12", "Box", []),+ createInstance("box13", "Box", []),+ ]),+ createInstance("box2", "Box", []),+ ]);+ let instancesIndex = createInstancesIndex(rootInstance);+ reparentInstanceMutable(instancesIndex, "target", {+ parentId: "box1",+ position: 1,+ });+ expect(rootInstance).toEqual(+ createInstance("root", "Body", [+ createInstance("box1", "Box", [+ createInstance("box11", "Box", []),+ createInstance("target", "Box", []),+ createInstance("box12", "Box", []),+ createInstance("box13", "Box", []),+ ]),+ createInstance("box2", "Box", []),+ ])+ );++ instancesIndex = createInstancesIndex(rootInstance);+ reparentInstanceMutable(instancesIndex, "target", {+ parentId: "box1",+ position: 3,+ });+ expect(rootInstance).toEqual(+ createInstance("root", "Body", [+ createInstance("box1", "Box", [+ createInstance("box11", "Box", []),+ createInstance("box12", "Box", []),+ createInstance("target", "Box", []),+ createInstance("box13", "Box", []),+ ]),+ createInstance("box2", "Box", []),+ ])+ );++ instancesIndex = createInstancesIndex(rootInstance);+ reparentInstanceMutable(instancesIndex, "target", {+ parentId: "root",+ position: "end",+ });+ expect(rootInstance).toEqual(+ createInstance("root", "Body", [+ createInstance("box1", "Box", [+ createInstance("box11", "Box", []),+ createInstance("box12", "Box", []),+ createInstance("box13", "Box", []),+ ]),+ createInstance("box2", "Box", []),+ createInstance("target", "Box", []),+ ])+ );+});+test("find subtree with all descendants and parent instance", () => {const rootInstance: Instance = createInstance("root", "Box", [createInstance("box1", "Box", []),diff --git a/apps/designer/app/shared/tree-utils.ts b/apps/designer/app/shared/tree-utils.tsindex f50ae9ff26..cecac3a8f6 100644--- a/apps/designer/app/shared/tree-utils.ts+++ b/apps/designer/app/shared/tree-utils.ts@@ -57,9 +57,9 @@ const isInstanceDroppable = (instance: Instance) => {return meta?.type === "body" || meta?.type === "container";};-type DroppableTarget = {+export type DroppableTarget = {parentId: Instance["id"];- position: number;+ position: number | "end";};export const findClosestDroppableTarget = (@@ -100,6 +100,48 @@ export const findClosestDroppableTarget = (};};+export const reparentInstanceMutable = (+ instancesIndex: InstancesIndex,+ instanceId: Instance["id"],+ dropTarget: DroppableTarget+) => {+ const prevParent = instancesIndex.parentInstancesById.get(instanceId);+ const nextParent = instancesIndex.instancesById.get(dropTarget.parentId);+ const instance = instancesIndex.instancesById.get(instanceId);+ if (+ prevParent === undefined ||+ nextParent === undefined ||+ instance === undefined+ ) {+ return;+ }++ const prevPosition = prevParent.children.findIndex(+ (child) => child.type === "instance" && child.id === instanceId+ );+ if (prevPosition === -1) {+ return;+ }++ // if parent is the same, we need to adjust the position+ // to account for the removal of the instance.+ let nextPosition = dropTarget.position;+ if (+ nextPosition !== "end" &&+ prevParent.id === nextParent.id &&+ prevPosition < nextPosition+ ) {+ nextPosition -= 1;+ }++ prevParent.children.splice(prevPosition, 1);+ if (nextPosition === "end") {+ nextParent.children.push(instance);+ } else {+ nextParent.children.splice(nextPosition, 0, instance);+ }+};+export const findSubtree = (rootInstance: Instance,targetInstanceId: Instance["id"]diff --git a/packages/project/src/shared/tree-utils/__snapshots__/delete-instance.test.ts.snap b/packages/project/src/shared/tree-utils/__snapshots__/delete-instance.test.ts.snapdeleted file mode 100644index 253f52c7c6..0000000000--- a/packages/project/src/shared/tree-utils/__snapshots__/delete-instance.test.ts.snap+++ /dev/null@@ -1,33 +0,0 @@-// Jest Snapshot v1, https://goo.gl/fbAQLP--exports[`Delete instance div 1 > div 2 > [div 3] 1`] = `-{- "children": [- {- "children": [],- "component": "Box",- "id": "2",- "type": "instance",- },- ],- "component": "Box",- "id": "1",- "type": "instance",-}-`;--exports[`Delete instance div 1 > div 2, [div 3] 1`] = `-{- "children": [- {- "children": [],- "component": "Box",- "id": "2",- "type": "instance",- },- ],- "component": "Box",- "id": "1",- "type": "instance",-}-`;diff --git a/packages/project/src/shared/tree-utils/__snapshots__/reparent-instance.test.ts.snap b/packages/project/src/shared/tree-utils/__snapshots__/reparent-instance.test.ts.snapdeleted file mode 100644index 2e42d526fe..0000000000--- a/packages/project/src/shared/tree-utils/__snapshots__/reparent-instance.test.ts.snap+++ /dev/null@@ -1,58 +0,0 @@-// Jest Snapshot v1, https://goo.gl/fbAQLP--exports[`Reparent instance new parent 1`] = `-{- "children": [- {- "children": [- {- "children": [],- "component": "Box",- "id": "4",- "type": "instance",- },- ],- "component": "Box",- "id": "2",- "type": "instance",- },- {- "children": [],- "component": "Box",- "id": "3",- "type": "instance",- },- ],- "component": "Box",- "id": "1",- "type": "instance",-}-`;--exports[`Reparent instance same parent different position 1`] = `-{- "children": [- {- "children": [],- "component": "Box",- "id": "3",- "type": "instance",- },- {- "children": [],- "component": "Box",- "id": "2",- "type": "instance",- },- {- "children": [],- "component": "Box",- "id": "4",- "type": "instance",- },- ],- "component": "Box",- "id": "1",- "type": "instance",-}-`;diff --git a/packages/project/src/shared/tree-utils/delete-instance.test.ts b/packages/project/src/shared/tree-utils/delete-instance.test.tsdeleted file mode 100644index efc6300cd6..0000000000--- a/packages/project/src/shared/tree-utils/delete-instance.test.ts+++ /dev/null@@ -1,56 +0,0 @@-import { describe, test, expect } from "@jest/globals";-import type { Instance } from "@webstudio-is/project-build";-import { deleteInstanceMutable } from "./delete-instance";--describe("Delete instance", () => {- test("div 1 > div 2 > [div 3]", () => {- const instance: Instance = {- type: "instance",- component: "Box",- id: "1",- children: [- {- type: "instance",- component: "Box",- id: "2",- children: [- {- type: "instance",- component: "Box",- id: "3",- children: [],- },- ],- },- ],- };-- deleteInstanceMutable(instance, "3");- expect(instance).toMatchSnapshot();- });-- test("div 1 > div 2, [div 3]", () => {- const instance: Instance = {- type: "instance",- component: "Box",- id: "1",- children: [- {- type: "instance",- component: "Box",- id: "2",- children: [],- },- {- type: "instance",- component: "Box",- id: "3",- children: [],- },- ],- };-- deleteInstanceMutable(instance, "3");- expect(instance).toMatchSnapshot();- });-});diff --git a/packages/project/src/shared/tree-utils/delete-instance.ts b/packages/project/src/shared/tree-utils/delete-instance.tsdeleted file mode 100644index f17d796ed6..0000000000--- a/packages/project/src/shared/tree-utils/delete-instance.ts+++ /dev/null@@ -1,23 +0,0 @@-import type { Instance } from "@webstudio-is/project-build";--export const deleteInstanceMutable = (- instance: Instance,- instanceId: Instance["id"]-): boolean => {- if (instance.id === instanceId) {- return true;- }-- for (const child of instance.children) {- if (child.type === "text") {- continue;- }- const shouldDelete = deleteInstanceMutable(child, instanceId);- if (shouldDelete === true) {- const index = instance.children.indexOf(child);- instance.children.splice(index, 1);- return false;- }- }- return false;-};diff --git a/packages/project/src/shared/tree-utils/find-parent-instance.test.ts b/packages/project/src/shared/tree-utils/find-parent-instance.test.tsdeleted file mode 100644index 4884ac4a3a..0000000000--- a/packages/project/src/shared/tree-utils/find-parent-instance.test.ts+++ /dev/null@@ -1,32 +0,0 @@-import { describe, test, expect } from "@jest/globals";-import type { Instance } from "@webstudio-is/project-build";-import { findParentInstance } from "./find-parent-instance";--describe("Find parent instance", () => {- test("find", () => {- const rootInstance: Instance = {- type: "instance",- component: "Box",- id: "1",- children: [- {- type: "instance",- component: "Box",- id: "2",- children: [- {- type: "instance",- component: "Box",- id: "3",- children: [],- },- ],- },- ],- };-- expect(findParentInstance(rootInstance, "3")?.id).toBe("2");- expect(findParentInstance(rootInstance, "2")?.id).toBe("1");- expect(findParentInstance(rootInstance, "1")).toBe(undefined);- });-});diff --git a/packages/project/src/shared/tree-utils/find-parent-instance.ts b/packages/project/src/shared/tree-utils/find-parent-instance.tsdeleted file mode 100644index a0de3c7f0c..0000000000--- a/packages/project/src/shared/tree-utils/find-parent-instance.ts+++ /dev/null@@ -1,25 +0,0 @@-import type { Instance } from "@webstudio-is/project-build";--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 (child.type === "text") {- continue;- }- const foundInstance = find(child, childInstance);- if (foundInstance !== undefined) {- return foundInstance;- }- }- };- return find(instance);-};diff --git a/packages/project/src/shared/tree-utils/index.ts b/packages/project/src/shared/tree-utils/index.tsindex f78e49adda..1bbf3f56c1 100644--- a/packages/project/src/shared/tree-utils/index.ts+++ b/packages/project/src/shared/tree-utils/index.ts@@ -1,11 +1,8 @@export * from "./create-instance";-export * from "./delete-instance";export * from "./insert-instance";export * from "./find-instance";export * from "./get-instance-path";export * from "./find-closest-sibling-instance";-export * from "./find-parent-instance";-export * from "./reparent-instance";export * from "./find-closest-non-inline-parent";export * from "./set-instance-children";export * from "./find-instance";diff --git a/packages/project/src/shared/tree-utils/reparent-instance.test.ts b/packages/project/src/shared/tree-utils/reparent-instance.test.tsdeleted file mode 100644index ad27f0f136..0000000000--- a/packages/project/src/shared/tree-utils/reparent-instance.test.ts+++ /dev/null@@ -1,68 +0,0 @@-import { describe, test, expect } from "@jest/globals";-import type { Instance } from "@webstudio-is/project-build";-import { reparentInstanceMutable } from "./reparent-instance";--describe("Reparent instance", () => {- test(`new parent`, () => {- const rootInstance: Instance = {- type: "instance",- component: "Box",- id: "1",- children: [- {- type: "instance",- component: "Box",- id: "2",- children: [],- },- {- type: "instance",- component: "Box",- id: "3",- children: [- {- type: "instance",- component: "Box",- id: "4",- children: [],- },- ],- },- ],- };-- reparentInstanceMutable(rootInstance, "4", "2", 0);- expect(rootInstance).toMatchSnapshot();- });-- test(`same parent different position`, () => {- const rootInstance: Instance = {- type: "instance",- component: "Box",- id: "1",- children: [- {- type: "instance",- component: "Box",- id: "2",- children: [],- },- {- type: "instance",- component: "Box",- id: "3",- children: [],- },- {- type: "instance",- component: "Box",- id: "4",- children: [],- },- ],- };-- reparentInstanceMutable(rootInstance, "2", "1", 2);- expect(rootInstance).toMatchSnapshot();- });-});diff --git a/packages/project/src/shared/tree-utils/reparent-instance.ts b/packages/project/src/shared/tree-utils/reparent-instance.tsdeleted file mode 100644index c3a3ba31f7..0000000000--- a/packages/project/src/shared/tree-utils/reparent-instance.ts+++ /dev/null@@ -1,45 +0,0 @@-import type { Instance } from "@webstudio-is/project-build";-import { findInstanceById } from "./find-instance";-import { findParentInstance } from "./find-parent-instance";-import { deleteInstanceMutable } from "./delete-instance";-import { insertInstanceMutable } from "./insert-instance";--export const reparentInstanceMutable = (- rootInstance: Instance,- instanceId: Instance["id"],- newParentId: Instance["id"],- newPosition: number | "end"-) => {- const instance = findInstanceById(rootInstance, instanceId);- if (instance === undefined) {- return;- }-- if (newPosition === "end") {- deleteInstanceMutable(rootInstance, instance.id);- insertInstanceMutable(rootInstance, instance, {- parentId: newParentId,- position: "end",- });- return;- }-- let newPositionAdjusted = newPosition;-- // If parent is the same, we need to adjust the position to account for the removal of the instance.- const currentParent = findParentInstance(rootInstance, instance.id);- if (currentParent !== undefined && currentParent.id === newParentId) {- const currentPosition = currentParent.children.findIndex(- (child) => child.type === "instance" && child.id === instance.id- );- if (currentPosition < newPosition) {- newPositionAdjusted--;- }- }-- deleteInstanceMutable(rootInstance, instance.id);- insertInstanceMutable(rootInstance, instance, {- parentId: newParentId,- position: newPositionAdjusted,- });-};