Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/client/theme-default/composables/outline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ export function useActiveAnchor(
const onScroll = throttleAndDebounce(setActiveLink, 100)

let prevActiveLink: HTMLAnchorElement | null = null
let ignoreScrollOnce: boolean = false

onMounted(() => {
requestAnimationFrame(setActiveLink)
window.addEventListener('scroll', onScroll)
container.value.addEventListener('click', onClick)
})

onUpdated(() => {
Expand All @@ -98,13 +100,33 @@ export function useActiveAnchor(

onUnmounted(() => {
window.removeEventListener('scroll', onScroll)
container.value.removeEventListener('click', onClick)
})

function onClick(e: MouseEvent) {
if (!isAsideEnabled.value) {
return
}

const hash =
e.target instanceof Element ? e.target.closest('a')?.hash : null

if (hash) {
ignoreScrollOnce = true
activateLink(hash)
}
}

function setActiveLink() {
if (!isAsideEnabled.value) {
return
}

if (ignoreScrollOnce) {
ignoreScrollOnce = false
return
}

const scrollY = window.scrollY
const innerHeight = window.innerHeight
const offsetHeight = document.body.offsetHeight
Expand Down Expand Up @@ -159,7 +181,7 @@ export function useActiveAnchor(
prevActiveLink = null
} else {
prevActiveLink = container.value.querySelector(
`a[href="${decodeURIComponent(hash)}"]`
`a[href$="${decodeURIComponent(hash)}"]`
)
}

Expand Down
Loading