Skip to content

Commit

Permalink
fix: change route path before page load (GreptimeTeam#615)
Browse files Browse the repository at this point in the history
Co-authored-by: Ning Sun <sunng@protonmail.com>
  • Loading branch information
Aganivi and sunng87 committed Oct 11, 2023
1 parent 0aa46ea commit 87a2749
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
23 changes: 20 additions & 3 deletions docs/.vitepress/theme/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,38 @@ Layout
<script setup name="Layout" lang="ts">
import Layout from 'vitepress/dist/client/theme-default/Layout.vue'
import { useRouter } from 'vitepress'
import { getSidebarIcon } from '@/utils.ts'
import { getSidebarIcon, setVersionOnPage } from '@/utils.ts'
// data
const { theme } = useData()
const { latestVersion, iconMap } = theme.value
// methods
// lifecycle
onMounted(async () => {
const router = useRouter()
const currentVersion = ref(latestVersion)
router.onBeforePageLoad = to => {
const pre = to
let res = to.replace(`/zh`, '')
res = res.replace(`/en`, '')
res = res.replace(`/v0.4`, '')
res = res.replace(`/v0.4`, '')
if (pre !== res) router.go(res)
setVersionOnPage(to, currentVersion)
}
onBeforeMount(async () => {
const body = document.querySelector('body')
body.style.display = 'none'
const router = useRouter()
let path = router.route.path
if (path.includes(latestVersion)) {
const res = path.replace(`/${latestVersion}`, '')
await router.go(res)
}
body.style.display = 'block'
})
onMounted(async () => {
setVersionOnPage(router.route.path, currentVersion)
getSidebarIcon(iconMap)
})
onUnmounted(() => {})
</script>
7 changes: 7 additions & 0 deletions docs/.vitepress/theme/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,10 @@ export function getSidebarIcon(iconMap) {
}
})
}

export function setVersionOnPage(path, currentVersion) {
currentVersion.value = path.includes('/v0.3/') ? 'V0.3' : 'V0.4'
const div = document.querySelector('.VPNavBarMenuGroup')
const targetElement = <HTMLInputElement>div.childNodes[0].childNodes[0].childNodes[1]
targetElement.innerText = currentVersion.value
}
2 changes: 1 addition & 1 deletion docs/en/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const enConfig = async () => ({
},
{
// TODO change version
text: 'The Version Of History',
text: 'All Versions',
items: [
{
text: `${LATEST_VERSION}(latest)`,
Expand Down

0 comments on commit 87a2749

Please sign in to comment.