@@ -19,29 +19,36 @@ function TreeView({
19
19
viewClassName : string ;
20
20
nodeMap : DevToolsTree ;
21
21
} ) : JSX . Element {
22
- const depthFirstSearch = ( node : DevToolsNode ) : DevToolsNode => {
22
+ // takes flat JSON structure, nests child comments inside parents
23
+ const depthFirstSearch = (
24
+ map : DevToolsTree = nodeMap ,
25
+ nodeKey = 'root' ,
26
+ depth = 0 ,
27
+ ) : DevToolsNode => {
28
+ const node = map [ nodeKey ] ;
23
29
const children : Array < DevToolsNode > = [ ] ;
24
30
25
31
if ( Object . prototype . hasOwnProperty . call ( node , '__children' ) ) {
26
32
node . __children . forEach ( ( childKey : string ) => {
27
- const child = nodeMap [ childKey ] ;
28
- children . push ( depthFirstSearch ( child ) ) ;
33
+ children . push ( depthFirstSearch ( map , childKey , depth + 1 ) ) ;
29
34
} ) ;
30
35
}
31
36
32
- return { ...node , __type : node . __type , children, lexicalKey : node . __key } ;
37
+ return {
38
+ ...node ,
39
+ __type : node . __type ,
40
+ children,
41
+ depth,
42
+ lexicalKey : node . __key ,
43
+ } ;
33
44
} ;
34
45
35
46
const generateTree = ( map : DevToolsTree ) : JSX . Element => {
36
- const root = depthFirstSearch ( map . root ) ;
47
+ const root = depthFirstSearch ( nodeMap , ' root' , 0 ) ;
37
48
return < TreeNode { ...root } /> ;
38
49
} ;
39
50
40
- return (
41
- < div className = { viewClassName } >
42
- < ul > { generateTree ( nodeMap ) } </ ul >
43
- </ div >
44
- ) ;
51
+ return < div className = { viewClassName } > { generateTree ( nodeMap ) } </ div > ;
45
52
}
46
53
47
54
export default TreeView ;
0 commit comments