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.

tests for the horizontal shift (wip)

Roman Pominov249de9555a13
diff --git a/apps/designer/app/designer/shared/tree/tree.test.ts b/apps/designer/app/designer/shared/tree/tree.test.ts
new file mode 100644
index 0000000000..3909e6a689
--- /dev/null
+++ b/apps/designer/app/designer/shared/tree/tree.test.ts
@@ -0,0 +1,90 @@
+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" });
+ const box2 = createInstance({ component: "Box" });
+
+ const tree = createInstance({
+ component: "Box",
+ children: [box1, box2],
+ });
+
+ const makeDropTarget = (
+ instance: Instance,
+ indexWithinChildren: number,
+ placement: Placement
+ ): DropTarget<Instance> => ({
+ element: null as unknown as HTMLElement, // element is not used
+ data: instance,
+ rect: {
+ top: Math.random(),
+ left: Math.random(),
+ width: Math.random(),
+ height: Math.random(),
+ } as DOMRect,
+ indexWithinChildren,
+ placement,
+ });
+
+ const render = (
+ {
+ dragItem,
+ dropTarget,
+ }: {
+ dragItem: Instance | undefined;
+ dropTarget: DropTarget<Instance> | undefined;
+ },
+ shift = -1
+ ) => {
+ const { result } = renderHook(useHorizontalShift, {
+ initialProps: {
+ dragItem,
+ dropTarget,
+ root: tree,
+ getIsExpanded: () => true,
+ },
+ });
+
+ act(() => {
+ const [, setHorizontalShift] = result.current;
+ setHorizontalShift(shift);
+ });
+
+ const [shiftedDropTarget] = result.current;
+
+ return shiftedDropTarget;
+ };
+
+ test("if there's no dragItem or dropTarget returns undefined", () => {
+ const shiftedDropTarget = render({
+ dragItem: undefined,
+ dropTarget: undefined,
+ });
+ expect(shiftedDropTarget).toBeUndefined();
+ });
+
+ test("drop target is empty or collapsed", () => {
+ const dropTarget = makeDropTarget(box2, 0, {
+ type: "inside-parent",
+ direction: "horizontal",
+ x: 0,
+ y: 0,
+ length: 0,
+ });
+
+ const shiftedDropTarget = render({ dragItem: box1, dropTarget });
+
+ // parent doesn't change
+ expect(shiftedDropTarget?.instance).toEqual(box2);
+
+ // placement is the original rect
+ expect(shiftedDropTarget?.placement).toEqual({
+ type: "rect",
+ rect: dropTarget.rect,
+ });
+ });
+});
diff --git a/apps/designer/app/designer/shared/tree/tree.tsx b/apps/designer/app/designer/shared/tree/tree.tsx
index ff1f868b92..10cca461f5 100644
--- a/apps/designer/app/designer/shared/tree/tree.tsx
+++ b/apps/designer/app/designer/shared/tree/tree.tsx
@@ -63,153 +63,12 @@ export const Tree = ({
const [dragItem, setDragItem] = useState<Instance>();
const [dropTarget, setDropTarget] = useState<DropTarget<Instance>>();
- const [horizontalShift, setHorizontalShift] = useState(0);
-
- const dragItemDepth = useMemo(
- () => dragItem && getInstancePath(root, dragItem.id).length - 1,
- [dragItem, root]
- );
-
- // Here we want to allow user to shift placement line horizontally
- // but only if that corresponds to a meaningful position in the tree
- const finalDropTarget = useMemo<
- | {
- instance: Instance;
- position: number | "end";
- placement:
- | { type: "rect"; rect: Rect }
- | { type: "line"; placement: Placement };
- }
- | undefined
- >(() => {
- if (
- dropTarget === undefined ||
- dragItemDepth === undefined ||
- dragItem === undefined
- ) {
- return undefined;
- }
-
- const { data, placement, rect, indexWithinChildren } = dropTarget;
-
- // Placement type “inside-parent” means that useDrop didn’t find any children.
- // This means the drop target is empty or collapsed.
- // In this case, we want to show a rect instead of a line.
- // Also, we don’t want to apply shift, as there’s no line to shift.
- if (placement.type === "inside-parent") {
- return {
- instance: data,
- position: "end",
- placement: { type: "rect", rect: rect },
- };
- }
-
- const dropTargetPath = getInstancePathWithPositions(root, data.id);
- dropTargetPath.reverse();
-
- const currentDepth = dropTargetPath.length;
- const desiredDepth = dragItemDepth + horizontalShift;
-
- const withoutShift = {
- instance: data,
- position: indexWithinChildren,
- placement: {
- type: "line",
- placement: shiftPlacement(placement, currentDepth),
- },
- } as const;
-
- const isDragItem = (instance: Instance | undefined | string) =>
- typeof instance === "object" && instance.id === dragItem.id;
-
- if (desiredDepth < currentDepth) {
- // 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(data.children[indexWithinChildren])
- ? indexWithinChildren + 1
- : indexWithinChildren;
-
- // Unless we're currently at the bottom of drop target's children,
- // decreasing depth will not correspond to a new meaningful position
- if (indexCorrected !== data.children.length) {
- return withoutShift;
- }
-
- const difference = Math.min(
- dropTargetPath.length - 1,
- currentDepth - desiredDepth
- );
-
- if (difference === 0) {
- return withoutShift;
- }
-
- // Ideally we should check canAcceptChildren on the new target
- // but we assume that because it already has a child, it can accept more.
-
- return {
- instance: dropTargetPath[difference].instance,
- position: dropTargetPath[difference - 1].position + 1,
- placement: {
- type: "line",
- placement: shiftPlacement(placement, currentDepth - difference),
- },
- };
- }
-
- if (desiredDepth > currentDepth) {
- let shifted = 0;
- let newParent = data;
-
- // 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.
- const getChildAbove = (
- parent: Instance,
- index: number
- ): string | undefined | Instance =>
- isDragItem(parent.children[index - 1])
- ? parent.children[index - 2]
- : parent.children[index - 1];
-
- let potentialNewParent = getChildAbove(data, indexWithinChildren);
-
- while (
- typeof potentialNewParent === "object" &&
- getIsExpanded(potentialNewParent) &&
- components[potentialNewParent.component].canAcceptChild() &&
- shifted < desiredDepth - currentDepth
- ) {
- newParent = potentialNewParent;
- potentialNewParent = getChildAbove(
- newParent,
- newParent.children.length
- );
- shifted++;
- }
-
- if (shifted === 0) {
- return withoutShift;
- }
-
- return {
- instance: newParent,
- position: "end",
- placement: {
- type: "line",
- placement: shiftPlacement(placement, currentDepth + shifted),
- },
- };
- }
-
- return withoutShift;
- }, [
+ const [shiftedDropTarget, setHorizontalShift] = useHorizontalShift({
dragItem,
- dragItemDepth,
dropTarget,
root,
- horizontalShift,
getIsExpanded,
- ]);
+ });
const getFallbackDropTarget = () => {
const element = rootNodeRef.current?.querySelector(
@@ -340,12 +199,12 @@ export const Tree = ({
setHorizontalShift(shifts);
},
onEnd: ({ isCanceled }) => {
- if (finalDropTarget && dragItem && isCanceled === false) {
+ if (shiftedDropTarget && dragItem && isCanceled === false) {
onDragEnd({
instanceId: dragItem.id,
dropTarget: {
- instanceId: finalDropTarget.instance.id,
- position: finalDropTarget.position,
+ instanceId: shiftedDropTarget.instance.id,
+ position: shiftedDropTarget.position,
},
});
}
@@ -411,13 +270,15 @@ export const Tree = ({
disableHoverStates={dragItem !== undefined}
/>
</Box>
- {finalDropTarget &&
+ {shiftedDropTarget &&
createPortal(
- finalDropTarget.placement.type === "rect" ? (
- <PlacementIndicatorOutline rect={finalDropTarget.placement.rect} />
+ shiftedDropTarget.placement.type === "rect" ? (
+ <PlacementIndicatorOutline
+ rect={shiftedDropTarget.placement.rect}
+ />
) : (
<PlacementIndicatorLine
- placement={finalDropTarget.placement.placement}
+ placement={shiftedDropTarget.placement.placement}
/>
),
document.body
@@ -631,11 +492,172 @@ const useExpandState = ({
return { getIsExpanded, setIsExpanded };
};
-const shiftPlacement = (placement: Placement, depth: number) => {
- const shift = getPlacementIndicatorAlignment(depth);
- return {
- ...placement,
- x: placement.x + shift,
- length: placement.length - shift,
- };
+type ShiftedDropTarget = {
+ instance: Instance;
+ position: number | "end";
+ placement:
+ | { type: "rect"; rect: Rect }
+ | { type: "line"; placement: Placement };
+};
+
+export const useHorizontalShift = ({
+ dragItem,
+ dropTarget,
+ root,
+ getIsExpanded,
+}: {
+ dragItem: Instance | undefined;
+ dropTarget: DropTarget<Instance> | undefined;
+ root: Instance;
+ getIsExpanded: (instance: Instance) => boolean;
+}) => {
+ const [horizontalShift, setHorizontalShift] = useState(0);
+
+ const dragItemDepth = useMemo(
+ () => dragItem && getInstancePath(root, dragItem.id).length - 1,
+ [dragItem, root]
+ );
+
+ // 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 | undefined>(() => {
+ if (
+ dropTarget === undefined ||
+ dragItemDepth === undefined ||
+ dragItem === undefined
+ ) {
+ return undefined;
+ }
+
+ const { data, placement, rect, 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.
+ // This means the drop target is empty or collapsed.
+ // In this case, we want to show a rect instead of a line.
+ // Also, we don’t want to apply shift, as there’s no line to shift.
+ if (placement.type === "inside-parent") {
+ return {
+ instance: data,
+ position: "end",
+ placement: { type: "rect", rect: rect },
+ };
+ }
+
+ const dropTargetPath = getInstancePathWithPositions(root, data.id);
+ dropTargetPath.reverse();
+
+ const currentDepth = dropTargetPath.length;
+ const desiredDepth = dragItemDepth + horizontalShift;
+
+ const withoutShift = {
+ instance: data,
+ position: indexWithinChildren,
+ placement: {
+ type: "line",
+ placement: shiftPlacement(currentDepth),
+ },
+ } as const;
+
+ const isDragItem = (instance: Instance | undefined | string) =>
+ typeof instance === "object" && instance.id === dragItem.id;
+
+ if (desiredDepth < currentDepth) {
+ // 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(data.children[indexWithinChildren])
+ ? indexWithinChildren + 1
+ : indexWithinChildren;
+
+ // Unless we're currently at the bottom of drop target's children,
+ // decreasing depth will not correspond to a new meaningful position
+ if (indexCorrected !== data.children.length) {
+ return withoutShift;
+ }
+
+ const difference = Math.min(
+ dropTargetPath.length - 1,
+ currentDepth - desiredDepth
+ );
+
+ if (difference === 0) {
+ return withoutShift;
+ }
+
+ // Ideally we should check canAcceptChildren on the new target
+ // but we assume that because it already has a child, it can accept more.
+
+ return {
+ instance: dropTargetPath[difference].instance,
+ position: dropTargetPath[difference - 1].position + 1,
+ placement: {
+ type: "line",
+ placement: shiftPlacement(currentDepth - difference),
+ },
+ };
+ }
+
+ if (desiredDepth > currentDepth) {
+ let shifted = 0;
+ let newParent = data;
+
+ // 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.
+ const getChildAbove = (
+ parent: Instance,
+ index: number
+ ): string | undefined | Instance =>
+ isDragItem(parent.children[index - 1])
+ ? parent.children[index - 2]
+ : parent.children[index - 1];
+
+ let potentialNewParent = getChildAbove(data, indexWithinChildren);
+
+ while (
+ typeof potentialNewParent === "object" &&
+ getIsExpanded(potentialNewParent) &&
+ components[potentialNewParent.component].canAcceptChild() &&
+ shifted < desiredDepth - currentDepth
+ ) {
+ newParent = potentialNewParent;
+ potentialNewParent = getChildAbove(
+ newParent,
+ newParent.children.length
+ );
+ shifted++;
+ }
+
+ if (shifted === 0) {
+ return withoutShift;
+ }
+
+ return {
+ instance: newParent,
+ position: "end",
+ placement: {
+ type: "line",
+ placement: shiftPlacement(currentDepth + shifted),
+ },
+ };
+ }
+
+ return withoutShift;
+ }, [
+ dragItem,
+ dragItemDepth,
+ dropTarget,
+ root,
+ horizontalShift,
+ getIsExpanded,
+ ]);
+
+ return [shiftedDropTarget, setHorizontalShift] as const;
};
diff --git a/apps/designer/docs/test-cases.md b/apps/designer/docs/test-cases.md
index 9b5e408745..a4361e6d99 100644
--- a/apps/designer/docs/test-cases.md
+++ b/apps/designer/docs/test-cases.md
@@ -124,5 +124,5 @@
- check that by moving horizontally you're able to change how deeply the item is nested inside the tree
- Drag an item between some tree items
- check that moving the cursor horizontally allows you to change the depth of the placement indicator line
- - check that after you drop, the drag item moves to the correct depth
+ - check that after you drop, the dragged item moves to the correct depth
- Check that after a drag&drop the dragged item is the selected item