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
cleanup & restore test
Roman Pominov01b9e4a24e91
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 007c8004d3..2bfea63e17 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,44 +1,10 @@-import { Flex, Tree, TreeNodeLabel } from "@webstudio-is/design-system";-import {- type Instance,- type Publish,- components,-} from "@webstudio-is/react-sdk";+import { Flex } from "@webstudio-is/design-system";+import { type Instance, type Publish } from "@webstudio-is/react-sdk";import { useCallback } from "react";import { useSelectedInstanceData } from "~/designer/shared/nano-states";import { useRootInstance } from "~/shared/nano-states";-import {- findInstanceById,- getInstancePath,- getInstancePathWithPositions,-} from "~/shared/tree-utils";import { Header } from "../header";--const tmp = {- findItemById: findInstanceById,- getItemPath: getInstancePath,- getItemPathWithPositions: getInstancePathWithPositions,- canBeReparented(item: Instance) {- return components[item.component].isInlineOnly !== true;- },- canAcceptChild(item: Instance) {- return components[item.component].canAcceptChildren;- },- getItemChildren(item: Instance) {- return item.children.filter(- (child) => typeof child !== "string"- ) as Instance[];- },- renderItem(props: { data: Instance; isSelected: boolean }) {- const { Icon, label } = components[props.data.component];- return (- <>- <Icon />- <TreeNodeLabel isSelected={props.isSelected} text={label} withIcon />- </>- );- },-};+import { InstanceTree } from "~/designer/shared/tree";type NavigatorProps = {publish: Publish;@@ -98,8 +64,7 @@ export const Navigator = ({ publish, isClosable, onClose }: NavigatorProps) => {<Flex css={{ height: "100%", flexDirection: "column" }}><Header title="Navigator" onClose={onClose} isClosable={isClosable} /><Flex css={{ flexGrow: 1, flexDirection: "column" }}>- <Tree- {...tmp}+ <InstanceTreeroot={rootInstance}selectedItemId={selectedInstanceData?.id}onSelect={handleSelect}diff --git a/apps/designer/app/designer/features/tree-preview/tree-preview.tsx b/apps/designer/app/designer/features/tree-preview/tree-preview.tsxindex 83f63fcaf3..eb6c4365a4 100644--- a/apps/designer/app/designer/features/tree-preview/tree-preview.tsx+++ b/apps/designer/app/designer/features/tree-preview/tree-preview.tsx@@ -1,7 +1,7 @@import produce from "immer";-import { components, type Instance } from "@webstudio-is/react-sdk";+import { type Instance } from "@webstudio-is/react-sdk";import { useMemo } from "react";-import { Flex, TreeNode, TreeNodeLabel } from "@webstudio-is/design-system";+import { Flex } from "@webstudio-is/design-system";import { useRootInstance, useDragAndDropState } from "~/shared/nano-states";import {reparentInstanceMutable,@@ -10,23 +10,7 @@ import {findInstanceById,getInstancePath,} from "~/shared/tree-utils";--const tmp = {- getItemChildren(item: Instance) {- return item.children.filter(- (child) => typeof child !== "string"- ) as Instance[];- },- renderItem(props: { data: Instance; isSelected: boolean }) {- const { Icon, label } = components[props.data.component];- return (- <>- <Icon />- <TreeNodeLabel isSelected={props.isSelected} text={label} withIcon />- </>- );- },-};+import { InstanceTreeNode } from "~/designer/shared/tree";export const TreePrevew = () => {const [rootInstance] = useRootInstance();@@ -91,7 +75,7 @@ export const TreePrevew = () => {return (treeProps && (<Flex direction="column" css={{ pt: "$1", pb: "$1", width: "100%" }}>- <TreeNode {...tmp} {...treeProps} />+ <InstanceTreeNode {...treeProps} /></Flex>));diff --git a/apps/designer/app/designer/shared/tree/index.tsx b/apps/designer/app/designer/shared/tree/index.tsxnew file mode 100644index 0000000000..f47c5e3247--- /dev/null+++ b/apps/designer/app/designer/shared/tree/index.tsx@@ -0,0 +1,59 @@+import {+ Tree,+ TreeNodeLabel,+ TreeNode,+ type TreeNodeProps,+ type TreeProps,+} from "@webstudio-is/design-system";+import { components, Instance } from "@webstudio-is/react-sdk";+import {+ findInstanceById,+ getInstancePath,+ getInstancePathWithPositions,+} from "~/shared/tree-utils";++const instanceRelatedProps = {+ findItemById: findInstanceById,+ getItemPath: getInstancePath,+ getItemPathWithPositions: getInstancePathWithPositions,+ canLeaveParent(item: Instance) {+ return components[item.component].isInlineOnly !== true;+ },+ canAcceptChild(item: Instance) {+ return components[item.component].canAcceptChildren;+ },+ getItemChildren(item: Instance) {+ // Only content editable components can have `string` children+ // and this cab be a hot path for performance.+ if (components[item.component].isContentEditable === false) {+ return item.children as Instance[];+ }++ return item.children.filter(+ (child) => typeof child !== "string"+ ) as Instance[];+ },+ renderItem(props: { data: Instance; isSelected: boolean }) {+ const { Icon, label } = components[props.data.component];+ return (+ <>+ <Icon />+ <TreeNodeLabel isSelected={props.isSelected} text={label} withIcon />+ </>+ );+ },+} as const;++export const InstanceTree = (+ props: Omit<TreeProps<Instance>, keyof typeof instanceRelatedProps>+) => <Tree {...props} {...instanceRelatedProps} />;++export const InstanceTreeNode = (+ props: Omit<TreeNodeProps<Instance>, "getItemChildren" | "renderItem">+) => (+ <TreeNode+ {...props}+ getItemChildren={instanceRelatedProps.getItemChildren}+ renderItem={instanceRelatedProps.renderItem}+ />+);diff --git a/packages/design-system/src/components/tree/horizontal-shift.test.ts b/packages/design-system/src/components/tree/horizontal-shift.test.tsnew file mode 100644index 0000000000..5e4b0faf6b--- /dev/null+++ b/packages/design-system/src/components/tree/horizontal-shift.test.ts@@ -0,0 +1,333 @@+import { renderHook, act } from "@testing-library/react-hooks";+import { DropTarget, Placement } from "@webstudio-is/design-system";+import { useHorizontalShift } from "./horizontal-shift";++type Item = {+ id: string;+ canAcceptChildren: boolean;+ children: Item[];+};++const box1: Item = { canAcceptChildren: true, id: "box1", children: [] };+const box2: Item = { canAcceptChildren: true, id: "box2", children: [] };+const box321: Item = { canAcceptChildren: true, id: "box321", children: [] };+const box32: Item = {+ canAcceptChildren: true,+ children: [box321],+ id: "box32",+};+const box31: Item = { canAcceptChildren: true, id: "box31", children: [] };+const box3: Item = {+ canAcceptChildren: true,+ children: [box31, box32],+ id: "box3",+};+const box4: Item = { canAcceptChildren: true, id: "box4", children: [] };+const heading: Item = {+ canAcceptChildren: false,+ children: [{ canAcceptChildren: false, id: "bold", children: [] }],+ id: "heading",+};+const box511: Item = { canAcceptChildren: true, id: "box511", children: [] };+const box51: Item = {+ canAcceptChildren: true,+ id: "box51",+ children: [box511],+};+const box52: Item = { canAcceptChildren: true, id: "box52", children: [] };+const box5: Item = {+ canAcceptChildren: true,+ id: "box5",+ children: [box51, box52],+};++// root+// | box1+// | box2+// | box3+// | | box31+// | | box32+// | | | box321+// | box4+// | heading+// | | bold+// | box5+// | | box51+// | | | box511+// | | box52+const tree: Item = {+ id: "root",+ canAcceptChildren: true,+ children: [box1, box2, box3, box4, heading, box5],+};++const makePlacement = (+ type: Placement["type"] = "next-to-child"+): Placement => ({+ type,+ direction: "horizontal",+ x: 0,+ y: 100,+ length: 300,+});++const makeDrop = ({+ into,+ after,+ placement = makePlacement(),+}: {+ into: Item;+ after?: Item;+ placement?: Placement;+}): DropTarget<Item> => ({+ data: into,+ indexWithinChildren:+ after === undefined ? 0 : into.children.indexOf(after) + 1,+ placement,++ // not used+ element: null as unknown as HTMLElement,+ rect: null as unknown as DOMRect,+});++const getItemPathWithPositions = (tree: Item, itemId: string) => {+ const path = [];++ const find = (item: Item) => {+ if (item.id === itemId) return true;+ const children = item.children;+ for (let i = 0; i < children.length; i++) {+ const child = children[i];+ if (typeof child === "string") continue;+ const found = find(child);+ if (found) {+ path.push({ item: child, position: i });+ return true;+ }+ }+ };++ if (find(tree)) {+ path.push({ item: tree, position: 0 });+ }++ return path.reverse();+};++const render = (+ {+ dragItem,+ dropTarget,+ }: {+ dragItem: Item | undefined;+ dropTarget: DropTarget<Item> | undefined;+ },+ shift = -1+) => {+ const { result } = renderHook((props) => useHorizontalShift<Item>(props), {+ initialProps: {+ dragItem,+ dropTarget,+ root: tree,+ getIsExpanded: (item: Item) => item.children.length > 0,+ canAcceptChild: (item: Item) => item.canAcceptChildren,+ getItemChildren: (item: Item) => item.children,+ getItemPath: (tree: Item, id: string) =>+ getItemPathWithPositions(tree, id).map(({ item }) => item),+ getItemPathWithPositions,+ },+ });++ act(() => {+ const [, setHorizontalShift] = result.current;+ setHorizontalShift(shift);+ });++ const [shiftedDroptTarget] = result.current;+ return shiftedDroptTarget;+};++test("if there's no dragItem or dropTarget returns undefined", () => {+ const result = render({+ dragItem: undefined,+ dropTarget: undefined,+ });+ expect(result).toBeUndefined();+});++test("drop target is empty or collapsed", () => {+ const dropTarget = makeDrop({+ into: box2,+ placement: makePlacement("inside-parent"),+ });+ const result = render({ dragItem: box1, dropTarget });++ // parent doesn't change+ expect(result?.item.id).toBe(box2.id);++ // placement line geometry isn't provided+ expect(result?.placement).toBeUndefined();+});++test("placement line coordinates are always adjusted", () => {+ const result = render(+ { dragItem: box31, dropTarget: makeDrop({ into: box3, after: box32 }) },+ 0+ );++ // parent doesn't change+ expect(result?.item.id).toBe(box3.id);++ // placement line is adjusted to account for depth,+ // even though depth didn't change+ expect(result?.placement).toEqual({+ ...makePlacement(),+ length: 260,+ x: 40,+ });+});++test("shifting is relative to the drag item's depth", () => {+ // same shift and drop target+ const shift = 0;+ const dropTarget = makeDrop({ into: tree, after: box3 });++ // box2's depth is 1, so it's inserted without shifting+ const result1 = render({ dragItem: box2, dropTarget }, shift);+ expect(result1?.item.id).toBe(tree.id);++ // box31's depth is 2, so it's inserted with shifting, to maintain the same depth+ const result2 = render({ dragItem: box31, dropTarget }, shift);+ expect(result2?.item.id).toBe(box3.id);+ expect(result2?.position).toBe("end");+});++describe("shifting to the left", () => {+ test("if we're already at the root, no shift occurs", () => {+ const result = render(+ { dragItem: box1, dropTarget: makeDrop({ into: tree, after: box2 }) },+ -1+ );+ expect(result?.item.id).toBe(tree.id);+ });++ test("a shifting is possible only when we're at the bottom of the initial drop target", () => {+ const result1 = render(+ { dragItem: box1, dropTarget: makeDrop({ into: box3, after: box31 }) },+ -1+ );+ expect(result1?.item.id).toBe(box3.id);+ });++ test("when above the drag item, which is at the bottom, the shift is allowed", () => {+ const result = render(+ { dragItem: box32, dropTarget: makeDrop({ into: box3, after: box31 }) },+ -1+ );+ expect(result?.item.id).toBe(tree.id);+ expect(result?.position).toBe(tree.children.indexOf(box3) + 1);+ expect(result?.placement).toEqual({+ ...makePlacement(),+ x: 24,+ length: 276,+ });+ });++ test("shifting by different ammounts works correctly", () => {+ const args = {+ dragItem: box1,+ dropTarget: makeDrop({ into: box32, after: box321 }),+ };++ const result1 = render(args, 0);+ expect(result1?.item.id).toBe(tree.id);+ expect(result1?.position).toBe(tree.children.indexOf(box3) + 1);+ expect(result1?.placement).toEqual({+ ...makePlacement(),+ x: 24,+ length: 276,+ });++ const result2 = render(args, 1);+ expect(result2?.item.id).toBe(box3.id);+ expect(result2?.position).toBe(box3.children.indexOf(box32) + 1);+ expect(result2?.placement).toEqual({+ ...makePlacement(),+ x: 40,+ length: 260,+ });+ });++ test('the "at the bottom" check is perforamed at every step, dissalowing futher shifting when we\'re no longer at the bottom', () => {+ const result = render(+ {+ dragItem: box1,+ dropTarget: makeDrop({ into: box51, after: box511 }),+ },+ -10+ );+ expect(result?.item.id).toBe(box5.id);+ expect(result?.position).toBe(box5.children.indexOf(box51) + 1);+ });+});++describe("shifting to the right ", () => {+ test("if there's no expanded item above, no shift occurs", () => {+ const result = render(+ { dragItem: box1, dropTarget: makeDrop({ into: tree, after: box2 }) },+ 1+ );+ expect(result?.item.id).toBe(tree.id);+ });++ test("if there's an expanded item above, we shift inside it", () => {+ const args = {+ dragItem: box1,+ dropTarget: makeDrop({ into: tree, after: box3 }),+ };++ const result1 = render(args, 1);+ expect(result1?.item.id).toBe(box3.id);++ const result2 = render(args, 2);+ expect(result2?.item.id).toBe(box32.id);+ });++ test("you cannot move inside drag item by shifting", () => {+ // drag item is right above, no shifting+ const result1 = render(+ { dragItem: box3, dropTarget: makeDrop({ into: tree, after: box3 }) },+ 3+ );+ expect(result1?.item.id).toBe(tree.id);++ // drag item is inside the item above+ // we shift into the item above, but not inside the drag item+ const result2 = render(+ { dragItem: box32, dropTarget: makeDrop({ into: tree, after: box3 }) },+ 3+ );+ expect(result2?.item.id).toBe(box3.id);+ expect(result2?.position).toBe("end");+ });++ test("when the item above is the drag item itself, we consider the item above it as a potential target", () => {+ const result = render(+ { dragItem: box4, dropTarget: makeDrop({ into: tree, after: box4 }) },+ 1+ );+ expect(result?.item.id).toBe(box3.id);+ expect(result?.position).toBe("end");+ });++ test("if there's an expanded item above, but it cannot accept shildren, no shift occurs", () => {+ const result = render(+ {+ dragItem: box1,+ dropTarget: makeDrop({ into: tree, after: heading }),+ },+ 1+ );+ expect(result?.item.id).toBe(tree.id);+ });+});diff --git a/packages/design-system/src/components/tree/horizontal-shift.ts b/packages/design-system/src/components/tree/horizontal-shift.tsnew file mode 100644index 0000000000..f2b8ab227f--- /dev/null+++ b/packages/design-system/src/components/tree/horizontal-shift.ts@@ -0,0 +1,179 @@+import { useMemo, useState } from "react";+import { DropTarget, Placement } from "../primitives/dnd";+import { getPlacementIndicatorAlignment } from "./tree-node";++export type ShiftedDropTarget<Data> = {+ item: Data;+ position: number | "end";+ placement?: Placement;+};++export const useHorizontalShift = <Data extends { id: string }>({+ dragItem,+ dropTarget,+ root,+ getIsExpanded,+ getItemPath,+ getItemPathWithPositions,+ canAcceptChild,+ getItemChildren,+}: {+ getItemChildren: (item: Data) => Data[];+ canAcceptChild: (item: Data) => boolean;+ getItemPath: (root: Data, id: string) => Data[];+ getItemPathWithPositions: (+ root: Data,+ id: string+ ) => Array<{ item: Data; position: number }>;+ dragItem: Data | undefined;+ dropTarget: DropTarget<Data> | undefined;+ root: Data;+ getIsExpanded: (item: Data) => boolean;+}) => {+ const [horizontalShift, setHorizontalShift] = useState(0);++ const dragItemDepth = useMemo(+ () => dragItem && getItemPath(root, dragItem.id).length - 1,+ [dragItem, root, getItemPath]+ );++ // Here we want to allow user to shift placement line horizontally+ // but only if that corresponds to a meaningful position in the tree+ const shiftedDropTarget = useMemo<ShiftedDropTarget<Data> | undefined>(() => {+ if (+ dropTarget === undefined ||+ dragItemDepth === undefined ||+ dragItem === undefined+ ) {+ return undefined;+ }++ const { data, placement, indexWithinChildren } = dropTarget;++ const shiftPlacement = (depth: number) => {+ const shift = getPlacementIndicatorAlignment(depth);+ return {+ ...placement,+ x: placement.x + shift,+ length: placement.length - shift,+ };+ };++ // Placement type “inside-parent” means that useDrop() didn’t find any children.+ // In this case the placement line coordinates are meaningless in the context of the tree.+ // We're dropping the placement and not performing any shifting.+ if (placement.type === "inside-parent") {+ return { item: data, position: "end" };+ }++ const dropTargetPath = getItemPathWithPositions(root, data.id);+ dropTargetPath.reverse();++ const currentDepth = dropTargetPath.length;+ const desiredDepth = dragItemDepth + horizontalShift;++ const withoutShift = {+ item: data,+ position: indexWithinChildren,+ placement: shiftPlacement(currentDepth),+ } as const;++ const isDragItem = (item: Data | undefined) =>+ typeof item === "object" && item.id === dragItem.id;++ if (desiredDepth < currentDepth) {+ let shifted = 0;+ let newParent = data;+ let newPosition = indexWithinChildren;++ const isAtTheBottom = (parent: Data, index: number) => {+ const children = getItemChildren(parent);++ // There's a special case when the placement line is above the drag item.+ // For reparenting, above and below the drag item means the same.+ const indexCorrected = isDragItem(children[index]) ? index + 1 : index;+ return indexCorrected === children.length;+ };++ let potentialNewParent = dropTargetPath[shifted + 1];++ while (+ isAtTheBottom(newParent, newPosition) &&+ typeof potentialNewParent === "object" &&+ canAcceptChild(potentialNewParent.item) &&+ shifted < currentDepth - desiredDepth+ ) {+ shifted++;+ newPosition = dropTargetPath[shifted - 1].position + 1;+ newParent = potentialNewParent.item;+ potentialNewParent = dropTargetPath[shifted + 1];+ }++ if (shifted === 0) {+ return withoutShift;+ }++ return {+ item: newParent,+ position: newPosition,+ placement: shiftPlacement(currentDepth - shifted),+ };+ }++ if (desiredDepth > currentDepth) {+ let shifted = 0;+ let newParent = data;++ const findNextParent = (+ parent: Data,+ position: number | "last"+ ): undefined | Data => {+ const children = getItemChildren(parent);+ const index = position === "last" ? children.length - 1 : position;++ // There's a special case when the placement line is below the drag item.+ // For reparenting, above and below the drag item means the same.+ return isDragItem(children[index])+ ? children[index - 1]+ : children[index];+ };++ let potentialNewParent = findNextParent(data, indexWithinChildren - 1);++ while (+ typeof potentialNewParent === "object" &&+ getIsExpanded(potentialNewParent) &&+ canAcceptChild(potentialNewParent) &&+ shifted < desiredDepth - currentDepth+ ) {+ newParent = potentialNewParent;+ potentialNewParent = findNextParent(newParent, "last");+ shifted++;+ }++ if (shifted === 0) {+ return withoutShift;+ }++ return {+ item: newParent,+ position: "end",+ placement: shiftPlacement(currentDepth + shifted),+ };+ }++ return withoutShift;+ }, [+ dropTarget,+ dragItemDepth,+ dragItem,+ getItemPathWithPositions,+ root,+ horizontalShift,+ canAcceptChild,+ getItemChildren,+ getIsExpanded,+ ]);++ return [shiftedDropTarget, setHorizontalShift] as const;+};diff --git a/packages/design-system/src/components/tree/tree-node.tsx b/packages/design-system/src/components/tree/tree-node.tsxindex 61de919499..91f6c2c38d 100644--- a/packages/design-system/src/components/tree/tree-node.tsx+++ b/packages/design-system/src/components/tree/tree-node.tsx@@ -131,7 +131,7 @@ const ItemWrapper = styled(Flex, {},});-type TreeNodeProps<Data extends { id: string }> = {+export type TreeNodeProps<Data extends { id: string }> = {itemData: Data;getItemChildren: (item: Data) => Data[];renderItem: (props: { data: Data; isSelected: boolean }) => React.ReactNode;diff --git a/packages/design-system/src/components/tree/tree.test.ts b/packages/design-system/src/components/tree/tree.test.tsdeleted file mode 100644index a382d8b0ea..0000000000--- a/packages/design-system/src/components/tree/tree.test.ts+++ /dev/null@@ -1,308 +0,0 @@-// import { renderHook, act } from "@testing-library/react-hooks";-// import { DropTarget, Placement } from "@webstudio-is/design-system";-// import { Instance } from "@webstudio-is/react-sdk";-// import { createInstance } from "~/shared/tree-utils";-// import { useHorizontalShift } from "./tree";--// describe("useHorizontalShift", () => {-// const box1 = createInstance({ component: "Box", id: "box1" });-// const box2 = createInstance({ component: "Box", id: "box2" });-// const box321 = createInstance({ component: "Box", id: "box321" });-// const box32 = createInstance({-// component: "Box",-// children: [box321],-// id: "box32",-// });-// const box31 = createInstance({ component: "Box", id: "box31" });-// const box3 = createInstance({-// component: "Box",-// children: [box31, box32],-// id: "box3",-// });-// const box4 = createInstance({ component: "Box", id: "box4" });-// const heading = createInstance({-// component: "Heading",-// children: [createInstance({ component: "Bold" })],-// id: "heading",-// });-// const box511 = createInstance({ component: "Box", id: "box511" });-// const box51 = createInstance({-// component: "Box",-// id: "box51",-// children: [box511],-// });-// const box52 = createInstance({ component: "Box", id: "box52" });-// const box5 = createInstance({-// component: "Box",-// id: "box5",-// children: [box51, box52],-// });--// // Body-// // | Box (box1)-// // | Box (box2)-// // | Box (box3)-// // | | Box (box31)-// // | | Box (box32)-// // | | | Box (box321)-// // | Box (box4)-// // | Heading (heading)-// // | | Bold-// // | Box (box5)-// // | | Box (box51)-// // | | | Box (box511)-// // | | Box (box52)-// const tree = createInstance({-// id: "root",-// component: "Body",-// children: [box1, box2, box3, box4, heading, box5],-// });--// const makePlacement = (-// type: Placement["type"] = "next-to-child"-// ): Placement => ({-// type,-// direction: "horizontal",-// x: 0,-// y: 100,-// length: 300,-// });--// const makeDrop = ({-// into,-// after,-// placement = makePlacement(),-// }: {-// into: Instance;-// after?: Instance;-// placement?: Placement;-// }): DropTarget<Instance> => ({-// data: into,-// indexWithinChildren:-// after === undefined ? 0 : into.children.indexOf(after) + 1,-// placement,--// // not used-// element: null as unknown as HTMLElement,-// rect: null as unknown as DOMRect,-// });--// const render = (-// {-// dragItem,-// dropTarget,-// }: {-// dragItem: Instance | undefined;-// dropTarget: DropTarget<Instance> | undefined;-// },-// shift = -1-// ) => {-// const { result } = renderHook(useHorizontalShift, {-// initialProps: {-// dragItem,-// dropTarget,-// root: tree,-// getIsExpanded: (instance) => instance.children.length > 0,-// },-// });--// act(() => {-// const [, setHorizontalShift] = result.current;-// setHorizontalShift(shift);-// });--// const [shiftedDroptTarget] = result.current;-// return shiftedDroptTarget;-// };--// test("if there's no dragItem or dropTarget returns undefined", () => {-// const result = render({-// dragItem: undefined,-// dropTarget: undefined,-// });-// expect(result).toBeUndefined();-// });--// test("drop target is empty or collapsed", () => {-// const dropTarget = makeDrop({-// into: box2,-// placement: makePlacement("inside-parent"),-// });-// const result = render({ dragItem: box1, dropTarget });--// // parent doesn't change-// expect(result?.instance.id).toBe(box2.id);--// // placement line geometry isn't provided-// expect(result?.placement).toBeUndefined();-// });--// test("placement line coordinates are always adjusted", () => {-// const result = render(-// { dragItem: box31, dropTarget: makeDrop({ into: box3, after: box32 }) },-// 0-// );--// // parent doesn't change-// expect(result?.instance.id).toBe(box3.id);--// // placement line is adjusted to account for depth,-// // even though depth didn't change-// expect(result?.placement).toEqual({-// ...makePlacement(),-// length: 260,-// x: 40,-// });-// });--// test("shifting is relative to the drag item's depth", () => {-// // same shift and drop target-// const shift = 0;-// const dropTarget = makeDrop({ into: tree, after: box3 });--// // box2's depth is 1, so it's inserted without shifting-// const result1 = render({ dragItem: box2, dropTarget }, shift);-// expect(result1?.instance.id).toBe(tree.id);--// // box31's depth is 2, so it's inserted with shifting, to maintain the same depth-// const result2 = render({ dragItem: box31, dropTarget }, shift);-// expect(result2?.instance.id).toBe(box3.id);-// expect(result2?.position).toBe("end");-// });--// describe("shifting to the left", () => {-// test("if we're already at the root, no shift occurs", () => {-// const result = render(-// { dragItem: box1, dropTarget: makeDrop({ into: tree, after: box2 }) },-// -1-// );-// expect(result?.instance.id).toBe(tree.id);-// });--// test("a shifting is possible only when we're at the bottom of the initial drop target", () => {-// const result1 = render(-// { dragItem: box1, dropTarget: makeDrop({ into: box3, after: box31 }) },-// -1-// );-// expect(result1?.instance.id).toBe(box3.id);-// });--// test("when above the drag item, which is at the bottom, the shift is allowed", () => {-// const result = render(-// { dragItem: box32, dropTarget: makeDrop({ into: box3, after: box31 }) },-// -1-// );-// expect(result?.instance.id).toBe(tree.id);-// expect(result?.position).toBe(tree.children.indexOf(box3) + 1);-// expect(result?.placement).toEqual({-// ...makePlacement(),-// x: 24,-// length: 276,-// });-// });--// test("shifting by different ammounts works correctly", () => {-// const args = {-// dragItem: box1,-// dropTarget: makeDrop({ into: box32, after: box321 }),-// };--// const result1 = render(args, 0);-// expect(result1?.instance.id).toBe(tree.id);-// expect(result1?.position).toBe(tree.children.indexOf(box3) + 1);-// expect(result1?.placement).toEqual({-// ...makePlacement(),-// x: 24,-// length: 276,-// });--// const result2 = render(args, 1);-// expect(result2?.instance.id).toBe(box3.id);-// expect(result2?.position).toBe(box3.children.indexOf(box32) + 1);-// expect(result2?.placement).toEqual({-// ...makePlacement(),-// x: 40,-// length: 260,-// });-// });--// test('the "at the bottom" check is perforamed at every step, dissalowing futher shifting when we\'re no longer at the bottom', () => {-// const result = render(-// {-// dragItem: box1,-// dropTarget: makeDrop({ into: box51, after: box511 }),-// },-// -10-// );-// expect(result?.instance.id).toBe(box5.id);-// expect(result?.position).toBe(box5.children.indexOf(box51) + 1);-// });-// });--// describe("shifting to the right ", () => {-// test("if there's no expanded item above, no shift occurs", () => {-// const result = render(-// { dragItem: box1, dropTarget: makeDrop({ into: tree, after: box2 }) },-// 1-// );-// expect(result?.instance.id).toBe(tree.id);-// });--// test("if there's an expanded item above, we shift inside it", () => {-// const args = {-// dragItem: box1,-// dropTarget: makeDrop({ into: tree, after: box3 }),-// };--// const result1 = render(args, 1);-// expect(result1?.instance.id).toBe(box3.id);--// const result2 = render(args, 2);-// expect(result2?.instance.id).toBe(box32.id);-// });--// test("you cannot move inside drag item by shifting", () => {-// // drag item is right above, no shifting-// const result1 = render(-// { dragItem: box3, dropTarget: makeDrop({ into: tree, after: box3 }) },-// 3-// );-// expect(result1?.instance.id).toBe(tree.id);--// // drag item is inside the item above-// // we shift into the item above, but not inside the drag item-// const result2 = render(-// { dragItem: box32, dropTarget: makeDrop({ into: tree, after: box3 }) },-// 3-// );-// expect(result2?.instance.id).toBe(box3.id);-// expect(result2?.position).toBe("end");-// });--// test("when the item above is the drag item itself, we consider the item above it as a potential target", () => {-// const result = render(-// { dragItem: box4, dropTarget: makeDrop({ into: tree, after: box4 }) },-// 1-// );-// expect(result?.instance.id).toBe(box3.id);-// expect(result?.position).toBe("end");-// });--// test("if there's an expanded item above, but it cannot accept shildren, no shift occurs", () => {-// const result = render(-// {-// dragItem: box1,-// dropTarget: makeDrop({ into: tree, after: heading }),-// },-// 1-// );-// expect(result?.instance.id).toBe(tree.id);-// });-// });-// });--export const tmp = 1;--test("tmp", () => {- expect(1).toBe(1);-});diff --git a/packages/design-system/src/components/tree/tree.tsx b/packages/design-system/src/components/tree/tree.tsxindex 9edcf1025c..71b0ad2a1c 100644--- a/packages/design-system/src/components/tree/tree.tsx+++ b/packages/design-system/src/components/tree/tree.tsx@@ -1,11 +1,10 @@import { useState, useMemo, useRef, useCallback, useEffect } from "react";import { createPortal } from "react-dom";import { useHotkeys } from "react-hotkeys-hook";-import { TreeNode, getPlacementIndicatorAlignment, INDENT } from "./tree-node";+import { TreeNode, INDENT } from "./tree-node";import { PlacementIndicator } from "./placement-indicator";import {type DropTarget,- type Placement,useHold,useDrop,useDrag,@@ -13,11 +12,12 @@ import {useDragCursor,} from "../primitives/dnd";import { Box } from "../box";+import { useHorizontalShift } from "./horizontal-shift";-type TreeProps<Data extends { id: string }> = {+export type TreeProps<Data extends { id: string }> = {root: Data;- canBeReparented: (item: Data) => boolean;+ canLeaveParent: (item: Data) => boolean;canAcceptChild: (item: Data) => boolean;findItemById: (root: Data, id: string) => Data | undefined;getItemPath: (root: Data, id: string) => Data[];@@ -41,7 +41,7 @@ type TreeProps<Data extends { id: string }> = {export const Tree = <Data extends { id: string }>({root,getItemPathWithPositions,- canBeReparented,+ canLeaveParent,canAcceptChild,findItemById,getItemPath,@@ -184,7 +184,7 @@ export const Tree = <Data extends { id: string }>({return false;}- if (canBeReparented(instance) === false) {+ if (canLeaveParent(instance) === false) {return false;}@@ -208,7 +208,7 @@ export const Tree = <Data extends { id: string }>({onDragEnd({itemId: dragItem.id,dropTarget: {- itemId: shiftedDropTarget.instance.id,+ itemId: shiftedDropTarget.item.id,position: shiftedDropTarget.position,},});@@ -273,7 +273,7 @@ export const Tree = <Data extends { id: string }>({getIsExpanded={getIsExpanded}setIsExpanded={setIsExpanded}onExpandTransitionEnd={dropHandlers.handleDomMutation}- forceHoverStateAtItem={shiftedDropTarget?.instance?.id}+ forceHoverStateAtItem={shiftedDropTarget?.item?.id}/></Box>@@ -502,179 +502,3 @@ const useExpandState = <Data extends { id: string }>({return { getIsExpanded, setIsExpanded };};--export type ShiftedDropTarget<Data> = {- instance: Data;- position: number | "end";- placement?: Placement;-};--export const useHorizontalShift = <Data extends { id: string }>({- dragItem,- dropTarget,- root,- getIsExpanded,- getItemPath,- getItemPathWithPositions,- canAcceptChild,- getItemChildren,-}: {- getItemChildren: (item: Data) => Data[];- canAcceptChild: (item: Data) => boolean;- getItemPath: (root: Data, id: string) => Data[];- getItemPathWithPositions: (- root: Data,- id: string- ) => Array<{ item: Data; position: number }>;- dragItem: Data | undefined;- dropTarget: DropTarget<Data> | undefined;- root: Data;- getIsExpanded: (instance: Data) => boolean;-}) => {- const [horizontalShift, setHorizontalShift] = useState(0);-- const dragItemDepth = useMemo(- () => dragItem && getItemPath(root, dragItem.id).length - 1,- [dragItem, root, getItemPath]- );-- // Here we want to allow user to shift placement line horizontally- // but only if that corresponds to a meaningful position in the tree- const shiftedDropTarget = useMemo<ShiftedDropTarget<Data> | undefined>(() => {- if (- dropTarget === undefined ||- dragItemDepth === undefined ||- dragItem === undefined- ) {- return undefined;- }-- const { data, placement, indexWithinChildren } = dropTarget;-- const shiftPlacement = (depth: number) => {- const shift = getPlacementIndicatorAlignment(depth);- return {- ...placement,- x: placement.x + shift,- length: placement.length - shift,- };- };-- // Placement type “inside-parent” means that useDrop() didn’t find any children.- // In this case the placement line coordinates are meaningless in the context of the tree.- // We're dropping the placement and not performing any shifting.- if (placement.type === "inside-parent") {- return { instance: data, position: "end" };- }-- const dropTargetPath = getItemPathWithPositions(root, data.id);- dropTargetPath.reverse();-- const currentDepth = dropTargetPath.length;- const desiredDepth = dragItemDepth + horizontalShift;-- const withoutShift = {- instance: data,- position: indexWithinChildren,- placement: shiftPlacement(currentDepth),- } as const;-- const isDragItem = (instance: Data | undefined) =>- typeof instance === "object" && instance.id === dragItem.id;-- if (desiredDepth < currentDepth) {- let shifted = 0;- let newParent = data;- let newPosition = indexWithinChildren;-- const isAtTheBottom = (parent: Data, index: number) => {- const children = getItemChildren(parent);-- // There's a special case when the placement line is above the drag item.- // For reparenting above and below the drag item means the same thing.- const indexCorrected = isDragItem(children[index]) ? index + 1 : index;- return indexCorrected === children.length;- };-- let potentialNewParent = dropTargetPath[shifted + 1];-- while (- isAtTheBottom(newParent, newPosition) &&- typeof potentialNewParent === "object" &&- canAcceptChild(potentialNewParent.item) &&- shifted < currentDepth - desiredDepth- ) {- shifted++;- newPosition = dropTargetPath[shifted - 1].position + 1;- newParent = potentialNewParent.item;- potentialNewParent = dropTargetPath[shifted + 1];- }-- if (shifted === 0) {- return withoutShift;- }-- return {- instance: newParent,- position: newPosition,- placement: shiftPlacement(currentDepth - shifted),- };- }-- if (desiredDepth > currentDepth) {- let shifted = 0;- let newParent = data;-- const findNextParent = (- parent: Data,- position: number | "last"- ): undefined | Data => {- const children = getItemChildren(parent);- const index = position === "last" ? children.length - 1 : position;-- // There's a special case when the placement line is below the drag item.- // For reparenting, above and below the drag item means the same thing.- return isDragItem(children[index])- ? children[index - 1]- : children[index];- };-- let potentialNewParent = findNextParent(data, indexWithinChildren - 1);-- while (- typeof potentialNewParent === "object" &&- getIsExpanded(potentialNewParent) &&- canAcceptChild(potentialNewParent) &&- shifted < desiredDepth - currentDepth- ) {- newParent = potentialNewParent;- potentialNewParent = findNextParent(newParent, "last");- shifted++;- }-- if (shifted === 0) {- return withoutShift;- }-- return {- instance: newParent,- position: "end",- placement: shiftPlacement(currentDepth + shifted),- };- }-- return withoutShift;- }, [- dropTarget,- dragItemDepth,- dragItem,- getItemPathWithPositions,- root,- horizontalShift,- canAcceptChild,- getItemChildren,- getIsExpanded,- ]);-- return [shiftedDropTarget, setHorizontalShift] as const;-};