Skip to content

Commit

Permalink
feat($active-header-links): simplify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz committed Nov 18, 2018
1 parent 5b0cdcf commit 6b9b38b
Showing 1 changed file with 6 additions and 33 deletions.
39 changes: 6 additions & 33 deletions packages/@vuepress/plugin-active-header-links/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ function calculateCurrentAnchor (anchors) {
return anchors[0]
}

function getAnchors () {
const sidebarLinks = [].slice.call(document.querySelectorAll(AHL_SIDEBAR_LINK_SELECTOR))
function getAnchors (sidebarLinks) {
return [].slice
.call(document.querySelectorAll(AHL_HEADER_ANCHOR_SELECTOR))
.filter(anchor => sidebarLinks.some(sidebarLink => sidebarLink.hash === anchor.hash))
Expand All @@ -41,49 +40,23 @@ function getAnchors () {
})
}

let freezeScrollEvent = true

export default {
mounted () {
this.$router.beforeEach((to, from, next) => {
if (to.path !== from.path) {
freezeScrollEvent = true
}
next()
})

this.$vuepress.$on('AsyncMarkdownContentMounted', (slotKey) => {
// delay activation of scroll event
setTimeout(() => {
freezeScrollEvent = false
}, 1000)
if (slotKey === 'default') {
window.addEventListener('scroll', () => this.onScroll(freezeScrollEvent))
window.addEventListener('scroll', this.onScroll)
}
})

this.$vuepress.$on('AnchorHashChange', (anchor) => {
// When user clicked sidebar links, we need to disable the scroll
// event triggered.
if (this.$route.hash === anchor.hash) {
return
}
this.$vuepress.$set('disableScrollBehavior', true)
this.$router.replace(decodeURIComponent(anchor.hash), () => {
// execute after scrollBehavior handler.
this.$nextTick(() => {
this.$vuepress.$set('disableScrollBehavior', false)
})
})
this.$router.replace(decodeURIComponent(anchor.hash))
})
},

methods: {
onScroll: throttle(function (freezeScrollEvent) {
if (freezeScrollEvent) {
return
}
const anchors = getAnchors()
onScroll: throttle(function () {
this.$sidebarLinks = [].slice.call(document.querySelectorAll(AHL_SIDEBAR_LINK_SELECTOR))
const anchors = getAnchors(this.$sidebarLinks)
if (anchors.length === 0) {
return
}
Expand Down

0 comments on commit 6b9b38b

Please sign in to comment.