1
1
import React from 'react'
2
2
import Branch from './branch.jsx'
3
+ import { isElementVisible } from '../lib'
3
4
4
5
class Tree extends React . Component {
5
6
constructor ( props ) {
6
7
super ( props )
7
8
8
9
this . onClose = this . onClose . bind ( this )
10
+ this . onScroll = this . onScroll . bind ( this )
9
11
10
12
this . state = {
11
- show : true
13
+ show : true ,
14
+ visibleElement : null
15
+ }
16
+ }
17
+
18
+ componentDidMount ( ) {
19
+ window . addEventListener ( 'DOMContentLoaded' , this . onScroll , false )
20
+ window . addEventListener ( 'load' , this . onScroll , false )
21
+ window . addEventListener ( 'scroll' , this . onScroll , false )
22
+ window . addEventListener ( 'resize' , this . onScroll , false )
23
+ }
24
+
25
+ componentWillUnmount ( ) {
26
+ window . removeEventListener ( 'DOMContentLoaded' , this . onScroll , false )
27
+ window . removeEventListener ( 'load' , this . onScroll , false )
28
+ window . removeEventListener ( 'scroll' , this . onScroll , false )
29
+ window . removeEventListener ( 'resize' , this . onScroll , false )
30
+ }
31
+
32
+ onScroll ( ) {
33
+ const { visibleElement } = this . state
34
+ const { root } = this . props
35
+ const { diffElements = [ ] } = root
36
+ const nextVisibleElement = diffElements . find ( isElementVisible )
37
+ if ( nextVisibleElement !== visibleElement ) {
38
+ this . setState ( {
39
+ visibleElement : nextVisibleElement
40
+ } )
12
41
}
13
42
}
14
43
@@ -20,7 +49,7 @@ class Tree extends React.Component {
20
49
21
50
render ( ) {
22
51
const { root } = this . props
23
- const { show } = this . state
52
+ const { show, visibleElement } = this . state
24
53
25
54
if ( ! show ) {
26
55
return null
@@ -31,7 +60,7 @@ class Tree extends React.Component {
31
60
< button onClick = { this . onClose } className = 'close_button' > ✖</ button >
32
61
{ root . list . map ( node => (
33
62
< span key = { node . nodeLabel } >
34
- < Branch { ...node } />
63
+ < Branch { ...node } visibleElement = { visibleElement } />
35
64
</ span >
36
65
) ) }
37
66
</ div >
0 commit comments