docs_vue2/plugins/target.js

42 lines
878 B
JavaScript
Raw Normal View History

2021-10-20 10:59:36 +08:00
let target
2021-10-20 10:59:36 +08:00
function clearTarget () {
if (!target) {
return
}
2021-10-20 10:59:36 +08:00
delete target.dataset.target
}
export default ({ app }) => {
2021-10-20 10:59:36 +08:00
document.documentElement.addEventListener('click', clearTarget)
app.router.afterEach(to => {
2021-10-20 10:59:36 +08:00
clearTarget()
if (!to.hash) {
return
}
setTimeout(() => {
const selector = decodeURIComponent(to.hash)
const anchor = document.querySelector(selector)
if (!anchor) {
return
}
if (anchor.tagName === 'CODE') {
target = anchor.closest('tr')
} else if (app.router.currentRoute.name === 'changelog' && anchor.tagName === 'H2') {
target = anchor.closest('.version-item')
2022-05-26 19:16:47 +08:00
} else if (['H1', 'H2', 'H3', 'H4', 'H5', 'H6'].includes(anchor.tagName)) {
target = anchor
}
if (target) {
target.dataset.target = ''
}
}, 0)
})
}