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.

Refactor getInstancePath with instances index (#1080)

Bogdan Chadkin86178234ab9c
diff --git a/apps/builder/app/builder/features/footer/breadcrumbs.tsx b/apps/builder/app/builder/features/footer/breadcrumbs.tsx
index 3ba6d50ed1..9fc5ce1280 100644
--- a/apps/builder/app/builder/features/footer/breadcrumbs.tsx
+++ b/apps/builder/app/builder/features/footer/breadcrumbs.tsx
@@ -1,13 +1,17 @@
+import { useMemo } from "react";
import { useStore } from "@nanostores/react";
import { ChevronRightIcon } from "@webstudio-is/icons";
import {
+ theme,
DeprecatedButton,
Flex,
DeprecatedText2,
} from "@webstudio-is/design-system";
-import { theme } from "@webstudio-is/design-system";
-import { selectedInstanceIdStore } from "~/shared/nano-states";
-import { useSelectedInstancePath } from "~/builder/shared/instance/use-selected-instance-path";
+import {
+ instancesIndexStore,
+ selectedInstanceIdStore,
+} from "~/shared/nano-states";
+import { getInstanceAncestorsAndSelf } from "~/shared/tree-utils";
type BreadcrumbProps = {
children: JSX.Element | string;
@@ -35,8 +39,16 @@ const Breadcrumb = ({ children, onClick }: BreadcrumbProps) => {
};
export const Breadcrumbs = () => {
+ const instancesIndex = useStore(instancesIndexStore);
const selectedInstanceId = useStore(selectedInstanceIdStore);
- const selectedInstancePath = useSelectedInstancePath(selectedInstanceId);
+
+ const selectedInstancePath = useMemo(() => {
+ if (selectedInstanceId === undefined) {
+ return [];
+ }
+ return getInstanceAncestorsAndSelf(instancesIndex, selectedInstanceId);
+ }, [selectedInstanceId, instancesIndex]);
+
return (
<Flex align="center" css={{ height: "100%" }}>
{selectedInstancePath.length === 0 ? (
diff --git a/apps/builder/app/builder/features/style-panel/shared/style-info.test.ts b/apps/builder/app/builder/features/style-panel/shared/style-info.test.ts
index 36f499cdf0..3c38553f6e 100644
--- a/apps/builder/app/builder/features/style-panel/shared/style-info.test.ts
+++ b/apps/builder/app/builder/features/style-panel/shared/style-info.test.ts
@@ -3,6 +3,7 @@ import type {
Instance,
StyleDecl,
} from "@webstudio-is/project-build";
+import { createInstancesIndex } from "~/shared/tree-utils";
import {
getCascadedBreakpointIds,
getCascadedInfo,
@@ -145,9 +146,10 @@ test("compute cascaded styles", () => {
});
test("compute inherited styles", () => {
+ const instancesIndex = createInstancesIndex(rootInstance);
expect(
getInheritedInfo(
- rootInstance,
+ instancesIndex,
inheritingStylesByInstanceId,
selectedInstanceId,
cascadedBreakpointIds,
diff --git a/apps/builder/app/builder/features/style-panel/shared/style-info.ts b/apps/builder/app/builder/features/style-panel/shared/style-info.ts
index 0b799ea3cf..161099a624 100644
--- a/apps/builder/app/builder/features/style-panel/shared/style-info.ts
+++ b/apps/builder/app/builder/features/style-panel/shared/style-info.ts
@@ -10,14 +10,18 @@ import type {
StyleSource as StyleSourceType,
} from "@webstudio-is/project-build";
import {
+ instancesIndexStore,
selectedInstanceBrowserStyleStore,
selectedInstanceIdStore,
selectedStyleSourceStore,
stylesIndexStore,
useBreakpoints,
- useRootInstance,
} from "~/shared/nano-states";
import { selectedBreakpointStore } from "~/shared/nano-states/breakpoints";
+import {
+ type InstancesIndex,
+ getInstanceAncestorsAndSelf,
+} from "~/shared/tree-utils";
type CascadedValueInfo = {
breakpointId: string;
@@ -144,15 +148,18 @@ export const getCascadedInfo = (
* including active breakpoints
*/
export const getInheritedInfo = (
- rootInstance: Instance,
+ instancesIndex: InstancesIndex,
stylesByInstanceId: Map<Instance["id"], StyleDecl[]>,
instanceId: string,
cascadedBreakpointIds: string[],
selectedBreakpointId: string
) => {
const inheritedStyle: InheritedProperties = {};
- const ancestors = utils.tree.getInstancePath(rootInstance, instanceId);
- for (const ancestorInstance of ancestors) {
+ const ancestorsAndSelf = getInstanceAncestorsAndSelf(
+ instancesIndex,
+ instanceId
+ );
+ for (const ancestorInstance of ancestorsAndSelf) {
// skip current element
if (ancestorInstance.id === instanceId) {
continue;
@@ -196,7 +203,7 @@ export const useStyleInfo = () => {
const selectedStyleSource = useStore(selectedStyleSourceStore);
const selectedStyleSourceId = selectedStyleSource?.id;
const browserStyle = useStore(selectedInstanceBrowserStyleStore);
- const [rootInstance] = useRootInstance();
+ const instancesIndex = useStore(instancesIndexStore);
const { stylesByInstanceId, stylesByStyleSourceId } =
useStore(stylesIndexStore);
@@ -221,21 +228,20 @@ export const useStyleInfo = () => {
const inheritedInfo = useMemo(() => {
if (
- rootInstance === undefined ||
selectedBreakpointId === undefined ||
selectedInstanceId === undefined
) {
return {};
}
return getInheritedInfo(
- rootInstance,
+ instancesIndex,
stylesByInstanceId,
selectedInstanceId,
cascadedBreakpointIds,
selectedBreakpointId
);
}, [
- rootInstance,
+ instancesIndex,
stylesByInstanceId,
cascadedBreakpointIds,
selectedBreakpointId,
diff --git a/apps/builder/app/builder/features/tree-preview/tree-preview.tsx b/apps/builder/app/builder/features/tree-preview/tree-preview.tsx
index dca9d1690b..cfb23b7c27 100644
--- a/apps/builder/app/builder/features/tree-preview/tree-preview.tsx
+++ b/apps/builder/app/builder/features/tree-preview/tree-preview.tsx
@@ -12,13 +12,14 @@ import {
import { InstanceTreeNode } from "~/builder/shared/tree";
import {
createInstancesIndex,
+ getInstanceAncestorsAndSelf,
insertInstanceMutable,
reparentInstanceMutable,
} from "~/shared/tree-utils";
export const TreePrevew = () => {
const [rootInstance] = useRootInstance();
- const { instancesById } = useStore(instancesIndexStore);
+ const instancesIndex = useStore(instancesIndexStore);
const [dragAndDropState] = useDragAndDropState();
const dragItemInstance = dragAndDropState.dragItem;
@@ -35,6 +36,7 @@ export const TreePrevew = () => {
return null;
}
+ const { instancesById } = instancesIndex;
const isNew = instancesById.get(dragItemInstance.id) === undefined;
const instance: Instance = produce<Instance>((draft) => {
@@ -56,9 +58,10 @@ export const TreePrevew = () => {
}
})(rootInstance);
- const dropTargetPath = utils.tree
- .getInstancePath(rootInstance, dropTargetInstanceId)
- .map((item) => item.id);
+ const dropTargetPath = getInstanceAncestorsAndSelf(
+ instancesIndex,
+ dropTargetInstanceId
+ ).map((item) => item.id);
return {
itemData: instance,
@@ -69,7 +72,7 @@ export const TreePrevew = () => {
};
}, [
rootInstance,
- instancesById,
+ instancesIndex,
dragItemInstance,
dropTargetInstanceId,
dropTargetPosition,
diff --git a/apps/builder/app/builder/shared/instance/use-selected-instance-path.ts b/apps/builder/app/builder/shared/instance/use-selected-instance-path.ts
deleted file mode 100644
index 590bdafb52..0000000000
--- a/apps/builder/app/builder/shared/instance/use-selected-instance-path.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-import type { Instance } from "@webstudio-is/project-build";
-import { useMemo } from "react";
-import { useRootInstance } from "~/shared/nano-states";
-import { utils } from "@webstudio-is/project";
-
-export const useSelectedInstancePath = (
- selectedInstanceId?: Instance["id"]
-): Array<Instance> => {
- const [rootInstance] = useRootInstance();
- return useMemo(
- () =>
- selectedInstanceId !== undefined && rootInstance !== undefined
- ? utils.tree.getInstancePath(rootInstance, selectedInstanceId)
- : [],
- [selectedInstanceId, rootInstance]
- );
-};
diff --git a/apps/builder/app/builder/shared/tree/index.tsx b/apps/builder/app/builder/shared/tree/index.tsx
index 336e822cf0..dbee3d677b 100644
--- a/apps/builder/app/builder/shared/tree/index.tsx
+++ b/apps/builder/app/builder/shared/tree/index.tsx
@@ -13,9 +13,9 @@ import type { Instance } from "@webstudio-is/project-build";
import { getComponentMeta } 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 = {
- getItemPath: utils.tree.getInstancePath,
getItemPathWithPositions: utils.tree.getInstancePathWithPositions,
canLeaveParent(item: Instance) {
const meta = getComponentMeta(item.component);
@@ -59,18 +59,33 @@ const instanceRelatedProps = {
export const InstanceTree = (
props: Omit<
TreeProps<Instance>,
- keyof typeof instanceRelatedProps | "findItemById"
+ keyof typeof instanceRelatedProps | "findItemById" | "getItemPath"
>
) => {
- const { instancesById } = useStore(instancesIndexStore);
+ const instancesIndex = useStore(instancesIndexStore);
+ const { instancesById } = instancesIndex;
+
const findItemById = useCallback(
(_rootInstance: Instance, instanceId: Instance["id"]) => {
return instancesById.get(instanceId);
},
[instancesById]
);
+
+ const getItemPath = useCallback(
+ (_rootInstance: Instance, instanceId: Instance["id"]) => {
+ return getInstanceAncestorsAndSelf(instancesIndex, instanceId);
+ },
+ [instancesIndex]
+ );
+
return (
- <Tree {...props} {...instanceRelatedProps} findItemById={findItemById} />
+ <Tree
+ {...props}
+ {...instanceRelatedProps}
+ findItemById={findItemById}
+ getItemPath={getItemPath}
+ />
);
};
diff --git a/apps/builder/app/canvas/shared/use-drag-drop.ts b/apps/builder/app/canvas/shared/use-drag-drop.ts
index 912a1ccdde..3ccd3d4df8 100644
--- a/apps/builder/app/canvas/shared/use-drag-drop.ts
+++ b/apps/builder/app/canvas/shared/use-drag-drop.ts
@@ -25,6 +25,7 @@ import {
getInstanceElementById,
getInstanceIdFromElement,
} from "~/shared/dom-utils";
+import { getInstanceAncestorsAndSelf } from "~/shared/tree-utils";
declare module "~/shared/pubsub" {
export interface PubsubMap {
@@ -64,7 +65,8 @@ const initialState: {
export const useDragAndDrop = () => {
const [rootInstance] = useRootInstance();
- const { instancesById } = useStore(instancesIndexStore);
+ const instancesIndex = useStore(instancesIndexStore);
+ const { instancesById } = instancesIndex;
const [textEditingInstanceId] = useTextEditingInstanceId();
const state = useRef({ ...initialState });
@@ -103,7 +105,10 @@ export const useDragAndDrop = () => {
return dropTarget;
}
- const path = utils.tree.getInstancePath(rootInstance, dropTarget.data.id);
+ const path = getInstanceAncestorsAndSelf(
+ instancesIndex,
+ dropTarget.data.id
+ );
path.reverse();
if (dropTarget.area !== "center") {
diff --git a/apps/builder/app/shared/tree-utils.test.ts b/apps/builder/app/shared/tree-utils.test.ts
index bd1e0c4385..0ade2aa7e1 100644
--- a/apps/builder/app/shared/tree-utils.test.ts
+++ b/apps/builder/app/shared/tree-utils.test.ts
@@ -17,6 +17,7 @@ import {
findClosestDroppableTarget,
findSubtree,
findSubtreeLocalStyleSources,
+ getInstanceAncestorsAndSelf,
insertInstanceMutable,
reparentInstanceMutable,
} from "./tree-utils";
@@ -80,7 +81,7 @@ const createStyleDecl = (
): StyleDecl => {
return {
styleSourceId,
- breakpointId: "breakpointId",
+ breakpointId,
property: "width",
value: {
type: "keyword",
@@ -251,6 +252,36 @@ test("reparent instance into target", () => {
);
});
+test("get path from instance and its ancestors", () => {
+ const rootInstance: Instance = createInstance("root", "Box", [
+ createInstance("box1", "Box", []),
+ createInstance("box2", "Box", [
+ createInstance("box3", "Box", [
+ createInstance("child1", "Box", []),
+ createInstance("child2", "Box", []),
+ ]),
+ ]),
+ createInstance("box4", "Box", []),
+ ]);
+ const instancesIndex = createInstancesIndex(rootInstance);
+
+ expect(getInstanceAncestorsAndSelf(instancesIndex, "box3")).toEqual([
+ rootInstance,
+
+ createInstance("box2", "Box", [
+ createInstance("box3", "Box", [
+ createInstance("child1", "Box", []),
+ createInstance("child2", "Box", []),
+ ]),
+ ]),
+
+ createInstance("box3", "Box", [
+ createInstance("child1", "Box", []),
+ createInstance("child2", "Box", []),
+ ]),
+ ]);
+});
+
test("find subtree with all descendants and parent instance", () => {
const rootInstance: Instance = createInstance("root", "Box", [
createInstance("box1", "Box", []),
diff --git a/apps/builder/app/shared/tree-utils.ts b/apps/builder/app/shared/tree-utils.ts
index d7c5d421a4..4c619e6624 100644
--- a/apps/builder/app/shared/tree-utils.ts
+++ b/apps/builder/app/shared/tree-utils.ts
@@ -28,7 +28,7 @@ const traverseInstances = (
}
};
-type InstancesIndex = {
+export type InstancesIndex = {
rootInstanceId: undefined | Instance["id"];
instancesById: Map<Instance["id"], Instance>;
parentInstancesById: Map<Instance["id"], Instance>;
@@ -164,6 +164,20 @@ export const reparentInstanceMutable = (
}
};
+export const getInstanceAncestorsAndSelf = (
+ instancesIndex: InstancesIndex,
+ instanceId: Instance["id"]
+) => {
+ const { instancesById, parentInstancesById } = instancesIndex;
+ const path = [];
+ let instance = instancesById.get(instanceId);
+ while (instance) {
+ path.unshift(instance);
+ instance = parentInstancesById.get(instance.id);
+ }
+ return path;
+};
+
export const findSubtree = (
rootInstance: Instance,
targetInstanceId: Instance["id"]