Added chevon icon when expanding / collapsing
Reviewed By: lblasa Differential Revision: D41838170 fbshipit-source-id: f04438d77445736e45e29e2c46e9e8f8dd0906a8
This commit is contained in:
committed by
Facebook GitHub Bot
parent
a6544489f3
commit
2bdb068531
@@ -6,6 +6,7 @@
|
|||||||
*
|
*
|
||||||
* @format
|
* @format
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {Id, UINode} from '../types';
|
import {Id, UINode} from '../types';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import {
|
import {
|
||||||
@@ -18,6 +19,7 @@ import {
|
|||||||
useValue,
|
useValue,
|
||||||
} from 'flipper-plugin';
|
} from 'flipper-plugin';
|
||||||
import {plugin} from '../index';
|
import {plugin} from '../index';
|
||||||
|
import {Glyph} from 'flipper';
|
||||||
|
|
||||||
export function Tree2({
|
export function Tree2({
|
||||||
nodes,
|
nodes,
|
||||||
@@ -56,6 +58,7 @@ export function Tree2({
|
|||||||
|
|
||||||
export type TreeNode = UINode & {
|
export type TreeNode = UINode & {
|
||||||
depth: number;
|
depth: number;
|
||||||
|
isExpanded: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
function TreeItemContainer({
|
function TreeItemContainer({
|
||||||
@@ -69,6 +72,7 @@ function TreeItemContainer({
|
|||||||
hoveredNode?: Id;
|
hoveredNode?: Id;
|
||||||
onSelectNode: (node?: Id) => void;
|
onSelectNode: (node?: Id) => void;
|
||||||
}) {
|
}) {
|
||||||
|
const instance = usePlugin(plugin);
|
||||||
return (
|
return (
|
||||||
<TreeItem
|
<TreeItem
|
||||||
isSelected={treeNode.id === selectedNode}
|
isSelected={treeNode.id === selectedNode}
|
||||||
@@ -77,8 +81,20 @@ function TreeItemContainer({
|
|||||||
onSelectNode(treeNode.id);
|
onSelectNode(treeNode.id);
|
||||||
}}
|
}}
|
||||||
item={treeNode}>
|
item={treeNode}>
|
||||||
{/*{arrow}*/}
|
<Arrow
|
||||||
{defaultIcon(treeNode)}
|
expanded={treeNode.isExpanded}
|
||||||
|
onClick={() => {
|
||||||
|
instance.uiState.expandedNodes.update((draft) => {
|
||||||
|
if (draft.has(treeNode.id)) {
|
||||||
|
draft.delete(treeNode.id);
|
||||||
|
} else {
|
||||||
|
draft.add(treeNode.id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{nodeIcon(treeNode)}
|
||||||
<HighlightedText text={treeNode.name} />
|
<HighlightedText text={treeNode.name} />
|
||||||
</TreeItem>
|
</TreeItem>
|
||||||
);
|
);
|
||||||
@@ -100,12 +116,29 @@ const TreeItem = styled.li<{
|
|||||||
backgroundColor: isSelected ? theme.selectionBackgroundColor : theme.white,
|
backgroundColor: isSelected ? theme.selectionBackgroundColor : theme.white,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
function Arrow(props: {onClick: () => void; expanded: boolean}) {
|
||||||
|
return (
|
||||||
|
<div style={{display: 'flex'}} onClick={props.onClick}>
|
||||||
|
<Glyph
|
||||||
|
style={{
|
||||||
|
transform: props.expanded ? 'rotate(90deg)' : '',
|
||||||
|
marginRight: '4px',
|
||||||
|
marginBottom: props.expanded ? '2px' : '',
|
||||||
|
}}
|
||||||
|
name="chevron-right"
|
||||||
|
size={12}
|
||||||
|
color="grey"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function HighlightedText(props: {text: string}) {
|
function HighlightedText(props: {text: string}) {
|
||||||
const highlightManager: HighlightManager = useHighlighter();
|
const highlightManager: HighlightManager = useHighlighter();
|
||||||
return <span>{highlightManager.render(props.text)}</span>;
|
return <span>{highlightManager.render(props.text)}</span>;
|
||||||
}
|
}
|
||||||
|
|
||||||
function defaultIcon(node: UINode) {
|
function nodeIcon(node: UINode) {
|
||||||
if (node.tags.includes('Litho')) {
|
if (node.tags.includes('Litho')) {
|
||||||
return <DecorationImage src="icons/litho-logo.png" />;
|
return <DecorationImage src="icons/litho-logo.png" />;
|
||||||
}
|
}
|
||||||
@@ -122,21 +155,27 @@ const renderDepthOffset = 4;
|
|||||||
function toTreeList(
|
function toTreeList(
|
||||||
nodes: Map<Id, UINode>,
|
nodes: Map<Id, UINode>,
|
||||||
rootId: Id,
|
rootId: Id,
|
||||||
expanded: Set<Id>,
|
expandedNodes: Set<Id>,
|
||||||
): TreeNode[] {
|
): TreeNode[] {
|
||||||
const stack = [[nodes.get(rootId), 0]] as [UINode, number][];
|
const root = nodes.get(rootId);
|
||||||
|
if (root == null) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
const stack = [[root, 0]] as [UINode, number][];
|
||||||
|
|
||||||
const res = [] as TreeNode[];
|
const res = [] as TreeNode[];
|
||||||
|
|
||||||
while (stack.length > 0) {
|
while (stack.length > 0) {
|
||||||
const [cur, depth] = stack.pop()!!;
|
const [cur, depth] = stack.pop()!!;
|
||||||
|
|
||||||
|
const isExpanded = expandedNodes.has(cur.id);
|
||||||
res.push({
|
res.push({
|
||||||
...cur,
|
...cur,
|
||||||
depth,
|
depth,
|
||||||
|
isExpanded,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (expanded.has(cur.id)) {
|
if (isExpanded) {
|
||||||
for (const childId of cur.children) {
|
for (const childId of cur.children) {
|
||||||
const child = nodes.get(childId);
|
const child = nodes.get(childId);
|
||||||
if (child != null) {
|
if (child != null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user