1
1
import React , { useContext , useEffect , useRef } from 'react' ;
2
2
import { FixedSizeList } from 'react-window' ;
3
+ import PubSub from 'pubsub-js' ;
3
4
import { ACTIONS } from '../../actions' ;
4
5
import { useKeyBindings } from '../../hooks/useKeyBindings' ;
5
6
import { DispatchContext , renderProvider , StateContext } from '../../pages/sidebar' ;
6
7
import Navigation from '../../service/Navigation' ;
7
8
import { INode } from '../../Tree' ;
8
9
import Node from './Node/Node' ;
10
+ import { PubSubEvents } from '../../constants/PubSubEvents' ;
9
11
10
12
interface IProps {
11
13
}
@@ -27,24 +29,38 @@ const Tree: React.FC<IProps> = () => {
27
29
if ( message . type === "onActiveTextEditor" ) {
28
30
const node = renderProvider . filePathMap . get ( message . value ) ;
29
31
if ( node ) {
30
- listRef . current ?. scrollToItem ( node . index , "smart" ) ;
31
- dispatch ( {
32
- type : ACTIONS . UPDATE_ACTIVE_NODE ,
33
- payload : node . id
34
- } ) ;
32
+ scrollToNode ( node ) ;
35
33
}
36
34
}
37
35
} ) ;
36
+ const subscription = PubSub . subscribe ( PubSubEvents . SCROLL_TO_NODE , ( _ , nodeId ) => {
37
+ const node = renderProvider . rowMap . get ( nodeId ) ;
38
+ if ( node ) {
39
+ scrollToNode ( node ) ;
40
+ }
41
+ } )
42
+ return ( ) => {
43
+ PubSub . unsubscribe ( subscription ) ;
44
+ }
38
45
} , [ ] ) ;
39
46
47
+ const scrollToNode = ( node : INode ) => {
48
+ if ( node ) {
49
+ listRef . current ?. scrollToItem ( node . index , "smart" ) ;
50
+ dispatch ( {
51
+ type : ACTIONS . UPDATE_ACTIVE_NODE ,
52
+ payload : node . id
53
+ } ) ;
54
+ }
55
+ } ;
56
+
40
57
useKeyBindings ( ( keyCode ) => {
41
58
const node = focussedNode ? renderProvider . rowMap . get ( focussedNode as string ) : null ;
42
- console . log ( focussedNode , node )
43
59
if ( node ) {
44
60
navigateHandler ( keyCode , node ) ;
45
61
}
46
62
} ,
47
- [ 'ArrowUp' , 'ArrowDown' , 'ArrowRight' , 'ArrowLeft' ] , focussedNode ) ;
63
+ [ 'ArrowUp' , 'ArrowDown' , 'ArrowRight' , 'ArrowLeft' , 'Enter' ] , focussedNode ) ;
48
64
49
65
50
66
return ( < FixedSizeList
@@ -96,14 +112,21 @@ const navigateHandler = (keyCode, node: INode) => {
96
112
break ;
97
113
}
98
114
case 'ArrowUp' : {
99
- Navigation . moveUp ( node )
115
+ Navigation . moveUp ( node ) ;
100
116
break ;
101
117
}
102
118
case 'ArrowDown' : {
103
- Navigation . moveDown ( node )
119
+ Navigation . moveDown ( node ) ;
120
+ break ;
121
+ }
122
+ case 'Enter' : {
123
+ renderProvider . visitNode ( node ) ;
124
+ break ;
125
+ }
126
+ default : {
104
127
break ;
105
128
}
106
129
}
107
- }
130
+ } ;
108
131
109
- export default Tree ;
132
+ export default Tree ;
0 commit comments