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
open tree when instance is selected #3
Oleg Isonen0e7b5821b40a
diff --git a/app/designer/sidebar-left/panels/tree.tsx b/app/designer/sidebar-left/panels/tree.tsxindex 077782ffb2..aabd15238d 100644--- a/app/designer/sidebar-left/panels/tree.tsx+++ b/app/designer/sidebar-left/panels/tree.tsx@@ -11,6 +11,7 @@ import {} from "~/shared/design-system";import { TriangleRightIcon, TriangleDownIcon } from "~/shared/icons";import { primitives, type SelectedInstanceData } from "~/shared/component";+import { getInstancePath } from "~/shared/tree-utils";import { type Publish } from "~/designer/iframe";const openKeyframes = keyframes({@@ -36,6 +37,7 @@ const CollapsibleContent = styled(Collapsible.Content, {type TreeProps = {instance: Instance;selectedInstanceId?: Instance["id"];+ selectedInstancePath: Array<Instance["id"]>;level?: number;onSelect: (instance: Instance) => void;};@@ -45,8 +47,11 @@ const Tree = ({selectedInstanceId,level = 0,onSelect,+ selectedInstancePath,}: TreeProps) => {- const [isOpen, setIsOpen] = useState(false);+ const [isOpen, setIsOpen] = useState(+ selectedInstancePath.includes(instance.id)+ );// Text nodes have only one child which is a string.const showChildren =@@ -63,6 +68,7 @@ const Tree = ({<Treeinstance={child}selectedInstanceId={selectedInstanceId}+ selectedInstancePath={selectedInstancePath}level={level + 1}key={child.id}onSelect={onSelect}@@ -70,7 +76,15 @@ const Tree = ({);}return children;- }, [instance, level, isOpen, selectedInstanceId, onSelect, showChildren]);+ }, [+ instance,+ level,+ isOpen,+ selectedInstanceId,+ selectedInstancePath,+ onSelect,+ showChildren,+ ]);const { Icon, label } = primitives[instance.component];@@ -118,12 +132,23 @@ export const TabContent = ({publish,selectedInstanceData,}: TabContentProps) => {+ const selectedInstancePath = useMemo(+ () =>+ selectedInstanceData !== undefined && rootInstance !== undefined+ ? getInstancePath(rootInstance, selectedInstanceData.id).map(+ ({ id }) => id+ )+ : [],+ [selectedInstanceData, rootInstance]+ );+if (rootInstance === undefined) return null;return (<Flex gap="3" direction="column" css={{ padding: "$1" }}><Treeinstance={rootInstance}+ selectedInstancePath={selectedInstancePath}selectedInstanceId={selectedInstanceData?.id}onSelect={(instance) => {publish<"focusElement", Instance["id"]>({diff --git a/app/shared/tree-utils/get-instance-path.test.ts b/app/shared/tree-utils/get-instance-path.test.tsnew file mode 100644index 0000000000..3283e6fdbe--- /dev/null+++ b/app/shared/tree-utils/get-instance-path.test.ts@@ -0,0 +1,71 @@+import { type Instance } from "@webstudio-is/sdk";+import { getInstancePath } from "./get-instance-path";++const getIds = (array: Array<{ id: string }>) => array.map((item) => item.id);++describe.only("Get instance path", () => {+ test("single possible path", () => {+ const instance: Instance = {+ component: "Box",+ id: "1",+ style: {},+ children: [+ {+ component: "Box",+ id: "2",+ style: {},+ children: [+ {+ component: "Box",+ id: "3",+ style: {},+ children: [],+ },+ ],+ },+ ],+ };+ const path = getInstancePath(instance, "3");+ expect(getIds(path)).toEqual(["1", "2", "3"]);+ });++ test("two possible paths", () => {+ const instance: Instance = {+ component: "Box",+ id: "1",+ style: {},+ children: [+ {+ component: "Box",+ id: "2a",+ style: {},+ children: [+ {+ component: "Box",+ id: "3a",+ style: {},+ children: [],+ },+ ],+ },+ {+ component: "Box",+ id: "2b",+ style: {},+ children: [+ {+ component: "Box",+ id: "3b",+ style: {},+ children: [],+ },+ ],+ },+ ],+ };+ const path1 = getInstancePath(instance, "3b");+ expect(getIds(path1)).toEqual(["1", "2b", "3b"]);+ const path2 = getInstancePath(instance, "3a");+ expect(getIds(path2)).toEqual(["1", "2a", "3a"]);+ });+});diff --git a/app/shared/tree-utils/get-instance-path.ts b/app/shared/tree-utils/get-instance-path.tsnew file mode 100644index 0000000000..811c80a5a9--- /dev/null+++ b/app/shared/tree-utils/get-instance-path.ts@@ -0,0 +1,26 @@+import { Instance } from "@webstudio-is/sdk";++export const getInstancePath = (+ instance: Instance,+ instanceId: Instance["id"]+) => {+ const path = [];++ const find = (instance: Instance) => {+ if (instance.id === instanceId) return true;+ for (const child of instance.children) {+ if (typeof child === "string") continue;+ const found = find(child);+ if (found) {+ path.push(child);+ return true;+ }+ }+ };++ if (find(instance)) {+ path.push(instance);+ }++ return path.reverse();+};diff --git a/app/shared/tree-utils/index.ts b/app/shared/tree-utils/index.tsindex 6de2dce233..98920d7582 100644--- a/app/shared/tree-utils/index.ts+++ b/app/shared/tree-utils/index.ts@@ -7,3 +7,4 @@ export { createElementsTree, type OnChangeChildren } from "@webstudio-is/sdk";export * from "./set-instance-style";export * from "./set-instance-children";export * from "./reparent-instance";+export * from "./get-instance-path";