mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-08-09 15:35:25 +02:00
Update table-of-content.js
This commit is contained in:
parent
9b4a53df01
commit
fe04c5b081
1 changed files with 25 additions and 2 deletions
|
@ -71,7 +71,7 @@ export default class TableOfContent {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find all tags on the page
|
* Find all section tags on the page
|
||||||
*
|
*
|
||||||
* @return {HTMLElement[]}
|
* @return {HTMLElement[]}
|
||||||
*/
|
*/
|
||||||
|
@ -79,9 +79,11 @@ export default class TableOfContent {
|
||||||
return Array.from(document.querySelectorAll(this.tagSelector));
|
return Array.from(document.querySelectorAll(this.tagSelector));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate top line position for each tag
|
||||||
|
*/
|
||||||
calculateBoundings() {
|
calculateBoundings() {
|
||||||
this.tagsSectionsMap = this.tags.map((tag) => {
|
this.tagsSectionsMap = this.tags.map((tag) => {
|
||||||
// calculate left upper corner of the tag
|
|
||||||
const rect = tag.getBoundingClientRect();
|
const rect = tag.getBoundingClientRect();
|
||||||
const top = rect.top + window.scrollY;
|
const top = rect.top + window.scrollY;
|
||||||
|
|
||||||
|
@ -92,23 +94,41 @@ export default class TableOfContent {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Watch active section while scrolling
|
||||||
|
*/
|
||||||
watchActiveSection() {
|
watchActiveSection() {
|
||||||
const detectSection = (scrollPosition) => {
|
const detectSection = (scrollPosition) => {
|
||||||
|
/**
|
||||||
|
* Find the nearest section above the scroll position
|
||||||
|
*/
|
||||||
const section = this.tagsSectionsMap.filter((tag) => {
|
const section = this.tagsSectionsMap.filter((tag) => {
|
||||||
return tag.top < scrollPosition;
|
return tag.top < scrollPosition;
|
||||||
}).pop();
|
}).pop();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If section found then set it as active
|
||||||
|
*/
|
||||||
if (section) {
|
if (section) {
|
||||||
const targetLink = section.tag.querySelector('a').getAttribute('href');
|
const targetLink = section.tag.querySelector('a').getAttribute('href');
|
||||||
|
|
||||||
this.setActiveLink(targetLink);
|
this.setActiveLink(targetLink);
|
||||||
} else {
|
} else {
|
||||||
|
/**
|
||||||
|
* Otherwise no active link will be highlighted
|
||||||
|
*/
|
||||||
this.setActiveLink();
|
this.setActiveLink();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define a flag to reduce number of calls to detectSection function
|
||||||
|
*/
|
||||||
let ticking;
|
let ticking;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scroll listener
|
||||||
|
*/
|
||||||
document.addEventListener('scroll', (event) => {
|
document.addEventListener('scroll', (event) => {
|
||||||
/**
|
/**
|
||||||
* Calculate scroll position
|
* Calculate scroll position
|
||||||
|
@ -128,6 +148,9 @@ export default class TableOfContent {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Call section detecting function
|
||||||
|
*/
|
||||||
if (!ticking) {
|
if (!ticking) {
|
||||||
window.requestAnimationFrame(() => {
|
window.requestAnimationFrame(() => {
|
||||||
detectSection(lastKnownScrollPosition);
|
detectSection(lastKnownScrollPosition);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue