fix: fix scroll to hash on new tab during dev

close #653
pull/2040/head
Evan You 2 years ago
parent 470ce3d3f3
commit 9aafc88d59

@ -9,7 +9,7 @@ import {
} from 'vue' } from 'vue'
import RawTheme from '@theme/index' import RawTheme from '@theme/index'
import { inBrowser, pathToFile } from './utils.js' import { inBrowser, pathToFile } from './utils.js'
import { type Router, RouterSymbol, createRouter } from './router.js' import { type Router, RouterSymbol, createRouter, scrollTo } from './router.js'
import { siteDataRef, useData } from './data.js' import { siteDataRef, useData } from './data.js'
import { useUpdateHead } from './composables/head.js' import { useUpdateHead } from './composables/head.js'
import { usePrefetch } from './composables/preFetch.js' import { usePrefetch } from './composables/preFetch.js'
@ -149,6 +149,14 @@ if (inBrowser) {
// dynamically update head tags // dynamically update head tags
useUpdateHead(router.route, data.site) useUpdateHead(router.route, data.site)
app.mount('#app') app.mount('#app')
// scroll to hash on new tab during dev
if (import.meta.env.DEV && location.hash) {
const target = document.querySelector(location.hash)
if (target) {
scrollTo(target, location.hash)
}
}
}) })
}) })
} }

@ -229,8 +229,8 @@ export function useRoute(): Route {
return useRouter().route return useRouter().route
} }
function scrollTo(el: HTMLElement | SVGElement, hash: string, smooth = false) { export function scrollTo(el: Element, hash: string, smooth = false) {
let target: HTMLElement | SVGElement | null = null let target: Element | null = null
try { try {
target = el.classList.contains('header-anchor') target = el.classList.contains('header-anchor')

Loading…
Cancel
Save