Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Commit 7941daa

Browse files
committed
Refactor a little logic for cleaner readability
1 parent 7f657ec commit 7941daa

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

src/app/shared/table-of-contents/table-of-contents.ts

+26-26
Original file line numberDiff line numberDiff line change
@@ -90,36 +90,22 @@ export class TableOfContents implements OnInit {
9090
}
9191
}
9292

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. */
10394
private createLinks(): Link[] {
104-
const links: Link[] = [];
10595
const headers =
10696
Array.from(this._document.querySelectorAll(this.headerSelectors)) as HTMLElement[];
10797

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(/^link/, '');
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(/^link/, '');
101+
const {top} = header.getBoundingClientRect();
102+
return {
103+
name,
104+
top,
105+
type: header.tagName.toLowerCase(),
106+
id: header.id
107+
};
108+
});
123109
}
124110

125111
private onScroll(): void {
@@ -134,4 +120,18 @@ export class TableOfContents implements OnInit {
134120
return scrollOffset >= currentLink.top && !(nextLink && nextLink.top < scrollOffset);
135121
}
136122

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+
137137
}

0 commit comments

Comments
 (0)