Ant tree -> React complex tree

Summary:
Upgraded from ant tree library to the much more capable React complex tree. Added the following:
1. Ability to expand / collapse nodes while automatically expanding / collapsing active/inactive children when they change
2. Keyboard controls of tree all the time
3. Basic search functionality
4. Selecting node in tree focuses and scrolls in the tree
5. Hover state for tree

Reviewed By: lblasa

Differential Revision: D40633876

fbshipit-source-id: 8dcef5ec2c277e476a3eb3cdaef62b15c25323c0
This commit is contained in:
Luke De Feo
2022-10-25 07:10:38 -07:00
committed by Facebook GitHub Bot
parent 0c52ad307e
commit f282a5eb8a
8 changed files with 191 additions and 124 deletions

View File

@@ -8,76 +8,108 @@
*/
import {Id, UINode} from '../types';
import {Tree as AntTree, TreeDataNode} from 'antd';
import {DownOutlined} from '@ant-design/icons';
import React from 'react';
import React, {useEffect, useRef} from 'react';
import {
Tree as ComplexTree,
ControlledTreeEnvironment,
TreeItem,
} from 'react-complex-tree';
import {plugin} from '../index';
import {usePlugin, useValue} from 'flipper-plugin';
import {
InteractionMode,
TreeEnvironmentRef,
} from 'react-complex-tree/lib/esm/types';
export function Tree(props: {
rootId: Id;
nodes: Map<Id, UINode>;
selectedNode?: Id;
hoveredNode?: Id;
onSelectNode: (id: Id) => void;
onHoveredNode: (id?: Id) => void;
}) {
const [antTree, inactive] = nodesToAntTree(props.rootId, props.nodes);
const instance = usePlugin(plugin);
const expandedItems = useValue(instance.treeState).expandedNodes;
const items = toComplexTree(props.nodes);
const treeRef = useRef<TreeEnvironmentRef>();
useEffect(() => {
//this makes the keyboard arrow controls work always, even when using the visualiser
treeRef.current?.focusTree('tree', true);
}, [props.hoveredNode, props.selectedNode]);
return (
<div
onMouseLeave={() => {
//This div exists so when mouse exits the entire tree then unhover
props.onHoveredNode(undefined);
}}>
<AntTree
showIcon
showLine
titleRender={(node) => {
return (
<div
onMouseEnter={() => {
props.onHoveredNode(node.key as Id);
}}>
{node.title}
</div>
<ControlledTreeEnvironment
ref={treeRef as any}
items={items}
getItemTitle={(item) => item.data.name}
canRename={false}
canDragAndDrop={false}
canSearch
autoFocus
viewState={{
tree: {
focusedItem: props.hoveredNode,
expandedItems,
selectedItems: props.selectedNode ? [props.selectedNode] : [],
},
}}
onFocusItem={(item) => props.onHoveredNode(item.index)}
onExpandItem={(item) => {
instance.treeState.update((draft) => {
draft.expandedNodes.push(item.index);
});
}}
onCollapseItem={(item) =>
instance.treeState.update((draft) => {
draft.expandedNodes = draft.expandedNodes.filter(
(expandedItemIndex) => expandedItemIndex !== item.index,
);
}}
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]}
})
}
onSelectItems={(items) => props.onSelectNode(items[0])}
defaultInteractionMode={{
mode: 'custom',
extends: InteractionMode.DoubleClickItemToExpand,
createInteractiveElementProps: (
item,
treeId,
actions,
renderFlags,
) => ({
onClick: () => {
if (renderFlags.isSelected) {
actions.unselectItem();
} else {
actions.selectItem();
}
},
onMouseOver: () => {
props.onHoveredNode(item.index);
},
}),
}}>
<ComplexTree
treeId="tree"
rootItem={props.rootId as any} //the typing in in the library is wrong here
treeLabel="UI"
/>
</div>
</ControlledTreeEnvironment>
);
}
function nodesToAntTree(
root: Id,
nodes: Map<Id, UINode>,
): [TreeDataNode, Id[]] {
const inactive: Id[] = [];
function uiNodeToAntNode(id: Id): TreeDataNode {
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)),
function toComplexTree(nodes: Map<Id, UINode>): Record<Id, TreeItem<UINode>> {
const res: Record<Id, TreeItem<UINode>> = {};
for (const node of nodes.values()) {
res[node.id] = {
index: node.id,
canMove: false,
canRename: false,
children: node.children,
data: node,
hasChildren: node.children.length > 0,
};
}
return [uiNodeToAntNode(root), inactive];
return res;
}

View File

@@ -10,11 +10,10 @@
import React from 'react';
import {Id, Snapshot, Tag, UINode} from '../types';
import {styled, Layout, theme} from 'flipper-plugin';
import {Typography} from 'antd';
export const Visualization2D: React.FC<
{
root: Id;
rootId: Id;
nodes: Map<Id, UINode>;
snapshots: Map<Id, Snapshot>;
hoveredNode?: Id;
@@ -24,7 +23,7 @@ export const Visualization2D: React.FC<
modifierPressed: boolean;
} & React.HTMLAttributes<HTMLDivElement>
> = ({
root,
rootId,
nodes,
snapshots,
hoveredNode,
@@ -34,53 +33,49 @@ export const Visualization2D: React.FC<
modifierPressed,
}) => {
//todo, do a bfs search for the first bounds found
const rootBounds = nodes.get(root)?.bounds;
const rootSnapshot = snapshots.get(root);
const rootBounds = nodes.get(rootId)?.bounds;
const rootSnapshot = snapshots.get(rootId);
if (!rootBounds) {
return null;
}
return (
<Layout.Container gap="large">
<Typography.Title>Visualizer</Typography.Title>
<div
onMouseLeave={(e) => {
e.stopPropagation();
onHoverNode(undefined);
}}
style={{
/**
* This relative position is so the root visualization 2DNode and outer border has a non static element to
* position itself relative to.
*
* Subsequent Visualization2DNode are positioned relative to their parent as each one is position absolute
* which despite the name acts are a reference point for absolute positioning...
*/
position: 'relative',
width: toPx(rootBounds.width),
height: toPx(rootBounds.height),
overflow: 'hidden',
}}>
<OuterBorder />
{rootSnapshot ? (
<img
src={'data:image/jpeg;base64,' + rootSnapshot}
style={{maxWidth: '100%'}}
/>
) : null}
<Visualization2DNode
nodeId={root}
nodes={nodes}
snapshots={snapshots}
hoveredNode={hoveredNode}
selectedNode={selectedNode}
onSelectNode={onSelectNode}
onHoverNode={onHoverNode}
modifierPressed={modifierPressed}
<div
onMouseLeave={(e) => {
e.stopPropagation();
onHoverNode(undefined);
}}
style={{
/**
* This relative position is so the root visualization 2DNode and outer border has a non static element to
* position itself relative to.
*
* Subsequent Visualization2DNode are positioned relative to their parent as each one is position absolute
* which despite the name acts are a reference point for absolute positioning...
*/
position: 'relative',
width: toPx(rootBounds.width),
height: toPx(rootBounds.height),
overflow: 'hidden',
}}>
<OuterBorder />
{rootSnapshot ? (
<img
src={'data:image/jpeg;base64,' + rootSnapshot}
style={{maxWidth: '100%'}}
/>
</div>
</Layout.Container>
) : null}
<Visualization2DNode
nodeId={rootId}
nodes={nodes}
snapshots={snapshots}
hoveredNode={hoveredNode}
selectedNode={selectedNode}
onSelectNode={onSelectNode}
onHoverNode={onHoverNode}
modifierPressed={modifierPressed}
/>
</div>
);
};

View File

@@ -47,32 +47,31 @@ export function Component() {
if (rootId) {
return (
<>
<Layout.Horizontal grow>
<Layout.ScrollContainer>
<Layout.Horizontal>
<Tree
selectedNode={selectedNode}
onSelectNode={setSelectedNode}
onHoveredNode={setHoveredNode}
nodes={nodes}
rootId={rootId}
/>
<Visualization2D
root={rootId}
nodes={nodes}
snapshots={snapshots}
hoveredNode={hoveredNode}
onHoverNode={setHoveredNode}
selectedNode={selectedNode}
onSelectNode={setSelectedNode}
modifierPressed={ctrlPressed}
/>
</Layout.Horizontal>
<Tree
selectedNode={selectedNode}
hoveredNode={hoveredNode}
onSelectNode={setSelectedNode}
onHoveredNode={setHoveredNode}
nodes={nodes}
rootId={rootId}
/>
</Layout.ScrollContainer>
<Visualization2D
rootId={rootId}
nodes={nodes}
snapshots={snapshots}
hoveredNode={hoveredNode}
onHoverNode={setHoveredNode}
selectedNode={selectedNode}
onSelectNode={setSelectedNode}
modifierPressed={ctrlPressed}
/>
{selectedNode && renderSidebar(nodes.get(selectedNode))}
</>
</Layout.Horizontal>
);
}
return <div>Nothing yet</div>;
return <div>Loading...</div>;
}