Summary: Introduced some basic bidirectional link between tree and wireframe, the specific interaction will need some tweaking but this should get us started. When hovering over the tree we halt the rendering of the wireframe up to that point, this allows us to explore parent views that layout child views. When clicking a view in the wireframe it is 'seleceted' as if it was clicked in the tree. This set the tree selection so you can identify it in the tree as well as opens the side bar Reviewed By: lblasa Differential Revision: D39539277 fbshipit-source-id: 3beb1ad4cb56b398c640ac3e7fac2cc97f3f1a18
80 lines
1.9 KiB
TypeScript
80 lines
1.9 KiB
TypeScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
*/
|
|
|
|
import {Id, UINode} from '../types';
|
|
import {DataNode} from 'antd/es/tree';
|
|
import {Tree as AntTree} from 'antd';
|
|
import {DownOutlined} from '@ant-design/icons';
|
|
import React from 'react';
|
|
|
|
export function Tree(props: {
|
|
rootId: Id;
|
|
nodes: Map<Id, UINode>;
|
|
selectedNode?: Id;
|
|
onSelectNode: (id: Id) => void;
|
|
onHoveredNode: (id?: Id) => void;
|
|
}) {
|
|
const [antTree, inactive] = nodesToAntTree(props.rootId, props.nodes);
|
|
|
|
return (
|
|
<AntTree
|
|
onMouseLeave={() => {
|
|
//when mouse exits the entire tree then unhover
|
|
props.onHoveredNode(undefined);
|
|
}}
|
|
showIcon
|
|
showLine
|
|
titleRender={(node) => {
|
|
return (
|
|
<div
|
|
onMouseEnter={() => {
|
|
props.onHoveredNode(node.key as Id);
|
|
}}>
|
|
{node.title}
|
|
</div>
|
|
);
|
|
}}
|
|
selectedKeys={[props.selectedNode ?? '']}
|
|
onSelect={(selected) => {
|
|
props.onSelectNode(selected[0] as Id);
|
|
}}
|
|
defaultExpandAll
|
|
expandedKeys={[...props.nodes.keys()].filter(
|
|
(key) => !inactive.includes(key),
|
|
)}
|
|
switcherIcon={<DownOutlined />}
|
|
treeData={[antTree]}
|
|
/>
|
|
);
|
|
}
|
|
|
|
function nodesToAntTree(root: Id, nodes: Map<Id, UINode>): [DataNode, Id[]] {
|
|
const inactive: Id[] = [];
|
|
|
|
function uiNodeToAntNode(id: Id): DataNode {
|
|
const node = nodes.get(id);
|
|
|
|
if (node?.activeChild) {
|
|
for (const child of node.children) {
|
|
if (child !== node?.activeChild) {
|
|
inactive.push(child);
|
|
}
|
|
}
|
|
}
|
|
|
|
return {
|
|
key: id,
|
|
title: node?.name,
|
|
children: node?.children.map((id) => uiNodeToAntNode(id)),
|
|
};
|
|
}
|
|
|
|
return [uiNodeToAntNode(root), inactive];
|
|
}
|