naive-ui/demo/SiteProvider.vue

99 lines
2.2 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"
2020-12-31 20:29:27 +08:00
:unstable-theme="theme === 'dark' ? darkTheme : undefined"
2020-09-27 22:27:25 +08:00
: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-12-31 20:29:27 +08:00
import { darkTheme } from '../src/styles.new'
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 () {
2020-12-12 13:51:22 +08:00
const displayModeRef = ref(localStorage.getItem('mode') ?? 'debug')
2020-11-14 20:38:30 +08:00
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-12-12 13:51:22 +08:00
case 'os-theme':
return 'light'
case 'dark':
return 'dark'
default:
return 'light'
2020-09-27 22:27:25 +08:00
}
},
set (theme) {
2020-12-12 13:51:22 +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,
2020-12-31 20:29:27 +08:00
lang: langRef,
// unstable
darkTheme
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>