11import React from 'react'
22import Branch from './branch.jsx'
3+ import { isElementVisible } from '../lib'
34
45class Tree extends React . Component {
56 constructor ( props ) {
67 super ( props )
78
89 this . onClose = this . onClose . bind ( this )
10+ this . onScroll = this . onScroll . bind ( this )
911
1012 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+ } )
1241 }
1342 }
1443
@@ -20,7 +49,7 @@ class Tree extends React.Component {
2049
2150 render ( ) {
2251 const { root } = this . props
23- const { show } = this . state
52+ const { show, visibleElement } = this . state
2453
2554 if ( ! show ) {
2655 return null
@@ -31,7 +60,7 @@ class Tree extends React.Component {
3160 < button onClick = { this . onClose } className = 'close_button' > ✖</ button >
3261 { root . list . map ( node => (
3362 < span key = { node . nodeLabel } >
34- < Branch { ...node } />
63+ < Branch { ...node } visibleElement = { visibleElement } />
3564 </ span >
3665 ) ) }
3766 </ div >
0 commit comments