naive-ui/demo/SiteProvider.vue

96 lines
2.0 KiB
Vue
Raw Normal View History

2020-09-27 22:27:25 +08:00
<template>
<n-config-provider
class="demo"
namespace="naive-ui-doc"
:theme="theme"
:language="lang"
>
<n-loading-bar-provider ref="loadingBar">
<n-message-provider>
<n-notification-provider>
<n-dialog-provider>
<site />
</n-dialog-provider>
</n-notification-provider>
</n-message-provider>
</n-loading-bar-provider>
</n-config-provider>
</template>
<script>
2020-11-14 20:38:30 +08:00
import { ref, computed } from 'vue'
import Site from './Site.vue'
2020-11-14 20:38:30 +08:00
import { useRoute, useRouter } from 'vue-router'
2020-09-27 22:27:25 +08:00
export default {
name: 'SiteProvider',
2020-11-03 15:10:29 +08:00
components: {
Site
},
2020-09-27 22:27:25 +08:00
provide () {
return {
SiteProvider: this
}
},
2020-11-03 15:10:29 +08:00
beforeRouteEnter (to, from, next) {
next()
},
beforeRouteUpdate (to, from, next) {
next()
2020-09-27 22:27:25 +08:00
},
2020-11-14 20:38:30 +08:00
setup () {
const displayModeRef = ref(
localStorage.getItem('mode') ?? 'debug'
)
const displayModeComputed = computed({
2020-09-27 22:27:25 +08:00
get () {
2020-11-14 20:38:30 +08:00
return displayModeRef.value
},
set (value) {
displayModeRef.value = value
localStorage.setItem('mode', value)
}
})
const route = useRoute()
const router = useRouter()
const langRef = computed({
get () {
return route.params.lang || 'en-US'
2020-09-27 22:27:25 +08:00
},
set (lang) {
2020-11-14 20:38:30 +08:00
router.push(changeLangInPath(route.fullPath, lang))
2020-09-27 22:27:25 +08:00
}
2020-11-14 20:38:30 +08:00
})
const themeRef = computed({
2020-09-27 22:27:25 +08:00
get () {
2020-11-14 20:38:30 +08:00
switch (route.params.theme) {
2020-09-27 22:27:25 +08:00
case 'os-theme': return 'light'
case 'dark': return 'dark'
default: return 'light'
}
},
set (theme) {
2020-11-14 20:38:30 +08:00
router.push(
changeThemeInPath(route.fullPath, theme)
2020-09-27 22:27:25 +08:00
)
}
2020-11-14 20:38:30 +08:00
})
return {
displayMode: displayModeComputed,
theme: themeRef,
lang: langRef
2020-09-27 22:27:25 +08:00
}
}
}
function changeLangInPath (path, lang) {
const langReg = /^\/(zh-CN|en-US)\//
return path.replace(langReg, `/${lang}/`)
}
function changeThemeInPath (path, theme) {
const themeReg = /(^\/[^/]+\/)([^/]+)/
return path.replace(themeReg, '$1' + theme)
}
</script>