diff --git a/apps/designer/app/designer/features/sidebar-left/lib/navigator/navigator.tsx b/apps/designer/app/designer/features/sidebar-left/lib/navigator/navigator.tsx
index 99aae2a814..96ef4bb775 100644
--- a/apps/designer/app/designer/features/sidebar-left/lib/navigator/navigator.tsx
+++ b/apps/designer/app/designer/features/sidebar-left/lib/navigator/navigator.tsx
@@ -1,6 +1,5 @@
import { Box } from "@webstudio-is/design-system";
import type { Instance, Publish } from "@webstudio-is/react-sdk";
-import { useSelectedInstancePath } from "~/designer/shared/instance/use-selected-instance-path";
import { useSelectedInstanceData } from "~/designer/shared/nano-states";
import { Tree } from "~/designer/shared/tree";
import { useRootInstance } from "~/shared/nano-states";
@@ -15,9 +14,6 @@ type NavigatorProps = {
export const Navigator = ({ publish, isClosable, onClose }: NavigatorProps) => {
const [selectedInstanceData] = useSelectedInstanceData();
const [rootInstance] = useRootInstance();
- const selectedInstancePath = useSelectedInstancePath(
- selectedInstanceData?.id
- );
if (rootInstance === undefined) return null;
return (
@@ -25,8 +21,7 @@ export const Navigator = ({ publish, isClosable, onClose }: NavigatorProps) => {
{
publish<"selectInstanceById", Instance["id"]>({
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 6d3aa9a7ba..936a067fbb 100644
--- a/apps/designer/app/designer/features/tree-preview/tree-preview.tsx
+++ b/apps/designer/app/designer/features/tree-preview/tree-preview.tsx
@@ -5,7 +5,6 @@ import { Tree } from "~/designer/shared/tree";
import { Flex } from "@webstudio-is/design-system";
import { useRootInstance, useDragAndDropState } from "~/shared/nano-states";
import {
- getInstancePath,
reparentInstanceMutable,
insertInstanceMutable,
createInstance,
@@ -54,9 +53,8 @@ export const TreePrevew = () => {
})(rootInstance);
return {
- instance,
+ root: instance,
selectedInstanceId: dragItemInstance.id,
- selectedInstancePath: getInstancePath(instance, dragItemInstance.id),
};
}, [
rootInstance,
diff --git a/apps/designer/app/designer/shared/tree/tree.tsx b/apps/designer/app/designer/shared/tree/tree.tsx
index 5200890d10..47fec15d17 100644
--- a/apps/designer/app/designer/shared/tree/tree.tsx
+++ b/apps/designer/app/designer/shared/tree/tree.tsx
@@ -1,4 +1,14 @@
-import { useState, useMemo, useEffect } from "react";
+/* eslint-disable */
+// @todo: enable eslint
+
+import {
+ useState,
+ useMemo,
+ useEffect,
+ forwardRef,
+ type ElementRef,
+ useRef,
+} from "react";
import { type Instance, components } from "@webstudio-is/react-sdk";
import {
Flex,
@@ -7,9 +17,15 @@ import {
Button,
keyframes,
styled,
+ type DropTarget,
+ type Point,
+ useAutoScroll,
+ useDrag,
+ useDrop,
+ Box,
} from "@webstudio-is/design-system";
import { TriangleRightIcon, TriangleDownIcon } from "@webstudio-is/icons";
-import noop from "lodash.noop";
+import { findInstanceById, getInstancePath } from "~/shared/tree-utils";
const openKeyframes = keyframes({
from: { height: 0 },
@@ -35,98 +51,214 @@ const CollapsibleContentUnanimated = styled(Collapsible.Content, {
overflow: "hidden",
});
-type TreeProps = {
+type NodeProps = {
instance: Instance;
- selectedInstanceId?: Instance["id"];
+ selectedInstanceId: Instance["id"] | undefined;
selectedInstancePath: Array;
- level?: number;
+ level: number;
+ onSelect: (instance: Instance) => void;
+ animate: boolean;
+};
+
+// @todo: remove forwardRef if not needed
+const Node = forwardRef, NodeProps>(
+ (
+ {
+ instance,
+ selectedInstanceId,
+ selectedInstancePath,
+ level,
+ onSelect,
+ animate,
+ },
+ ref
+ ) => {
+ const [isOpen, setIsOpen] = useState(
+ selectedInstancePath.includes(instance)
+ );
+
+ useEffect(() => {
+ setIsOpen(selectedInstancePath.includes(instance));
+ }, [selectedInstancePath, instance]);
+
+ // Text nodes have only one child which is a string.
+ const showChildren =
+ instance.children.length > 1 || typeof instance.children[0] === "object";
+
+ const children = useMemo(() => {
+ if ((isOpen === false && animate === false) || showChildren === false) {
+ return null;
+ }
+ const children = [];
+ for (const child of instance.children) {
+ if (typeof child === "string") continue;
+ children.push(
+
+ );
+ }
+ return children;
+ }, [
+ instance,
+ level,
+ isOpen,
+ selectedInstanceId,
+ selectedInstancePath,
+ onSelect,
+ showChildren,
+ animate,
+ ]);
+
+ const { Icon, label } = components[instance.component];
+ const CollapsibleContent = animate
+ ? CollapsibleContentAnimated
+ : CollapsibleContentUnanimated;
+
+ return (
+
+
+ {showChildren && (
+
+ {isOpen ? : }
+
+ )}
+
+
+ {children != null && (
+ {children}
+ )}
+
+ );
+ }
+);
+
+type TreeProps = {
+ root: Instance;
+ selectedInstanceId?: Instance["id"];
onSelect?: (instance: Instance) => void;
animate?: boolean;
};
export const Tree = ({
- instance,
+ root,
selectedInstanceId,
- selectedInstancePath,
- level = 0,
- onSelect = noop,
+ onSelect = () => null,
animate = true,
}: TreeProps) => {
- const [isOpen, setIsOpen] = useState(selectedInstancePath.includes(instance));
+ const selectedInstancePath = useMemo(
+ () =>
+ selectedInstanceId !== undefined
+ ? getInstancePath(root, selectedInstanceId)
+ : [],
+ [root, selectedInstanceId]
+ );
- useEffect(() => {
- setIsOpen(selectedInstancePath.includes(instance));
- }, [selectedInstancePath, instance]);
+ const rootRef = useRef(null);
- // Text nodes have only one child which is a string.
- const showChildren =
- instance.children.length > 1 || typeof instance.children[0] === "object";
+ const dropHandlers = useDrop({
+ elementToData: (element) => {
+ const id = (element as HTMLElement).dataset.dropTargetId;
+ const instance = id && findInstanceById(root, id);
+ return instance || false;
+ },
- const children = useMemo(() => {
- if ((isOpen === false && animate === false) || showChildren === false) {
- return null;
- }
- const children = [];
- for (const child of instance.children) {
- if (typeof child === "string") continue;
- children.push(
-
+ // @todo: we need an ability to tell useDrop to keep previous drop target
+ // because once user moves the cursor out of navigator
+ // we loose the ability to meaningfully track the drop target
+ //
+ // OR: modify coordinates when passing to dropHandlers.handleMove()
+ swapDropTarget: (dropTarget) => {
+ console.log("swapDropTarget", dropTarget);
+
+ if (dropTarget) {
+ return dropTarget;
+ }
+
+ return { element: document.body as Element, data: root };
+ },
+
+ onDropTargetChange: (dropTarget) => {
+ console.log("onDropTargetChange", dropTarget);
+ },
+
+ getValidChildren: (element) => {
+ return Array.from(
+ element.querySelectorAll(":scope > div > [data-drop-target-id]")
);
- }
- return children;
- }, [
- instance,
- level,
- isOpen,
- selectedInstanceId,
- selectedInstancePath,
- onSelect,
- showChildren,
- animate,
- ]);
+ },
+ });
- const { Icon, label } = components[instance.component];
- const CollapsibleContent = animate
- ? CollapsibleContentAnimated
- : CollapsibleContentUnanimated;
+ const dragHandlers = useDrag({
+ elementToData: (element) => {
+ const dragItemElement = element.closest("[data-drag-item-id]");
+ if (!(dragItemElement instanceof HTMLElement)) {
+ return false;
+ }
+ const id = dragItemElement.dataset.dragItemId;
+ if (id === undefined || id === root.id) {
+ return false;
+ }
+ return id;
+ },
+ onStart: ({ data }) => {
+ console.log("drag start", data);
+ },
+ onMove: (point) => {
+ dropHandlers.handleMove(point);
+ },
+ onEnd: ({ isCanceled }) => {
+ console.log("drag end", isCanceled);
+ dropHandlers.handleEnd();
+ },
+ });
return (
-
-
- {showChildren && (
-
- {isOpen ? : }
-
- )}
-
-
- {children != null && {children}}
-
+ {
+ rootRef.current = element;
+ dragHandlers.rootRef(element);
+ dropHandlers.rootRef(element);
+ }}
+ >
+
+
);
};