Skip to content

Commit 6b9b38b

Browse files
committed
feat($active-header-links): simplify implementation
1 parent 5b0cdcf commit 6b9b38b

File tree

1 file changed

+6
-33
lines changed

1 file changed

+6
-33
lines changed

packages/@vuepress/plugin-active-header-links/mixin.js

+6-33
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ function calculateCurrentAnchor (anchors) {
2626
return anchors[0]
2727
}
2828

29-
function getAnchors () {
30-
const sidebarLinks = [].slice.call(document.querySelectorAll(AHL_SIDEBAR_LINK_SELECTOR))
29+
function getAnchors (sidebarLinks) {
3130
return [].slice
3231
.call(document.querySelectorAll(AHL_HEADER_ANCHOR_SELECTOR))
3332
.filter(anchor => sidebarLinks.some(sidebarLink => sidebarLink.hash === anchor.hash))
@@ -41,49 +40,23 @@ function getAnchors () {
4140
})
4241
}
4342

44-
let freezeScrollEvent = true
45-
4643
export default {
4744
mounted () {
48-
this.$router.beforeEach((to, from, next) => {
49-
if (to.path !== from.path) {
50-
freezeScrollEvent = true
51-
}
52-
next()
53-
})
54-
5545
this.$vuepress.$on('AsyncMarkdownContentMounted', (slotKey) => {
56-
// delay activation of scroll event
57-
setTimeout(() => {
58-
freezeScrollEvent = false
59-
}, 1000)
6046
if (slotKey === 'default') {
61-
window.addEventListener('scroll', () => this.onScroll(freezeScrollEvent))
47+
window.addEventListener('scroll', this.onScroll)
6248
}
6349
})
6450

6551
this.$vuepress.$on('AnchorHashChange', (anchor) => {
66-
// When user clicked sidebar links, we need to disable the scroll
67-
// event triggered.
68-
if (this.$route.hash === anchor.hash) {
69-
return
70-
}
71-
this.$vuepress.$set('disableScrollBehavior', true)
72-
this.$router.replace(decodeURIComponent(anchor.hash), () => {
73-
// execute after scrollBehavior handler.
74-
this.$nextTick(() => {
75-
this.$vuepress.$set('disableScrollBehavior', false)
76-
})
77-
})
52+
this.$router.replace(decodeURIComponent(anchor.hash))
7853
})
7954
},
8055

8156
methods: {
82-
onScroll: throttle(function (freezeScrollEvent) {
83-
if (freezeScrollEvent) {
84-
return
85-
}
86-
const anchors = getAnchors()
57+
onScroll: throttle(function () {
58+
this.$sidebarLinks = [].slice.call(document.querySelectorAll(AHL_SIDEBAR_LINK_SELECTOR))
59+
const anchors = getAnchors(this.$sidebarLinks)
8760
if (anchors.length === 0) {
8861
return
8962
}

0 commit comments

Comments
 (0)