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.

reparentInstanceMutable helper & ...

Roman Pominov4fa48f25ed8f
diff --git a/apps/designer/app/canvas/shared/instance.ts b/apps/designer/app/canvas/shared/instance.ts
index 37cb6cb92d..f341233b62 100644
--- a/apps/designer/app/canvas/shared/instance.ts
+++ b/apps/designer/app/canvas/shared/instance.ts
@@ -17,6 +17,7 @@ import {
findClosestSiblingInstance,
insertInstanceMutable,
findInstanceById,
+ reparentInstanceMutable,
} from "~/shared/tree-utils";
import store from "immerhin";
import {
@@ -83,36 +84,18 @@ export const useReparentInstance = () => {
useSubscribe<
"reparentInstance",
{
- instance: Instance;
+ instanceId: Instance["id"];
dropTarget: { instanceId: Instance["id"]; position: number };
}
- >("reparentInstance", ({ instance, dropTarget }) => {
+ >("reparentInstance", ({ instanceId, dropTarget }) => {
store.createTransaction([rootInstanceContainer], (rootInstance) => {
if (rootInstance === undefined) return;
-
- // placement.index does not take into account the fact that the drag item will be removed.
- // we need to do this to account for it.
- //
- // @todo we need an util that can do reparenting with this adjustment
- const currentParent = findParentInstance(rootInstance, instance.id);
- let dropTargetPositionAdjusted = dropTarget.position;
- if (
- currentParent !== undefined &&
- currentParent.id === dropTarget.instanceId
- ) {
- const currentPosition = currentParent.children.findIndex(
- (x) => typeof x !== "string" && x.id === instance.id
- );
- if (currentPosition < dropTarget.position) {
- dropTargetPositionAdjusted--;
- }
- }
-
- deleteInstanceMutable(rootInstance, instance.id);
- insertInstanceMutable(rootInstance, instance, {
- parentId: dropTarget.instanceId,
- position: dropTargetPositionAdjusted,
- });
+ reparentInstanceMutable(
+ rootInstance,
+ instanceId,
+ dropTarget.instanceId,
+ dropTarget.position
+ );
});
});
};
diff --git a/apps/designer/app/canvas/shared/use-drag-drop.ts b/apps/designer/app/canvas/shared/use-drag-drop.ts
index d746193b20..a98fada445 100644
--- a/apps/designer/app/canvas/shared/use-drag-drop.ts
+++ b/apps/designer/app/canvas/shared/use-drag-drop.ts
@@ -148,10 +148,10 @@ export const useDragAndDrop = () => {
publish<
"dragStart",
- { origin: "panel" | "canvas"; dragItem: { instance: Instance } }
+ { origin: "panel" | "canvas"; dragItem: { instanceId: Instance["id"] } }
>({
type: "dragStart",
- payload: { origin: "canvas", dragItem: { instance } },
+ payload: { origin: "canvas", dragItem: { instanceId: instance.id } },
});
},
onMove: (poiterCoordinate) => {
@@ -175,13 +175,13 @@ export const useDragAndDrop = () => {
publish<
"reparentInstance",
{
- instance: Instance;
+ instanceId: Instance["id"];
dropTarget: { instanceId: Instance["id"]; position: number };
}
>({
type: "reparentInstance",
payload: {
- instance: dragItem,
+ instanceId: dragItem.id,
dropTarget: {
instanceId: dropTarget.data.id,
position: placement.index,
diff --git a/apps/designer/app/designer/designer.tsx b/apps/designer/app/designer/designer.tsx
index 564cb4cc52..c6c2d3c99c 100644
--- a/apps/designer/app/designer/designer.tsx
+++ b/apps/designer/app/designer/designer.tsx
@@ -177,7 +177,7 @@ export const Designer = ({ config, project, assets }: DesignerProps) => {
usePublishShortcuts(publish);
const onRefReadCanvasWidth = useUpdateCanvasWidth();
const { onRef: onRefReadCanvas, onTransitionEnd } = useReadCanvasRect();
- const [dragAndDrop] = useDragAndDropState();
+ const [dragAndDropState] = useDragAndDropState();
const iframeRefCallback = useCallback(
(ref) => {
@@ -202,7 +202,7 @@ export const Designer = ({ config, project, assets }: DesignerProps) => {
ref={iframeRefCallback}
src={`${config.canvasPath}/${project.id}`}
pointerEvents={
- dragAndDrop.isDragging && dragAndDrop.origin === "panel"
+ dragAndDropState.isDragging && dragAndDropState.origin === "panel"
? "none"
: "all"
}
@@ -222,7 +222,7 @@ export const Designer = ({ config, project, assets }: DesignerProps) => {
isPreviewMode={isPreviewMode}
css={{ overflow: "hidden" }}
>
- {dragAndDrop.isDragging ? (
+ {dragAndDropState.isDragging ? (
<TreePrevew />
) : (
<Inspector publish={publish} />
diff --git a/apps/designer/app/designer/features/tree-preview/tree-preview.tsx b/apps/designer/app/designer/features/tree-preview/tree-preview.tsx
index 187c96077c..9f479fb706 100644
--- a/apps/designer/app/designer/features/tree-preview/tree-preview.tsx
+++ b/apps/designer/app/designer/features/tree-preview/tree-preview.tsx
@@ -4,25 +4,19 @@ import { useMemo } from "react";
import { Tree } from "~/designer/shared/tree";
import { Flex } from "@webstudio-is/design-system";
import { useRootInstance, useDragAndDropState } from "~/shared/nano-states";
-
-import {
- getInstancePath,
- insertInstanceMutable,
- deleteInstanceMutable,
- findParentInstance,
-} from "~/shared/tree-utils";
+import { getInstancePath, reparentInstanceMutable } from "~/shared/tree-utils";
export const TreePrevew = () => {
const [rootInstance] = useRootInstance();
const [dragAndDropState] = useDragAndDropState();
- const dragItemInstance = dragAndDropState.dragItem?.instance;
+ const dragItemInstanceId = dragAndDropState.dragItem?.instanceId;
const dropTargetInstanceId = dragAndDropState.dropTarget?.instanceId;
const dropTargetPosition = dragAndDropState.dropTarget?.position;
const treeProps = useMemo(() => {
if (
- dragItemInstance === undefined ||
+ dragItemInstanceId === undefined ||
dropTargetInstanceId === undefined ||
dropTargetPosition === undefined
) {
@@ -30,40 +24,22 @@ export const TreePrevew = () => {
}
const instance: Instance = produce((draft) => {
- const currentParent = findParentInstance(draft, dragItemInstance.id);
-
- // placement.index does not take into account the fact that the drag item will be removed.
- // we need to do this to account for it.
- //
- // @todo we need an util that can do reparenting with this adjustment
- let dropTargetPositionAdjusted = dropTargetPosition;
- if (
- currentParent !== undefined &&
- currentParent.id === dropTargetInstanceId
- ) {
- const currentPosition = currentParent.children.findIndex(
- (x) => typeof x !== "string" && x.id === dragItemInstance.id
- );
- if (currentPosition < dropTargetPosition) {
- dropTargetPositionAdjusted--;
- }
- }
-
- deleteInstanceMutable(draft, dragItemInstance.id);
- insertInstanceMutable(draft, dragItemInstance, {
- parentId: dropTargetInstanceId,
- position: dropTargetPositionAdjusted,
- });
+ reparentInstanceMutable(
+ draft,
+ dragItemInstanceId,
+ dropTargetInstanceId,
+ dropTargetPosition
+ );
})(rootInstance);
return {
instance,
- selectedInstanceId: dragItemInstance.id,
- selectedInstancePath: getInstancePath(instance, dragItemInstance.id),
+ selectedInstanceId: dragItemInstanceId,
+ selectedInstancePath: getInstancePath(instance, dragItemInstanceId),
};
}, [
rootInstance,
- dragItemInstance,
+ dragItemInstanceId,
dropTargetInstanceId,
dropTargetPosition,
]);
diff --git a/apps/designer/app/shared/nano-states/nano-states.ts b/apps/designer/app/shared/nano-states/nano-states.ts
index 013e189291..b33cafd6ef 100644
--- a/apps/designer/app/shared/nano-states/nano-states.ts
+++ b/apps/designer/app/shared/nano-states/nano-states.ts
@@ -41,7 +41,7 @@ export type DragAndDropState = {
isDragging: boolean;
origin?: "canvas" | "panel";
dropTarget?: DropTargetSharedData;
- dragItem?: { instance: Instance };
+ dragItem?: { instanceId: Instance["id"] };
};
const dragAndDropStateContainer = createValueContainer<DragAndDropState>({
isDragging: false,
diff --git a/apps/designer/app/shared/nano-states/use-subscribe-drag-drop-state.ts b/apps/designer/app/shared/nano-states/use-subscribe-drag-drop-state.ts
index eaa5b104e0..a49a780a64 100644
--- a/apps/designer/app/shared/nano-states/use-subscribe-drag-drop-state.ts
+++ b/apps/designer/app/shared/nano-states/use-subscribe-drag-drop-state.ts
@@ -16,7 +16,7 @@ export const useSubscribeDragAndDropState = () => {
useSubscribe<
"dragStart",
- { origin: "panel" | "canvas"; dragItem: { instance: Instance } }
+ { origin: "panel" | "canvas"; dragItem: { instanceId: Instance["id"] } }
>("dragStart", ({ origin, dragItem }) => {
setState({
isDragging: true,
diff --git a/apps/designer/app/shared/tree-utils/__snapshots__/reparent-instance.test.ts.snap b/apps/designer/app/shared/tree-utils/__snapshots__/reparent-instance.test.ts.snap
new file mode 100644
index 0000000000..ecdb541cb2
--- /dev/null
+++ b/apps/designer/app/shared/tree-utils/__snapshots__/reparent-instance.test.ts.snap
@@ -0,0 +1,58 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`Reparent instance new parent 1`] = `
+Object {
+ "children": Array [
+ Object {
+ "children": Array [
+ Object {
+ "children": Array [],
+ "component": "Box",
+ "cssRules": Array [],
+ "id": "4",
+ },
+ ],
+ "component": "Box",
+ "cssRules": Array [],
+ "id": "2",
+ },
+ Object {
+ "children": Array [],
+ "component": "Box",
+ "cssRules": Array [],
+ "id": "3",
+ },
+ ],
+ "component": "Box",
+ "cssRules": Array [],
+ "id": "1",
+}
+`;
+
+exports[`Reparent instance same parent different position 1`] = `
+Object {
+ "children": Array [
+ Object {
+ "children": Array [],
+ "component": "Box",
+ "cssRules": Array [],
+ "id": "3",
+ },
+ Object {
+ "children": Array [],
+ "component": "Box",
+ "cssRules": Array [],
+ "id": "2",
+ },
+ Object {
+ "children": Array [],
+ "component": "Box",
+ "cssRules": Array [],
+ "id": "4",
+ },
+ ],
+ "component": "Box",
+ "cssRules": Array [],
+ "id": "1",
+}
+`;
diff --git a/apps/designer/app/shared/tree-utils/index.ts b/apps/designer/app/shared/tree-utils/index.ts
index e510e32304..545be78e8c 100644
--- a/apps/designer/app/shared/tree-utils/index.ts
+++ b/apps/designer/app/shared/tree-utils/index.ts
@@ -13,3 +13,4 @@ export * from "./get-instance-path";
export * from "./find-closest-sibling-instance";
export * from "./find-parent-instance";
export * from "./clone-instance";
+export * from "./reparent-instance";
diff --git a/apps/designer/app/shared/tree-utils/reparent-instance.test.ts b/apps/designer/app/shared/tree-utils/reparent-instance.test.ts
new file mode 100644
index 0000000000..b5ad253864
--- /dev/null
+++ b/apps/designer/app/shared/tree-utils/reparent-instance.test.ts
@@ -0,0 +1,67 @@
+import { type Instance } from "@webstudio-is/react-sdk";
+import { reparentInstanceMutable } from "./reparent-instance";
+
+describe("Reparent instance", () => {
+ test(`new parent`, () => {
+ const rootInstance: Instance = {
+ component: "Box",
+ id: "1",
+ cssRules: [],
+ children: [
+ {
+ component: "Box",
+ id: "2",
+ cssRules: [],
+ children: [],
+ },
+ {
+ component: "Box",
+ id: "3",
+ cssRules: [],
+ children: [
+ {
+ component: "Box",
+ id: "4",
+ cssRules: [],
+ children: [],
+ },
+ ],
+ },
+ ],
+ };
+
+ reparentInstanceMutable(rootInstance, "4", "2", 0);
+ expect(rootInstance).toMatchSnapshot();
+ });
+
+ test(`same parent different position`, () => {
+ const rootInstance: Instance = {
+ component: "Box",
+ id: "1",
+ cssRules: [],
+ children: [
+ {
+ component: "Box",
+ id: "2",
+ cssRules: [],
+ children: [],
+ },
+ {
+ component: "Box",
+ id: "3",
+ cssRules: [],
+ children: [],
+ },
+ {
+ component: "Box",
+ id: "4",
+ cssRules: [],
+ children: [],
+ },
+ ],
+ };
+
+ reparentInstanceMutable(rootInstance, "2", "1", 2);
+ expect(rootInstance).toMatchSnapshot();
+ });
+});
diff --git a/apps/designer/app/shared/tree-utils/reparent-instance.ts b/apps/designer/app/shared/tree-utils/reparent-instance.ts
new file mode 100644
index 0000000000..f1ff63588e
--- /dev/null
+++ b/apps/designer/app/shared/tree-utils/reparent-instance.ts
@@ -0,0 +1,35 @@
+import { type Instance } from "@webstudio-is/react-sdk";
+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
+) => {
+ const instance = findInstanceById(rootInstance, instanceId);
+ if (instance === undefined) {
+ return;
+ }
+
+ // 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);
+ let newPositionAdjusted = newPosition;
+ if (currentParent !== undefined && currentParent.id === newParentId) {
+ const currentPosition = currentParent.children.findIndex(
+ (x) => typeof x !== "string" && x.id === instance.id
+ );
+ if (currentPosition < newPosition) {
+ newPositionAdjusted--;
+ }
+ }
+
+ deleteInstanceMutable(rootInstance, instance.id);
+ insertInstanceMutable(rootInstance, instance, {
+ parentId: newParentId,
+ position: newPositionAdjusted,
+ });
+};