From 1a548226e68466a5e5fc22853872c95dad354297 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Wed, 4 Dec 2024 18:51:17 +0700 Subject: [PATCH] experimental: show only block and ts children in navigator (#4506) Ref https://github.com/webstudio-is/webstudio/issues/3994 Screenshot 2024-12-04 at 17 32 32 Screenshot 2024-12-04 at 17 32 41 --- .../features/navigator/navigator-tree.tsx | 71 ++++++++++++++++++- packages/react-sdk/src/core-components.ts | 4 ++ 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/apps/builder/app/builder/features/navigator/navigator-tree.tsx b/apps/builder/app/builder/features/navigator/navigator-tree.tsx index 07c77e0bd620..11d912c71ab6 100644 --- a/apps/builder/app/builder/features/navigator/navigator-tree.tsx +++ b/apps/builder/app/builder/features/navigator/navigator-tree.tsx @@ -25,6 +25,7 @@ import { rootComponent, showAttribute, WsComponentMeta, + blockTemplateComponent, } from "@webstudio-is/react-sdk"; import { ROOT_INSTANCE_ID, type Instance } from "@webstudio-is/sdk"; import { @@ -82,7 +83,70 @@ const $dropTarget = computed( (dragAndDropState) => dragAndDropState.dropTarget ); -const $flatTree = computed( +const $contentTree = computed( + [$selectedPage, $instances, $propValuesByInstanceSelector], + (page, instances, propValuesByInstanceSelector) => { + const flatTree: TreeItem[] = []; + if (page === undefined) { + return flatTree; + } + const traverse = ( + instanceId: Instance["id"], + selector: InstanceSelector, + parentComponent?: Instance["component"], + isParentHidden = false, + isLastChild = false, + level = 0 + ) => { + const instance = instances.get(instanceId); + if (instance === undefined) { + // log instead of failing navigator tree + console.error(`Unknown instance ${instanceId}`); + return; + } + const propValues = propValuesByInstanceSelector.get( + getInstanceKey(selector) + ); + const isHidden = + isParentHidden || + false === Boolean(propValues?.get(showAttribute) ?? true); + const treeItem: TreeItem = { + level, + selector, + instance, + isExpanded: undefined, + isLastChild, + isHidden, + isReusable: false, + }; + const isVisible = + instance.component === blockComponent || + (parentComponent === blockComponent && + instance.component !== blockTemplateComponent); + if (isVisible) { + flatTree.push(treeItem); + } + for (let index = 0; index < instance.children.length; index += 1) { + const child = instance.children[index]; + if (child.type === "id") { + const isLastChild = index === instance.children.length - 1; + traverse( + child.value, + [child.value, ...selector], + instance.component, + isHidden, + isLastChild, + isVisible ? level + 1 : level + ); + } + } + }; + traverse(page.rootInstanceId, [page.rootInstanceId]); + return flatTree; + } +); + +export const $flatTree = computed( [ $selectedPage, $instances, @@ -480,7 +544,8 @@ const canDrop = ( }; export const NavigatorTree = () => { - const flatTree = useStore($flatTree); + const isContentMode = useStore($isContentMode); + const flatTree = useStore(isContentMode ? $contentTree : $flatTree); const selectedInstanceSelector = useStore($selectedInstanceSelector); const selectedKey = selectedInstanceSelector?.join(); const hoveredInstanceSelector = useStore($hoveredInstanceSelector); @@ -523,7 +588,7 @@ export const NavigatorTree = () => { }} > - {rootMeta && ( + {rootMeta && isContentMode === false && (