mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-30 04:12:52 +09:00
wip
This commit is contained in:
1 parent
b40da388b3
commit
dc82b5ecbf
3 files changed
+212
-87
No files matched your search
@@ -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) => {
|
||||
<Header title="Navigator" onClose={onClose} isClosable={isClosable} />
|
||||
<Box css={{ padding: "$1" }}>
|
||||
<Tree
|
||||
instance={rootInstance}
|
||||
selectedInstancePath={selectedInstancePath}
|
||||
root={rootInstance}
|
||||
selectedInstanceId={selectedInstanceData?.id}
|
||||
onSelect={(instance) => {
|
||||
publish<"selectInstanceById", Instance["id"]>({
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<Instance>;
|
||||
level?: number;
|
||||
level: number;
|
||||
onSelect: (instance: Instance) => void;
|
||||
animate: boolean;
|
||||
};
|
||||
|
||||
// @todo: remove forwardRef if not needed
|
||||
const Node = forwardRef<ElementRef<typeof Collapsible.Root>, 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(
|
||||
<Node
|
||||
instance={child}
|
||||
selectedInstanceId={selectedInstanceId}
|
||||
selectedInstancePath={selectedInstancePath}
|
||||
level={level + 1}
|
||||
key={child.id}
|
||||
onSelect={onSelect}
|
||||
animate={animate}
|
||||
/>
|
||||
);
|
||||
}
|
||||
return children;
|
||||
}, [
|
||||
instance,
|
||||
level,
|
||||
isOpen,
|
||||
selectedInstanceId,
|
||||
selectedInstancePath,
|
||||
onSelect,
|
||||
showChildren,
|
||||
animate,
|
||||
]);
|
||||
|
||||
const { Icon, label } = components[instance.component];
|
||||
const CollapsibleContent = animate
|
||||
? CollapsibleContentAnimated
|
||||
: CollapsibleContentUnanimated;
|
||||
|
||||
return (
|
||||
<Collapsible.Root
|
||||
ref={ref}
|
||||
open={isOpen}
|
||||
onOpenChange={setIsOpen}
|
||||
data-drop-target-id={instance.id}
|
||||
>
|
||||
<Flex
|
||||
css={{
|
||||
// @todo don't hardcode the padding
|
||||
paddingLeft: level * 15 + (showChildren ? 0 : 15),
|
||||
color: "$hiContrast",
|
||||
alignItems: "center",
|
||||
}}
|
||||
>
|
||||
{showChildren && (
|
||||
<Collapsible.Trigger asChild>
|
||||
{isOpen ? <TriangleDownIcon /> : <TriangleRightIcon />}
|
||||
</Collapsible.Trigger>
|
||||
)}
|
||||
<Button
|
||||
{...(instance.id === selectedInstanceId
|
||||
? { state: "active" }
|
||||
: { ghost: true })}
|
||||
css={{ display: "flex", gap: "$1", padding: "$1" }}
|
||||
data-drag-item-id={instance.id}
|
||||
onFocus={() => {
|
||||
onSelect(instance);
|
||||
}}
|
||||
>
|
||||
<Icon />
|
||||
<Text size="1">{label}</Text>
|
||||
</Button>
|
||||
</Flex>
|
||||
{children != null && (
|
||||
<CollapsibleContent>{children}</CollapsibleContent>
|
||||
)}
|
||||
</Collapsible.Root>
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
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<HTMLElement | null>(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<Instance>({
|
||||
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(
|
||||
<Tree
|
||||
instance={child}
|
||||
selectedInstanceId={selectedInstanceId}
|
||||
selectedInstancePath={selectedInstancePath}
|
||||
level={level + 1}
|
||||
key={child.id}
|
||||
onSelect={onSelect}
|
||||
animate={animate}
|
||||
/>
|
||||
// @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<Instance["id"]>({
|
||||
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 (
|
||||
<Collapsible.Root open={isOpen} onOpenChange={setIsOpen}>
|
||||
<Flex
|
||||
css={{
|
||||
// @todo don't hardcode the padding
|
||||
paddingLeft: level * 15 + (showChildren ? 0 : 15),
|
||||
color: "$hiContrast",
|
||||
alignItems: "center",
|
||||
}}
|
||||
>
|
||||
{showChildren && (
|
||||
<Collapsible.Trigger asChild>
|
||||
{isOpen ? <TriangleDownIcon /> : <TriangleRightIcon />}
|
||||
</Collapsible.Trigger>
|
||||
)}
|
||||
<Button
|
||||
{...(instance.id === selectedInstanceId
|
||||
? { state: "active" }
|
||||
: { ghost: true })}
|
||||
css={{ display: "flex", gap: "$1", padding: "$1" }}
|
||||
onFocus={() => {
|
||||
onSelect(instance);
|
||||
}}
|
||||
>
|
||||
<Icon />
|
||||
<Text size="1">{label}</Text>
|
||||
</Button>
|
||||
</Flex>
|
||||
{children != null && <CollapsibleContent>{children}</CollapsibleContent>}
|
||||
</Collapsible.Root>
|
||||
<Box
|
||||
ref={(element) => {
|
||||
rootRef.current = element;
|
||||
dragHandlers.rootRef(element);
|
||||
dropHandlers.rootRef(element);
|
||||
}}
|
||||
>
|
||||
<Node
|
||||
instance={root}
|
||||
selectedInstanceId={selectedInstanceId}
|
||||
selectedInstancePath={selectedInstancePath}
|
||||
onSelect={onSelect}
|
||||
animate={animate}
|
||||
level={0}
|
||||
/>
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user