Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quantum-kittens
GitHub Repository: quantum-kittens/platypus
Path: blob/main/frontend/ts/indexhighlighter.ts
3855 views
1
const threshold = 1
2
const windowTriggerMargins = '-50px 0px -66% 0px'
3
4
let activeSectionId = ''
5
6
const onActiveSectionChanged = function (prevSectionId: string, nextSectionId: string) {
7
const prevSectionLink = document.querySelector('a.c-sidebar__entry.c-sidebar__entry--progress') as HTMLAnchorElement
8
const nextSectionLink = document.querySelector(`a.c-sidebar__entry[href="#${nextSectionId}"]`) as HTMLAnchorElement
9
const activeClass = ' c-sidebar__entry--active c-sidebar__entry--progress '
10
11
if (nextSectionLink) {
12
if (prevSectionLink) {
13
prevSectionLink.className = (prevSectionLink.className.replace(activeClass, ''))
14
}
15
nextSectionLink.className += activeClass
16
17
const linkOffset = nextSectionLink.offsetTop
18
const sectionId = window.textbook?.course?.section
19
const connection = document.querySelector(`[data-section-id="${sectionId}"] .connection--progress`) as HTMLDivElement
20
if (connection) {
21
connection.style.height = `${linkOffset}px`
22
}
23
}
24
}
25
26
const onSectionVisibility = function (entries: Array<IntersectionObserverEntry>) {
27
let highestTopValue = Infinity
28
29
entries.forEach((entry) => {
30
const { target, boundingClientRect, rootBounds } = entry
31
32
if (rootBounds) {
33
const targetTop = boundingClientRect.top
34
const triggerWindowBottom = rootBounds.bottom
35
const onTop = targetTop >= 0 && targetTop <= triggerWindowBottom
36
37
if (onTop && targetTop < highestTopValue) {
38
if (target.id && activeSectionId !== target.id) {
39
onActiveSectionChanged(activeSectionId, target.id)
40
activeSectionId = target.id
41
}
42
highestTopValue = targetTop
43
}
44
}
45
})
46
}
47
48
const initIndexHighlight = function () {
49
const observer = new IntersectionObserver(
50
onSectionVisibility,
51
{
52
root: null,
53
rootMargin: windowTriggerMargins,
54
threshold
55
}
56
)
57
58
document.querySelectorAll(':is(h1, h2, h3, h4) > a[id]')
59
.forEach((section) => {
60
(observer as IntersectionObserver).observe(section)
61
})
62
63
window.addEventListener('beforeunload', function (e) {
64
observer && observer.disconnect()
65
})
66
}
67
68
export {
69
initIndexHighlight
70
}
71
72