Skip to content

Commit 1a54822

Browse files
authored
experimental: show only block and ts children in navigator (#4506)
Ref #3994 <img width="279" alt="Screenshot 2024-12-04 at 17 32 32" src="https://github.com/user-attachments/assets/277c7fbc-537a-459d-bab0-fbcb56c990b4"> <img width="284" alt="Screenshot 2024-12-04 at 17 32 41" src="https://github.com/user-attachments/assets/8e1d870f-7751-441f-bb49-3da5180109cd">
1 parent 9806caa commit 1a54822

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

apps/builder/app/builder/features/navigator/navigator-tree.tsx

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
rootComponent,
2626
showAttribute,
2727
WsComponentMeta,
28+
blockTemplateComponent,
2829
} from "@webstudio-is/react-sdk";
2930
import { ROOT_INSTANCE_ID, type Instance } from "@webstudio-is/sdk";
3031
import {
@@ -82,7 +83,70 @@ const $dropTarget = computed(
8283
(dragAndDropState) => dragAndDropState.dropTarget
8384
);
8485

85-
const $flatTree = computed(
86+
const $contentTree = computed(
87+
[$selectedPage, $instances, $propValuesByInstanceSelector],
88+
(page, instances, propValuesByInstanceSelector) => {
89+
const flatTree: TreeItem[] = [];
90+
if (page === undefined) {
91+
return flatTree;
92+
}
93+
const traverse = (
94+
instanceId: Instance["id"],
95+
selector: InstanceSelector,
96+
parentComponent?: Instance["component"],
97+
isParentHidden = false,
98+
isLastChild = false,
99+
level = 0
100+
) => {
101+
const instance = instances.get(instanceId);
102+
if (instance === undefined) {
103+
// log instead of failing navigator tree
104+
console.error(`Unknown instance ${instanceId}`);
105+
return;
106+
}
107+
const propValues = propValuesByInstanceSelector.get(
108+
getInstanceKey(selector)
109+
);
110+
const isHidden =
111+
isParentHidden ||
112+
false === Boolean(propValues?.get(showAttribute) ?? true);
113+
const treeItem: TreeItem = {
114+
level,
115+
selector,
116+
instance,
117+
isExpanded: undefined,
118+
isLastChild,
119+
isHidden,
120+
isReusable: false,
121+
};
122+
const isVisible =
123+
instance.component === blockComponent ||
124+
(parentComponent === blockComponent &&
125+
instance.component !== blockTemplateComponent);
126+
if (isVisible) {
127+
flatTree.push(treeItem);
128+
}
129+
for (let index = 0; index < instance.children.length; index += 1) {
130+
const child = instance.children[index];
131+
if (child.type === "id") {
132+
const isLastChild = index === instance.children.length - 1;
133+
traverse(
134+
child.value,
135+
[child.value, ...selector],
136+
instance.component,
137+
isHidden,
138+
isLastChild,
139+
isVisible ? level + 1 : level
140+
);
141+
}
142+
}
143+
};
144+
traverse(page.rootInstanceId, [page.rootInstanceId]);
145+
return flatTree;
146+
}
147+
);
148+
149+
export const $flatTree = computed(
86150
[
87151
$selectedPage,
88152
$instances,
@@ -480,7 +544,8 @@ const canDrop = (
480544
};
481545

482546
export const NavigatorTree = () => {
483-
const flatTree = useStore($flatTree);
547+
const isContentMode = useStore($isContentMode);
548+
const flatTree = useStore(isContentMode ? $contentTree : $flatTree);
484549
const selectedInstanceSelector = useStore($selectedInstanceSelector);
485550
const selectedKey = selectedInstanceSelector?.join();
486551
const hoveredInstanceSelector = useStore($hoveredInstanceSelector);
@@ -523,7 +588,7 @@ export const NavigatorTree = () => {
523588
}}
524589
>
525590
<TreeRoot>
526-
{rootMeta && (
591+
{rootMeta && isContentMode === false && (
527592
<TreeNode
528593
level={0}
529594
isSelected={selectedKey === ROOT_INSTANCE_ID}

packages/react-sdk/src/core-components.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ const blockMeta: WsComponentMeta = {
149149
type: "container",
150150
label: "Content Block",
151151
icon: EditIcon,
152+
constraints: {
153+
relation: "ancestor",
154+
component: { $neq: collectionComponent },
155+
},
152156
stylable: false,
153157
template: [
154158
{

0 commit comments

Comments
 (0)