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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
nearEdge->area & split Tree/SortableTree
Roman Pominov539a92cefbfa
diff --git a/apps/designer/app/canvas/shared/use-drag-drop.ts b/apps/designer/app/canvas/shared/use-drag-drop.tsindex 3cd73d34e7..f7ab5c16b3 100644--- a/apps/designer/app/canvas/shared/use-drag-drop.ts+++ b/apps/designer/app/canvas/shared/use-drag-drop.ts@@ -92,7 +92,7 @@ export const useDragAndDrop = () => {const path = getInstancePath(rootInstance, dropTarget.data.id);path.reverse();- if (dropTarget.nearEdge) {+ if (dropTarget.area !== "center") {path.shift();}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.tsxindex cf09350e51..0f7e651ad2 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,7 +1,7 @@import { Box } from "@webstudio-is/design-system";import type { Instance, Publish } from "@webstudio-is/react-sdk";import { useSelectedInstanceData } from "~/designer/shared/nano-states";-import { Tree } from "~/designer/shared/tree";+import { SortableTree } from "~/designer/shared/tree";import { useRootInstance } from "~/shared/nano-states";import { Header } from "../header";@@ -20,7 +20,7 @@ export const Navigator = ({ publish, isClosable, onClose }: NavigatorProps) => {<><Header title="Navigator" onClose={onClose} isClosable={isClosable} /><Box css={{ padding: "$1" }}>- <Tree+ <SortableTreeroot={rootInstance}selectedInstanceId={selectedInstanceData?.id}onSelect={(instance) => {diff --git a/apps/designer/app/designer/shared/tree/tree.tsx b/apps/designer/app/designer/shared/tree/tree.tsxindex f057db0330..f8c274c1cd 100644--- a/apps/designer/app/designer/shared/tree/tree.tsx+++ b/apps/designer/app/designer/shared/tree/tree.tsx@@ -170,28 +170,43 @@ type TreeProps = {selectedInstanceId?: Instance["id"];onSelect?: (instance: Instance) => void;animate?: boolean;-- // @todo: export a Tree component without drag and drop, to use in tree preview etc.- onDragEnd?: (event: {- instanceId: Instance["id"];- dropTarget: { instanceId: Instance["id"]; position: number };- }) => void;};-export const Tree = ({- root,- selectedInstanceId,- onSelect = () => null,- animate = true,- onDragEnd = () => null,-}: TreeProps) => {- const selectedInstancePath = useMemo(- () =>- selectedInstanceId !== undefined- ? getInstancePath(root, selectedInstanceId)- : [],- [root, selectedInstanceId]- );+export const Tree = forwardRef<ElementRef<typeof Node>, TreeProps>(+ (+ { root, selectedInstanceId, onSelect = () => null, animate = true },+ ref+ ) => {+ const selectedInstancePath = useMemo(+ () =>+ selectedInstanceId !== undefined+ ? getInstancePath(root, selectedInstanceId)+ : [],+ [root, selectedInstanceId]+ );+ return (+ <Node+ instance={root}+ selectedInstanceId={selectedInstanceId}+ selectedInstancePath={selectedInstancePath}+ onSelect={onSelect}+ animate={animate}+ level={0}+ ref={ref}+ />+ );+ }+);++export const SortableTree = (+ props: TreeProps & {+ onDragEnd: (event: {+ instanceId: Instance["id"];+ dropTarget: { instanceId: Instance["id"]; position: number };+ }) => void;+ }+) => {+ const { root, onDragEnd } = props;const rootRef = useRef<HTMLElement | null>(null);@@ -237,7 +252,7 @@ export const Tree = ({const path = getInstancePath(root, dropTarget.data.id);path.reverse();- if (dropTarget.nearEdge) {+ if (dropTarget.area === "top" || dropTarget.area === "bottom") {path.shift();}@@ -318,20 +333,14 @@ export const Tree = ({return (<>- <Node- instance={root}- selectedInstanceId={selectedInstanceId}- selectedInstancePath={selectedInstancePath}- onSelect={onSelect}- animate={animate}- level={0}+ <Tree+ {...props}ref={(element) => {rootRef.current = element;dragHandlers.rootRef(element);dropHandlers.rootRef(element);}}/>-{/* @todo: need to render an outline instead of the line when there're no children */}{/* @todo: need to adjust line length according to the depth of the drop target inside the tree */}{dropTarget && (diff --git a/packages/design-system/src/components/primitives/dnd/canvas.stories.tsx b/packages/design-system/src/components/primitives/dnd/canvas.stories.tsxindex 6843068802..0e835d4746 100644--- a/packages/design-system/src/components/primitives/dnd/canvas.stories.tsx+++ b/packages/design-system/src/components/primitives/dnd/canvas.stories.tsx@@ -211,7 +211,7 @@ export const Canvas = () => {return getDefaultDropTarget();}- const { data: id, nearEdge } = dropTarget;+ const { data: id, area } = dropTarget;if (id === ROOT_ID) {return dropTarget;@@ -219,7 +219,7 @@ export const Canvas = () => {const path = findItemPath(data, id) ?? [];- if (nearEdge) {+ if (area !== "center") {path.shift();}diff --git a/packages/design-system/src/components/primitives/dnd/geometry-utils.ts b/packages/design-system/src/components/primitives/dnd/geometry-utils.tsindex 8f3a1e555c..f1a24f0f02 100644--- a/packages/design-system/src/components/primitives/dnd/geometry-utils.ts+++ b/packages/design-system/src/components/primitives/dnd/geometry-utils.ts@@ -18,6 +18,8 @@ export type Placement = {direction: "horizontal" | "vertical";};+export type Area = "top" | "bottom" | "left" | "right" | "center";+// https://stackoverflow.com/a/18157551/478603const getDistanceToRect = (rect: Rect, { x, y }: Point) => {const dx = Math.max(rect.left - x, 0, x - (rect.left + rect.width));@@ -45,17 +47,25 @@ export const isEqualRect = (a: Rect | undefined, b: Rect) =>a.width === b.width &&a.height === b.height;-export const isNearEdge = (+export const getArea = ({ x, y }: Point,edgeDistanceThreshold: number,rect: Rect-) =>- Math.min(- y - rect.top,- rect.top + rect.height - y,- x - rect.left,- rect.left + rect.width - x- ) <= edgeDistanceThreshold;+): Area => {+ if (x - rect.left <= edgeDistanceThreshold) {+ return "left";+ }+ if (rect.left + rect.width - x <= edgeDistanceThreshold) {+ return "right";+ }+ if (y - rect.top <= edgeDistanceThreshold) {+ return "top";+ }+ if (rect.top + rect.height - y <= edgeDistanceThreshold) {+ return "bottom";+ }+ return "center";+};export const getPlacementBetween = (a: Rect | undefined,diff --git a/packages/design-system/src/components/primitives/dnd/use-drop.ts b/packages/design-system/src/components/primitives/dnd/use-drop.tsindex a85e91dc09..6d029bfe4b 100644--- a/packages/design-system/src/components/primitives/dnd/use-drop.ts+++ b/packages/design-system/src/components/primitives/dnd/use-drop.ts@@ -1,7 +1,7 @@import { useRef, useMemo } from "react";import {isEqualRect,- isNearEdge,+ getArea,getClosestRectIndex,getPlacementBetween,getPlacementInside,@@ -10,6 +10,7 @@ import {type Rect,type Point,type Placement,+ type Area,} from "./geometry-utils";import { getLocalChildrenOrientation, getChildrenRects } from "./dom-utils";@@ -33,7 +34,7 @@ export type UseDropProps<Data> = {// To check that the element can qualify as a targetelementToData: (target: Element) => Data | false;- // Distance from an edge to set nearEdge to true in swapDropTarget+ // Distance from an edge to determine "area" argument for swapDropTargetedgeDistanceThreshold?: number;// Given the potential target that has passed the elementToData check,@@ -41,7 +42,7 @@ export type UseDropProps<Data> = {// you can swap to another targetswapDropTarget: (// undefined is passed when no suitable element is found under the pointer- dropTarget: (PartialDropTarget<Data> & { nearEdge: boolean }) | undefined+ dropTarget: (PartialDropTarget<Data> & { area: Area }) | undefined) => PartialDropTarget<Data>;onDropTargetChange: (dropTarget: DropTarget<Data>) => void;@@ -81,7 +82,7 @@ const getInitialState = <Data>() => {dropTarget: undefined as DropTarget<Data> | undefined,childrenRectsCache: new WeakMap<Element, Rect[]>(),lastCandidateElement: undefined as Element | undefined,- lastCandidateIsNearEdge: undefined as boolean | undefined,+ lastCandidateArea: undefined as Area | undefined,};};@@ -124,8 +125,6 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {const childrenRects = getChildrenRectsMemoized(partialDropTarget.element);- console.log({ parentRect, pointerRelativeToParent, childrenRects });-const closestChildIndex =childrenRects.length === 0? 0@@ -226,24 +225,21 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {const isNewCandidate =candidate?.element !== state.current.lastCandidateElement;state.current.lastCandidateElement = candidate?.element;- const candidateIsNearEdge =+ const candidateArea =candidate && pointerCoordinates- ? isNearEdge(+ ? getArea(pointerCoordinates,edgeDistanceThreshold,candidate.element.getBoundingClientRect()): undefined;- const isNewIsNearEdge =- candidateIsNearEdge !== state.current.lastCandidateIsNearEdge;- state.current.lastCandidateIsNearEdge = candidateIsNearEdge;+ const isNewArea = candidateArea !== state.current.lastCandidateArea;+ state.current.lastCandidateArea = candidateArea;if (isNewCandidate === false &&- isNewIsNearEdge === false &&+ isNewArea === false &&state.current.dropTarget) {- console.log("skipping swap", state.current.dropTarget);-// Still need to call setDropTarget to update rect and/or placement.// Because indexWithinChildren might have changed,// or parent coordinates might have moved in case of a scroll@@ -257,7 +253,7 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {candidate && pointerCoordinates? {...candidate,- nearEdge: isNearEdge(+ area: getArea(pointerCoordinates,edgeDistanceThreshold,candidate.element.getBoundingClientRect()