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.

make tree-preview work & fix a bug

Roman Pominova49a795a5644
diff --git a/apps/designer/app/canvas/shared/use-drag-drop.ts b/apps/designer/app/canvas/shared/use-drag-drop.ts
index 62a366d384..a99291976c 100644
--- a/apps/designer/app/canvas/shared/use-drag-drop.ts
+++ b/apps/designer/app/canvas/shared/use-drag-drop.ts
@@ -12,22 +12,11 @@ import { findInstanceById, getInstancePath } from "~/shared/tree-utils";
import { primitives } from "~/shared/canvas-components";
import { publish, type Instance } from "@webstudio-is/react-sdk";
-// This is used in tree-preview
-// @todo: Update it to use the new events, or fire these events as well.
-export type DragData = { instance: Instance };
-export type DropData = {
- instance: { id: Instance["id"] };
- position: number | "end";
-};
-// publish<"dropPreview", { dropData: DropData; dragData: DragData }>({
-// type: "dropPreview",
-// payload: { dropData, dragData },
-// });
-
// data shared between iframe and main window
export type DropTargetSharedData = {
rect: Rect;
placementRect: Rect;
+ position: number;
instanceId: Instance["id"];
instanceComponent: Instance["component"];
};
@@ -53,6 +42,12 @@ export const useDragAndDrop = () => {
payload: {
rect: dropTarget.rect,
placementRect: placement.placementRect,
+
+ // @todo: adjust index if the parent stays the same
+ // (account for the item being removed from the old position)
+ // Can the hook do that for us automatically?
+ position: placement.index,
+
instanceId: dropTarget.data.id,
instanceComponent: dropTarget.data.component,
},
@@ -84,24 +79,27 @@ export const useDragAndDrop = () => {
// This must be fast, it can be called multiple times per pointer move
swapDropTarget(dropTarget) {
+ const { dragItem } = state.current;
if (
+ dragItem === undefined ||
rootInstance === undefined ||
dropTarget.data.id === rootInstance.id
) {
return dropTarget;
}
- if (
- primitives[dropTarget.data.component].canAcceptChild() &&
- dropTarget.area === "center"
- ) {
- return dropTarget;
- }
+ const path = getInstancePath(rootInstance, dropTarget.data.id);
+ path.reverse();
- // @todo: Don't allow to dpop inside drag item or any of its children
+ if (dropTarget.area !== "center") {
+ path.shift();
+ }
- const path = getInstancePath(rootInstance, dropTarget.data.id);
- path.reverse().shift();
+ // Don't allow to dpop inside drag item or any of its children
+ const dragItemIndex = path.findIndex((x) => x.id === dragItem.id);
+ if (dragItemIndex !== -1) {
+ path.splice(0, dragItemIndex + 1);
+ }
const data =
path.find((instance) =>
@@ -109,6 +107,7 @@ export const useDragAndDrop = () => {
) || rootInstance;
const element = document.getElementById(data.id);
+
if (element == null) {
return dropTarget;
}
@@ -147,9 +146,9 @@ export const useDragAndDrop = () => {
autoScrollHandlers.setEnabled(true);
- publish<"dragStart", { dragItem: { instanceId: Instance["id"] } }>({
+ publish<"dragStart", { dragItem: { instance: Instance } }>({
type: "dragStart",
- payload: { dragItem: { instanceId: instance.id } },
+ payload: { dragItem: { instance } },
});
},
onMove: (poiterCoordinate) => {
@@ -179,6 +178,10 @@ export const useDragAndDrop = () => {
instance: dragItem,
dropTarget: {
instanceId: dropTarget.data.id,
+
+ // @todo: adjust index if the parent stays the same
+ // (account for the item being removed from the old position)
+ // Can the hook do that for us automatically?
position: placement.index,
},
},
diff --git a/apps/designer/app/designer/designer.tsx b/apps/designer/app/designer/designer.tsx
index f5105acd8b..06a959f242 100644
--- a/apps/designer/app/designer/designer.tsx
+++ b/apps/designer/app/designer/designer.tsx
@@ -30,7 +30,11 @@ import {
} from "./features/workspace";
import { usePublishShortcuts } from "./shared/shortcuts";
import { type SyncStatus } from "~/shared/sync";
-import { useIsPreviewMode, useRootInstance } from "~/shared/nano-states";
+import {
+ useIsPreviewMode,
+ useRootInstance,
+ useDragAndDropState,
+} from "~/shared/nano-states";
import { useSubscribeClientSetting } from "./shared/client-settings";
export const links = () => {
@@ -186,6 +190,9 @@ export const Designer = ({ config, project, assets }: DesignerProps) => {
const onRefReadCanvasWidth = useUpdateCanvasWidth();
const { onRef: onRefReadCanvas, onTransitionEnd } = useReadCanvasRect();
+ // @todo: useIsDragging propably should live here too eventually?
+ const [dragAndDrop] = useDragAndDropState();
+
const iframeRefCallback = useCallback(
(ref) => {
publishRef.current = ref;
@@ -229,7 +236,11 @@ export const Designer = ({ config, project, assets }: DesignerProps) => {
isPreviewMode={isPreviewMode}
css={{ overflow: "hidden" }}
>
- {isDragging ? <TreePrevew /> : <Inspector publish={publish} />}
+ {isDragging || dragAndDrop.isDragging ? (
+ <TreePrevew />
+ ) : (
+ <Inspector publish={publish} />
+ )}
</SidePanel>
<Breadcrumbs publish={publish} />
</ChromeWrapper>
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 ed606f3f20..b16a327568 100644
--- a/apps/designer/app/designer/features/tree-preview/tree-preview.tsx
+++ b/apps/designer/app/designer/features/tree-preview/tree-preview.tsx
@@ -1,12 +1,10 @@
-import { Instance, useSubscribe } from "@webstudio-is/react-sdk";
import produce from "immer";
-import { useState } from "react";
+import { useMemo } from "react";
import { Tree } from "~/designer/shared/tree";
-import { type DragData, type DropData } from "~/canvas/shared/use-drag-drop";
import { Flex } from "~/shared/design-system";
-import { useRootInstance } from "~/shared/nano-states";
+import { useRootInstance, useDragAndDropState } from "~/shared/nano-states";
import {
- findInstanceById,
+ // findInstanceById,
getInstancePath,
insertInstanceMutable,
deleteInstanceMutable,
@@ -14,38 +12,70 @@ import {
export const TreePrevew = () => {
const [rootInstance] = useRootInstance();
- const [draftRootInstance, setDraftRootInstance] = useState(rootInstance);
- const [instanceId, setInstanceId] = useState<Instance["id"]>();
-
- useSubscribe<"dropPreview", { dragData: DragData; dropData: DropData }>(
- "dropPreview",
- ({ dragData, dropData }) => {
- if (rootInstance === undefined) return;
- setInstanceId(dragData.instance.id);
-
- const isNew =
- findInstanceById(rootInstance, dragData.instance.id) === undefined;
-
- const updatedRootInstance = produce((rootInstanceDraft) => {
- // - Only delete if the instance existed before.
- // - Can't reparent an instance inside itself.
- if (isNew === false && dropData.instance.id !== dragData.instance.id) {
- deleteInstanceMutable(rootInstanceDraft, dragData.instance.id);
- }
- insertInstanceMutable(rootInstanceDraft, dragData.instance, {
- parentId: dropData.instance.id,
- position: dropData.position,
- });
- })(rootInstance);
- setDraftRootInstance(updatedRootInstance);
+ const [dragAndDropState] = useDragAndDropState();
+
+ // const [draftRootInstance, setDraftRootInstance] = useState(rootInstance);
+ // const [instanceId, setInstanceId] = useState<Instance["id"]>();
+
+ // useSubscribe<"dropPreview", { dragData: DragData; dropData: DropData }>(
+ // "dropPreview",
+ // ({ dragData, dropData }) => {
+ // if (rootInstance === undefined) return;
+ // setInstanceId(dragData.instance.id);
+
+ // const isNew =
+ // findInstanceById(rootInstance, dragData.instance.id) === undefined;
+
+ // const updatedRootInstance = produce((rootInstanceDraft) => {
+ // // - Only delete if the instance existed before.
+ // // - Can't reparent an instance inside itself.
+ // if (isNew === false && dropData.instance.id !== dragData.instance.id) {
+ // deleteInstanceMutable(rootInstanceDraft, dragData.instance.id);
+ // }
+ // insertInstanceMutable(rootInstanceDraft, dragData.instance, {
+ // parentId: dropData.instance.id,
+ // position: dropData.position,
+ // });
+ // })(rootInstance);
+ // setDraftRootInstance(updatedRootInstance);
+ // }
+ // );
+
+ const dragItemInstance = dragAndDropState.dragItem?.instance;
+ const dropTargetInstanceId = dragAndDropState.dropTarget?.instanceId;
+ const dropTargetPosition = dragAndDropState.dropTarget?.position;
+
+ const draftRootInstance = useMemo(() => {
+ if (
+ dragItemInstance === undefined ||
+ dropTargetInstanceId === undefined ||
+ dropTargetPosition === undefined
+ ) {
+ return rootInstance;
}
- );
- if (draftRootInstance === undefined || instanceId === undefined) {
+ return produce((draft) => {
+ deleteInstanceMutable(draft, dragItemInstance.id);
+ insertInstanceMutable(draft, dragItemInstance, {
+ parentId: dropTargetInstanceId,
+ position: dropTargetPosition,
+ });
+ })(rootInstance);
+ }, [
+ rootInstance,
+ dragItemInstance,
+ dropTargetInstanceId,
+ dropTargetPosition,
+ ]);
+
+ if (draftRootInstance === undefined || dragItemInstance === undefined) {
return null;
}
- const selectedInstancePath = getInstancePath(draftRootInstance, instanceId);
+ const selectedInstancePath = getInstancePath(
+ draftRootInstance,
+ dragItemInstance.id
+ );
if (selectedInstancePath.length === 0) return null;
@@ -54,7 +84,7 @@ export const TreePrevew = () => {
<Tree
instance={draftRootInstance}
selectedInstancePath={selectedInstancePath}
- selectedInstanceId={instanceId}
+ selectedInstanceId={dragItemInstance.id}
animate={false}
/>
</Flex>
diff --git a/apps/designer/app/shared/nano-states/nano-states.ts b/apps/designer/app/shared/nano-states/nano-states.ts
index c41c2febf7..45c4374c79 100644
--- a/apps/designer/app/shared/nano-states/nano-states.ts
+++ b/apps/designer/app/shared/nano-states/nano-states.ts
@@ -35,13 +35,11 @@ const textEditingInstanceIdContainer = createValueContainer<
export const useTextEditingInstanceId = () =>
useValue(textEditingInstanceIdContainer);
-export type DragAndDropState =
- | { isDragging: false }
- | {
- isDragging: true;
- dropTarget?: DropTargetSharedData;
- dragItem: { instanceId: Instance["id"] };
- };
+export type DragAndDropState = {
+ isDragging: boolean;
+ dropTarget?: DropTargetSharedData;
+ dragItem?: { instance: Instance };
+};
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 8e995465fd..1eba22389c 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
@@ -14,7 +14,7 @@ export const useSubscribeDragAndDropState = () => {
}
);
- useSubscribe<"dragStart", { dragItem: { instanceId: Instance["id"] } }>(
+ useSubscribe<"dragStart", { dragItem: { instance: Instance } }>(
"dragStart",
({ dragItem }) => {
setState({ isDragging: true, dragItem });