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.

Simplify horizontal shift (#1248)

Bogdan Chadkin48559568f54d
diff --git a/apps/builder/app/builder/shared/tree/index.tsx b/apps/builder/app/builder/shared/tree/index.tsx
index e368b90bc4..0a54012852 100644
--- a/apps/builder/app/builder/shared/tree/index.tsx
+++ b/apps/builder/app/builder/shared/tree/index.tsx
@@ -14,12 +14,10 @@ import {
getComponentMeta,
type WsComponentMeta,
} from "@webstudio-is/react-sdk";
-import { utils } from "@webstudio-is/project";
import { instancesIndexStore } from "~/shared/nano-states";
import { getInstanceAncestorsAndSelf } from "~/shared/tree-utils";
const instanceRelatedProps = {
- getItemPathWithPositions: utils.tree.getInstancePathWithPositions,
canLeaveParent(item: Instance) {
const meta = getComponentMeta(item.component);
return meta?.type !== "rich-text-child";
diff --git a/packages/design-system/src/components/tree/horizontal-shift.test.ts b/packages/design-system/src/components/tree/horizontal-shift.test.ts
index 11b8e39120..9582288646 100644
--- a/packages/design-system/src/components/tree/horizontal-shift.test.ts
+++ b/packages/design-system/src/components/tree/horizontal-shift.test.ts
@@ -6,7 +6,6 @@ import {
canAcceptChild,
getItemChildren,
getItemPath,
- getItemPathWithPositions,
Item,
} from "./test-tree-data";
@@ -111,7 +110,6 @@ const render = (
canAcceptChild,
getItemChildren,
getItemPath,
- getItemPathWithPositions,
},
});
diff --git a/packages/design-system/src/components/tree/horizontal-shift.ts b/packages/design-system/src/components/tree/horizontal-shift.ts
index 965fdbfc58..09a60c093f 100644
--- a/packages/design-system/src/components/tree/horizontal-shift.ts
+++ b/packages/design-system/src/components/tree/horizontal-shift.ts
@@ -14,17 +14,12 @@ export const useHorizontalShift = <Data extends { id: string }>({
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;
@@ -32,22 +27,15 @@ export const useHorizontalShift = <Data extends { id: string }>({
}) => {
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
- ) {
+ if (dropTarget === undefined || dragItem === undefined) {
return undefined;
}
+ const dragItemDepth = getItemPath(root, dragItem.id).length - 1;
+
const { data, placement, indexWithinChildren } = dropTarget;
const shiftPlacement = (depth: number) => {
@@ -66,7 +54,7 @@ export const useHorizontalShift = <Data extends { id: string }>({
return { item: data, position: "end" };
}
- const dropTargetPath = getItemPathWithPositions(root, data.id);
+ const dropTargetPath = getItemPath(root, data.id);
dropTargetPath.reverse();
const currentDepth = dropTargetPath.length;
@@ -78,8 +66,7 @@ export const useHorizontalShift = <Data extends { id: string }>({
placement: shiftPlacement(currentDepth),
} as const;
- const isDragItem = (item: Data | undefined) =>
- typeof item === "object" && item.id === dragItem.id;
+ const isDragItem = (item: Data | undefined) => item?.id === dragItem.id;
if (desiredDepth < currentDepth) {
let shifted = 0;
@@ -95,18 +82,24 @@ export const useHorizontalShift = <Data extends { id: string }>({
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];
+ // skip drop item
+ for (let index = 1; index < dropTargetPath.length; index += 1) {
+ const potentialNewParent = dropTargetPath[index];
+ if (
+ isAtTheBottom(newParent, newPosition) &&
+ canAcceptChild(potentialNewParent) &&
+ shifted < currentDepth - desiredDepth
+ ) {
+ shifted = index;
+ newParent = potentialNewParent;
+ const child = dropTargetPath[index - 1];
+ const childPosition = getItemChildren(newParent).findIndex(
+ (item) => item.id === child.id
+ );
+ newPosition = childPosition + 1;
+ continue;
+ }
+ break;
}
if (shifted === 0) {
@@ -141,7 +134,7 @@ export const useHorizontalShift = <Data extends { id: string }>({
let potentialNewParent = findNextParent(data, indexWithinChildren - 1);
while (
- typeof potentialNewParent === "object" &&
+ potentialNewParent &&
getIsExpanded(potentialNewParent) &&
canAcceptChild(potentialNewParent) &&
shifted < desiredDepth - currentDepth
@@ -165,11 +158,10 @@ export const useHorizontalShift = <Data extends { id: string }>({
return withoutShift;
}, [
dropTarget,
- dragItemDepth,
dragItem,
- getItemPathWithPositions,
root,
horizontalShift,
+ getItemPath,
canAcceptChild,
getItemChildren,
getIsExpanded,
diff --git a/packages/design-system/src/components/tree/test-tree-data.ts b/packages/design-system/src/components/tree/test-tree-data.ts
index c5b80148e2..65e14d3819 100644
--- a/packages/design-system/src/components/tree/test-tree-data.ts
+++ b/packages/design-system/src/components/tree/test-tree-data.ts
@@ -6,7 +6,7 @@ export type Item = {
children: Item[];
};
-export const getItemPathWithPositions = (tree: Item, itemId: string) => {
+export const getItemPath = (tree: Item, itemId: string) => {
const path = [];
const find = (item: Item) => {
@@ -21,22 +21,19 @@ export const getItemPathWithPositions = (tree: Item, itemId: string) => {
}
const found = find(child);
if (found) {
- path.push({ item: child, position: i });
+ path.push(child);
return true;
}
}
};
if (find(tree)) {
- path.push({ item: tree, position: 0 });
+ path.push(tree);
}
return path.reverse();
};
-export const getItemPath = (tree: Item, id: string) =>
- getItemPathWithPositions(tree, id).map(({ item }) => item);
-
export const canAcceptChild = (item: Item) => item.canAcceptChildren;
export const getItemChildren = (item: Item) => item.children;
diff --git a/packages/design-system/src/components/tree/tree.stories.tsx b/packages/design-system/src/components/tree/tree.stories.tsx
index d5d6c59fd2..09549fe00f 100644
--- a/packages/design-system/src/components/tree/tree.stories.tsx
+++ b/packages/design-system/src/components/tree/tree.stories.tsx
@@ -6,7 +6,6 @@ import {
findItemById,
getItemChildren,
getItemPath,
- getItemPathWithPositions,
Item,
reparent,
} from "./test-tree-data";
@@ -79,7 +78,6 @@ export const StressTest = ({ animate }: { animate: boolean }) => {
<Flex css={{ width: 300, height: 500, flexDirection: "column" }}>
<Tree
findItemById={findItemById}
- getItemPathWithPositions={getItemPathWithPositions}
getItemPath={getItemPath}
canAcceptChild={canAcceptChild}
canLeaveParent={() => true}
diff --git a/packages/design-system/src/components/tree/tree.tsx b/packages/design-system/src/components/tree/tree.tsx
index 819aa8d4e8..2ec9e5bb52 100644
--- a/packages/design-system/src/components/tree/tree.tsx
+++ b/packages/design-system/src/components/tree/tree.tsx
@@ -21,10 +21,6 @@ export type TreeProps<Data extends { id: string }> = {
canAcceptChild: (item: Data) => boolean;
findItemById: (root: Data, id: string) => Data | undefined;
getItemPath: (root: Data, id: string) => Data[];
- getItemPathWithPositions: (
- root: Data,
- id: string
- ) => Array<{ item: Data; position: number }>;
getItemChildren: (item: Data) => Data[];
renderItem: (props: TreeItemRenderProps<Data>) => React.ReactNode;
@@ -40,7 +36,6 @@ export type TreeProps<Data extends { id: string }> = {
export const Tree = <Data extends { id: string }>({
root,
- getItemPathWithPositions,
canLeaveParent,
canAcceptChild,
findItemById,
@@ -77,7 +72,6 @@ export const Tree = <Data extends { id: string }>({
root,
getIsExpanded,
getItemPath,
- getItemPathWithPositions,
getItemChildren,
canAcceptChild,
});
diff --git a/packages/project/src/shared/tree-utils/get-instance-path.test.ts b/packages/project/src/shared/tree-utils/get-instance-path.test.ts
deleted file mode 100644
index a48ce7374f..0000000000
--- a/packages/project/src/shared/tree-utils/get-instance-path.test.ts
+++ /dev/null
@@ -1,87 +0,0 @@
-import { describe, test, expect } from "@jest/globals";
-import type { Instance } from "@webstudio-is/project-build";
-import { getInstancePathWithPositions } from "./get-instance-path";
-
-const getIdAndPosition = (
- array: Array<{ item: { id: string }; position: number }>
-) => array.map((item) => [item.item.id, item.position]);
-
-describe("Get instance path with positions", () => {
- test("single possible path", () => {
- const instance: Instance = {
- type: "instance",
- component: "Box",
- id: "1",
- children: [
- {
- type: "instance",
- component: "Box",
- id: "2",
- children: [
- {
- type: "instance",
- component: "Box",
- id: "3",
- children: [],
- },
- ],
- },
- ],
- };
- const path = getInstancePathWithPositions(instance, "3");
- expect(getIdAndPosition(path)).toEqual([
- ["1", 0],
- ["2", 0],
- ["3", 0],
- ]);
- });
-
- test("two possible paths", () => {
- const instance: Instance = {
- type: "instance",
- component: "Box",
- id: "1",
- children: [
- {
- type: "instance",
- component: "Box",
- id: "2a",
- children: [
- {
- type: "instance",
- component: "Box",
- id: "3a",
- children: [],
- },
- ],
- },
- {
- type: "instance",
- component: "Box",
- id: "2b",
- children: [
- {
- type: "instance",
- component: "Box",
- id: "3b",
- children: [],
- },
- ],
- },
- ],
- };
- const path1 = getInstancePathWithPositions(instance, "3b");
- expect(getIdAndPosition(path1)).toEqual([
- ["1", 0],
- ["2b", 1],
- ["3b", 0],
- ]);
-
- const path2 = getInstancePathWithPositions(instance, "3a");
- expect(getIdAndPosition(path2)).toEqual([
- ["1", 0],
- ["2a", 0],
- ["3a", 0],
- ]);
- });
-});
diff --git a/packages/project/src/shared/tree-utils/get-instance-path.ts b/packages/project/src/shared/tree-utils/get-instance-path.ts
deleted file mode 100644
index b49b3a2e4c..0000000000
--- a/packages/project/src/shared/tree-utils/get-instance-path.ts
+++ /dev/null
@@ -1,31 +0,0 @@
-import type { Instance } from "@webstudio-is/project-build";
-
-export const getInstancePathWithPositions = (
- instance: Instance,
- instanceId: Instance["id"]
-) => {
- const path = [];
-
- const find = (instance: Instance) => {
- if (instance.id === instanceId) {
- return true;
- }
- for (let index = 0; index < instance.children.length; index++) {
- const child = instance.children[index];
- if (child.type === "text") {
- continue;
- }
- const found = find(child);
- if (found) {
- path.push({ item: child, position: index });
- return true;
- }
- }
- };
-
- if (find(instance)) {
- path.push({ item: instance, position: 0 });
- }
-
- return path.reverse();
-};
diff --git a/packages/project/src/shared/tree-utils/index.ts b/packages/project/src/shared/tree-utils/index.ts
index c6e6ddd789..2835c96c85 100644
--- a/packages/project/src/shared/tree-utils/index.ts
+++ b/packages/project/src/shared/tree-utils/index.ts
@@ -1,2 +1 @@
export * from "./create-instance";
-export * from "./get-instance-path";