refactor(theme): improve robustness and readability of outline component

pull/3368/head
Yuxuan Zhang 2 years ago
parent 93122eee20
commit f92a68e12f

@ -6,8 +6,11 @@ import { throttleAndDebounce } from '../support/utils'
// magic number to avoid repeated retrieval // magic number to avoid repeated retrieval
const PAGE_OFFSET = 71 const PAGE_OFFSET = 71
// cached list of anchor elements from resolveHeaders
const resolvedHeaders: { element: HTMLHeadElement; link: string }[] = []
export type MenuItem = Omit<Header, 'slug' | 'children'> & { export type MenuItem = Omit<Header, 'slug' | 'children'> & {
element: HTMLHeadElement
children?: MenuItem[] children?: MenuItem[]
} }
@ -29,6 +32,7 @@ export function getHeaders(range: DefaultTheme.Config['outline']) {
.map((el) => { .map((el) => {
const level = Number(el.tagName[1]) const level = Number(el.tagName[1])
return { return {
element: el as HTMLHeadElement,
title: serializeHeader(el), title: serializeHeader(el),
link: '#' + el.id, link: '#' + el.id,
level level
@ -78,6 +82,12 @@ export function resolveHeaders(
: levelsRange : levelsRange
headers = headers.filter((h) => h.level >= high && h.level <= low) headers = headers.filter((h) => h.level >= high && h.level <= low)
// clear previous caches
resolvedHeaders.length = 0
// update global header list for active link rendering
for (const { element, link } of headers) {
resolvedHeaders.push({ element, link })
}
const ret: MenuItem[] = [] const ret: MenuItem[] = []
outer: for (let i = 0; i < headers.length; i++) { outer: for (let i = 0; i < headers.length; i++) {
@ -128,40 +138,48 @@ export function useActiveAnchor(
return return
} }
const links = [].slice.call(
container.value.querySelectorAll('.outline-link')
) as HTMLAnchorElement[]
const anchors = [].slice
.call(document.querySelectorAll('.content .header-anchor'))
.filter((anchor: HTMLAnchorElement) => {
return links.some((link) => {
return link.hash === anchor.hash && anchor.offsetParent !== null
})
}) as HTMLAnchorElement[]
const scrollY = window.scrollY const scrollY = window.scrollY
const innerHeight = window.innerHeight const innerHeight = window.innerHeight
const offsetHeight = document.body.offsetHeight const offsetHeight = document.body.offsetHeight
const isBottom = Math.abs(scrollY + innerHeight - offsetHeight) < 1 const isBottom = Math.abs(scrollY + innerHeight - offsetHeight) < 1
// page bottom - highlight last one // resolvedHeaders may be repositioned, hidden or fix positioned
if (anchors.length && isBottom) { const headers = resolvedHeaders
activateLink(anchors[anchors.length - 1].hash) .map(({ element, link }) => ({
link,
top: getAbsoluteTop(element)
}))
.filter(({ top }) => !Number.isNaN(top))
.sort((a, b) => a.top - b.top)
// no headers available for active link
if (!headers.length) {
activateLink(null)
return return
} }
for (let i = 0; i < anchors.length; i++) { // page top
const anchor = anchors[i] if (scrollY < 1) {
const nextAnchor = anchors[i + 1] activateLink(null)
return
const [isActive, hash] = isAnchorActive(i, anchor, nextAnchor) }
if (isActive) { // page bottom - highlight last link
activateLink(hash) if (isBottom) {
activateLink(headers[headers.length - 1].link)
return return
} }
// find the last header above the top of viewport
let activeLink: string | null = null
for (const { link, top } of headers) {
if (top + PAGE_OFFSET > scrollY) {
break
} }
activeLink = link
}
activateLink(activeLink)
} }
function activateLink(hash: string | null) { function activateLink(hash: string | null) {
@ -190,28 +208,18 @@ export function useActiveAnchor(
} }
} }
function getAnchorTop(anchor: HTMLAnchorElement): number { function getAbsoluteTop(element: HTMLElement): number {
return anchor.parentElement!.offsetTop - PAGE_OFFSET let offsetTop = 0
} while (element !== document.body) {
if (element === null) {
function isAnchorActive( // child element is:
index: number, // - not attached to the DOM (display: none)
anchor: HTMLAnchorElement, // - set to fixed position (not scrollable)
nextAnchor: HTMLAnchorElement | undefined // - body or html element (null offsetParent)
): [boolean, string | null] { return NaN
const scrollTop = window.scrollY
if (index === 0 && scrollTop === 0) {
return [true, null]
}
if (scrollTop < getAnchorTop(anchor)) {
return [false, null]
} }
offsetTop += element.offsetTop
if (!nextAnchor || scrollTop < getAnchorTop(nextAnchor)) { element = element.offsetParent as HTMLElement
return [true, anchor.hash]
} }
return offsetTop
return [false, null]
} }

Loading…
Cancel
Save