Skip to content

Commit

Permalink
experimental: show only block and ts children in navigator (#4506)
Browse files Browse the repository at this point in the history
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">
  • Loading branch information
TrySound authored Dec 4, 2024
1 parent 9806caa commit 1a54822
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
71 changes: 68 additions & 3 deletions apps/builder/app/builder/features/navigator/navigator-tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -523,7 +588,7 @@ export const NavigatorTree = () => {
}}
>
<TreeRoot>
{rootMeta && (
{rootMeta && isContentMode === false && (
<TreeNode
level={0}
isSelected={selectedKey === ROOT_INSTANCE_ID}
Expand Down
4 changes: 4 additions & 0 deletions packages/react-sdk/src/core-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ const blockMeta: WsComponentMeta = {
type: "container",
label: "Content Block",
icon: EditIcon,
constraints: {
relation: "ancestor",
component: { $neq: collectionComponent },
},
stylable: false,
template: [
{
Expand Down

0 comments on commit 1a54822

Please sign in to comment.