2021-09-23 14:16:37 +08:00
|
|
|
import { onMounted, onUnmounted, onUpdated } from 'vue'
|
2021-09-17 00:18:50 +08:00
|
|
|
import { throttleAndDebounce } from '../utils'
|
|
|
|
|
|
|
|
import type { Ref } from 'vue'
|
|
|
|
|
|
|
|
export function useActiveSidebarLinks(
|
|
|
|
container: Ref<HTMLElement>,
|
|
|
|
marker: Ref<HTMLElement>
|
|
|
|
) {
|
|
|
|
const onScroll = throttleAndDebounce(setActiveLink, 150)
|
|
|
|
function setActiveLink() {
|
|
|
|
const sidebarLinks = getSidebarLinks()
|
|
|
|
const anchors = getAnchors(sidebarLinks)
|
|
|
|
|
|
|
|
if (
|
|
|
|
anchors.length &&
|
|
|
|
window.scrollY + window.innerHeight === document.body.offsetHeight
|
|
|
|
) {
|
|
|
|
activateLink(anchors[anchors.length - 1].hash)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for (let i = 0; i < anchors.length; i++) {
|
|
|
|
const anchor = anchors[i]
|
|
|
|
const nextAnchor = anchors[i + 1]
|
|
|
|
const [isActive, hash] = isAnchorActive(i, anchor, nextAnchor)
|
|
|
|
if (isActive) {
|
|
|
|
history.replaceState(
|
|
|
|
null,
|
|
|
|
document.title,
|
|
|
|
hash ? (hash as string) : ' '
|
|
|
|
)
|
|
|
|
activateLink(hash as string)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let prevActiveLink: HTMLAnchorElement | null = null
|
|
|
|
|
|
|
|
function activateLink(hash: string) {
|
|
|
|
deactiveLink(prevActiveLink)
|
|
|
|
|
|
|
|
const activeLink = (prevActiveLink =
|
|
|
|
hash == null
|
|
|
|
? null
|
|
|
|
: (container.value.querySelector(
|
|
|
|
`.toc-item a[href="${decodeURIComponent(hash)}"]`
|
|
|
|
) as HTMLAnchorElement))
|
|
|
|
if (activeLink) {
|
|
|
|
activeLink.classList.add('active')
|
|
|
|
marker.value.style.opacity = '1'
|
2021-09-23 14:16:37 +08:00
|
|
|
marker.value.style.top = `${activeLink.offsetTop}px`
|
2021-09-17 00:18:50 +08:00
|
|
|
} else {
|
|
|
|
marker.value.style.opacity = '0'
|
|
|
|
marker.value.style.top = '33px'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-30 19:00:11 +08:00
|
|
|
function deactiveLink(link: HTMLElement | null) {
|
2021-09-17 00:18:50 +08:00
|
|
|
link && link.classList.remove('active')
|
|
|
|
}
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
window.requestAnimationFrame(setActiveLink)
|
|
|
|
window.addEventListener('scroll', onScroll)
|
|
|
|
})
|
|
|
|
|
|
|
|
onUpdated(() => {
|
|
|
|
activateLink(location.hash)
|
|
|
|
})
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
window.removeEventListener('scroll', onScroll)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
function getSidebarLinks() {
|
|
|
|
return Array.from(
|
|
|
|
document.querySelectorAll('.toc-content .toc-link')
|
|
|
|
) as HTMLAnchorElement[]
|
|
|
|
}
|
|
|
|
function getAnchors(sidebarLinks: HTMLAnchorElement[]) {
|
|
|
|
return (
|
|
|
|
Array.from(
|
|
|
|
document.querySelectorAll('.doc-content .header-anchor')
|
|
|
|
) as HTMLAnchorElement[]
|
|
|
|
).filter((anchor) =>
|
|
|
|
sidebarLinks.some((sidebarLink) => sidebarLink.hash === anchor.hash)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
function getPageOffset() {
|
|
|
|
return (document.querySelector('.navbar') as HTMLElement).offsetHeight
|
|
|
|
}
|
|
|
|
function getAnchorTop(anchor: HTMLAnchorElement) {
|
|
|
|
const pageOffset = getPageOffset()
|
|
|
|
try {
|
2021-12-30 19:00:11 +08:00
|
|
|
return anchor.parentElement!.offsetTop - pageOffset - 15
|
2021-09-17 00:18:50 +08:00
|
|
|
} catch (e) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function isAnchorActive(
|
|
|
|
index: number,
|
|
|
|
anchor: HTMLAnchorElement,
|
|
|
|
nextAnchor: HTMLAnchorElement
|
|
|
|
) {
|
|
|
|
const scrollTop = window.scrollY
|
|
|
|
if (index === 0 && scrollTop === 0) {
|
|
|
|
return [true, null]
|
|
|
|
}
|
|
|
|
if (scrollTop < getAnchorTop(anchor)) {
|
|
|
|
return [false, null]
|
|
|
|
}
|
|
|
|
if (!nextAnchor || scrollTop < getAnchorTop(nextAnchor)) {
|
|
|
|
return [true, decodeURIComponent(anchor.hash)]
|
|
|
|
}
|
|
|
|
return [false, null]
|
|
|
|
}
|