@@ -14,9 +14,6 @@ interface Link {
14
14
/* header type h3/h4 */
15
15
type : string ;
16
16
17
- /* If the anchor is in view of the page */
18
- active : boolean ;
19
-
20
17
/* name of the anchor */
21
18
name : string ;
22
19
@@ -27,14 +24,15 @@ interface Link {
27
24
@Component ( {
28
25
selector : 'table-of-contents' ,
29
26
styleUrls : [ './table-of-contents.scss' ] ,
30
- templateUrl : './table-of-contents.html'
27
+ templateUrl : './table-of-contents.html' ,
31
28
} )
32
29
export class TableOfContents implements OnInit {
33
30
34
31
@Input ( ) links : Link [ ] = [ ] ;
35
32
@Input ( ) container : string ;
36
33
@Input ( ) headerSelectors = '.docs-markdown-h3,.docs-markdown-h4' ;
37
34
35
+ _activeLinkIndex : number ;
38
36
_rootUrl : string ;
39
37
private _scrollContainer : any ;
40
38
private _destroyed = new Subject ( ) ;
@@ -103,7 +101,7 @@ export class TableOfContents implements OnInit {
103
101
}
104
102
105
103
private createLinks ( ) : Link [ ] {
106
- const links = [ ] ;
104
+ const links : Link [ ] = [ ] ;
107
105
const headers =
108
106
Array . from ( this . _document . querySelectorAll ( this . headerSelectors ) ) as HTMLElement [ ] ;
109
107
@@ -114,10 +112,9 @@ export class TableOfContents implements OnInit {
114
112
const { top} = header . getBoundingClientRect ( ) ;
115
113
links . push ( {
116
114
name,
115
+ top,
117
116
type : header . tagName . toLowerCase ( ) ,
118
- top : top ,
119
- id : header . id ,
120
- active : false
117
+ id : header . id
121
118
} ) ;
122
119
}
123
120
}
@@ -126,9 +123,8 @@ export class TableOfContents implements OnInit {
126
123
}
127
124
128
125
private onScroll ( ) : void {
129
- for ( let i = 0 ; i < this . links . length ; i ++ ) {
130
- this . links [ i ] . active = this . isLinkActive ( this . links [ i ] , this . links [ i + 1 ] ) ;
131
- }
126
+ this . _activeLinkIndex = this . links
127
+ . findIndex ( ( link , i ) => this . isLinkActive ( link , this . links [ i + 1 ] ) ) ;
132
128
}
133
129
134
130
private isLinkActive ( currentLink : any , nextLink : any ) : boolean {
0 commit comments