@@ -90,36 +90,22 @@ export class TableOfContents implements OnInit {
90
90
}
91
91
}
92
92
93
- /** Gets the scroll offset of the scroll container */
94
- private getScrollOffset ( ) : number {
95
- const { top} = this . _element . nativeElement . getBoundingClientRect ( ) ;
96
- if ( typeof this . _scrollContainer . scrollTop !== 'undefined' ) {
97
- return this . _scrollContainer . scrollTop + top ;
98
- } else if ( typeof this . _scrollContainer . pageYOffset !== 'undefined' ) {
99
- return this . _scrollContainer . pageYOffset + top ;
100
- }
101
- }
102
-
93
+ /** Gets links generated from header selectors. */
103
94
private createLinks ( ) : Link [ ] {
104
- const links : Link [ ] = [ ] ;
105
95
const headers =
106
96
Array . from ( this . _document . querySelectorAll ( this . headerSelectors ) ) as HTMLElement [ ] ;
107
97
108
- if ( headers . length ) {
109
- for ( const header of headers ) {
110
- // remove the 'link' icon name from the inner text
111
- const name = header . innerText . trim ( ) . replace ( / ^ l i n k / , '' ) ;
112
- const { top} = header . getBoundingClientRect ( ) ;
113
- links . push ( {
114
- name,
115
- top,
116
- type : header . tagName . toLowerCase ( ) ,
117
- id : header . id
118
- } ) ;
119
- }
120
- }
121
-
122
- return links ;
98
+ return headers . map ( header => {
99
+ // remove the 'link' icon name from the inner text
100
+ const name = header . innerText . trim ( ) . replace ( / ^ l i n k / , '' ) ;
101
+ const { top} = header . getBoundingClientRect ( ) ;
102
+ return {
103
+ name,
104
+ top,
105
+ type : header . tagName . toLowerCase ( ) ,
106
+ id : header . id
107
+ } ;
108
+ } ) ;
123
109
}
124
110
125
111
private onScroll ( ) : void {
@@ -134,4 +120,18 @@ export class TableOfContents implements OnInit {
134
120
return scrollOffset >= currentLink . top && ! ( nextLink && nextLink . top < scrollOffset ) ;
135
121
}
136
122
123
+ /** Gets the scroll offset of the scroll container */
124
+ private getScrollOffset ( ) : number {
125
+ const { top} = this . _element . nativeElement . getBoundingClientRect ( ) ;
126
+ if ( this . _scrollContainer . scrollTop != null ) {
127
+ return this . _scrollContainer . scrollTop + top ;
128
+ }
129
+
130
+ if ( this . _scrollContainer . pageYOffset != null ) {
131
+ return this . _scrollContainer . pageYOffset + top ;
132
+ }
133
+
134
+ return 0 ;
135
+ }
136
+
137
137
}
0 commit comments