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.

use BaseInstance when movig data from/to iframe

Roman Pominov390b9256edce
diff --git a/apps/designer/app/canvas/shared/use-drag-drop.ts b/apps/designer/app/canvas/shared/use-drag-drop.ts
index a1101b15be..b1218606fd 100644
--- a/apps/designer/app/canvas/shared/use-drag-drop.ts
+++ b/apps/designer/app/canvas/shared/use-drag-drop.ts
@@ -3,7 +3,11 @@ import {
useRootInstance,
useTextEditingInstanceId,
} from "~/shared/nano-states";
-import { findInstanceById, getInstancePath } from "~/shared/tree-utils";
+import {
+ findInstanceById,
+ getInstancePath,
+ createInstance,
+} from "~/shared/tree-utils";
import { getInstanceElementById } from "~/shared/dom-utils";
import {
type DropTarget,
@@ -17,19 +21,20 @@ import {
useSubscribe,
type Instance,
components as primitives,
+ BaseInstance,
+ toBaseInstance,
} from "@webstudio-is/react-sdk";
export type DropTargetChangePayload = {
rect: DropTarget<null>["rect"];
placement: DropTarget<null>["placement"];
position: number;
- instanceId: Instance["id"];
- component: Instance["component"];
+ instance: BaseInstance;
};
export type DragStartPayload = {
origin: "panel" | "canvas";
- dragItem: Instance;
+ dragItem: BaseInstance;
};
export type DragEndPayload = { origin: "panel" | "canvas" };
@@ -38,7 +43,7 @@ export type DragMovePayload = { canvasCoordinates: Point };
const initialState = {
dropTarget: undefined as DropTarget<Instance> | undefined,
- dragItem: undefined as Instance | undefined,
+ dragItem: undefined as BaseInstance | undefined,
};
export const useDragAndDrop = () => {
@@ -126,10 +131,7 @@ export const useDragAndDrop = () => {
rect: dropTarget.rect,
placement: dropTarget.placement,
position: dropTarget.indexWithinChildren,
-
- // @todo: use ShalowInstance
- instanceId: dropTarget.data.id,
- component: dropTarget.data.component,
+ instance: toBaseInstance(dropTarget.data),
},
});
},
@@ -168,7 +170,7 @@ export const useDragAndDrop = () => {
type: "dragStart",
payload: {
origin: "canvas",
- dragItem: instance,
+ dragItem: toBaseInstance(instance),
},
});
},
@@ -264,7 +266,7 @@ export const useDragAndDrop = () => {
>({
type: "insertInstance",
payload: {
- instance: dragItem,
+ instance: createInstance({ component: dragItem.component }),
dropTarget: {
instanceId: dropTarget.data.id,
position: dropTarget.indexWithinChildren,
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 9acce49620..6d3aa9a7ba 100644
--- a/apps/designer/app/designer/features/tree-preview/tree-preview.tsx
+++ b/apps/designer/app/designer/features/tree-preview/tree-preview.tsx
@@ -8,6 +8,7 @@ import {
getInstancePath,
reparentInstanceMutable,
insertInstanceMutable,
+ createInstance,
findInstanceById,
} from "~/shared/tree-utils";
@@ -16,7 +17,7 @@ export const TreePrevew = () => {
const [dragAndDropState] = useDragAndDropState();
const dragItemInstance = dragAndDropState.dragItem;
- const dropTargetInstanceId = dragAndDropState.dropTarget?.instanceId;
+ const dropTargetInstanceId = dragAndDropState.dropTarget?.instance.id;
const dropTargetPosition = dragAndDropState.dropTarget?.position;
const treeProps = useMemo(() => {
@@ -34,10 +35,14 @@ export const TreePrevew = () => {
const instance: Instance = produce((draft) => {
if (isNew) {
- insertInstanceMutable(draft, dragItemInstance, {
- parentId: dropTargetInstanceId,
- position: dropTargetPosition,
- });
+ insertInstanceMutable(
+ draft,
+ createInstance({ component: dragItemInstance.component }),
+ {
+ parentId: dropTargetInstanceId,
+ position: dropTargetPosition,
+ }
+ );
} else {
reparentInstanceMutable(
draft,
diff --git a/apps/designer/app/designer/features/workspace/canvas-tools/outline/drop-target-outline.tsx b/apps/designer/app/designer/features/workspace/canvas-tools/outline/drop-target-outline.tsx
index 6f0bbc230a..f0c79a25d3 100644
--- a/apps/designer/app/designer/features/workspace/canvas-tools/outline/drop-target-outline.tsx
+++ b/apps/designer/app/designer/features/workspace/canvas-tools/outline/drop-target-outline.tsx
@@ -9,7 +9,10 @@ export const DropTargetOutline = ({
}) => {
return (
<Outline rect={dropTarget.rect}>
- <Label component={dropTarget.component} instanceRect={dropTarget.rect} />
+ <Label
+ component={dropTarget.instance.component}
+ instanceRect={dropTarget.rect}
+ />
</Outline>
);
};
diff --git a/packages/react-sdk/src/db/instance.ts b/packages/react-sdk/src/db/instance.ts
index 6f39ae6377..e5c8b71b2e 100644
--- a/packages/react-sdk/src/db/instance.ts
+++ b/packages/react-sdk/src/db/instance.ts
@@ -2,13 +2,26 @@ import { z } from "zod";
import * as components from "../components";
import { type CssRule, CssRuleSchema } from "../css";
-export type Instance = {
+// This should be used when passing a lot of data is potentially costly.
+// For example, when passing data from an iframe.
+export type BaseInstance = {
id: string;
component: keyof typeof components;
- children: Array<Instance | string>;
cssRules: Array<CssRule>;
};
+export type Instance = BaseInstance & {
+ children: Array<Instance | string>;
+};
+
+export const toBaseInstance = (instance: Instance): BaseInstance => {
+ return {
+ id: instance.id,
+ component: instance.component,
+ cssRules: instance.cssRules,
+ };
+};
+
export const InstanceSchema = z.lazy(
() =>
z.object({