site: refactor store, lazy init refs

This commit is contained in:
07akioni 2021-04-06 00:40:02 +08:00
parent eae73b9968
commit 8f824033ed
2 changed files with 85 additions and 85 deletions

View File

@ -24,7 +24,8 @@
</template> </template>
<script> <script>
import { siteSetup } from './store' import { useRouter, useRoute } from 'vue-router'
import { initRouter, siteSetup } from './store'
import Site from './Site.vue' import Site from './Site.vue'
export default { export default {
@ -33,6 +34,7 @@ export default {
Site Site
}, },
setup () { setup () {
initRouter(useRouter(), useRoute())
return siteSetup() return siteSetup()
} }
} }

View File

@ -1,5 +1,4 @@
import { computed, ref, provide, reactive, toRef, inject } from 'vue' import { computed, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useMemo } from 'vooks' import { useMemo } from 'vooks'
import { import {
NConfigProvider, NConfigProvider,
@ -18,24 +17,13 @@ import {
} from './menu-options' } from './menu-options'
import hljs from './hljs' import hljs from './hljs'
const storeKey = 'site-store' let route = null
let router = null
export function siteSetup () { export function initRouter (_router, _route) {
const route = useRoute() route = _route
const router = useRouter() router = _router
// display mode localeNameRef = useMemo({
const _displayModeRef = ref(window.localStorage.getItem('mode') ?? 'debug')
const displayModeRef = computed({
get () {
return _displayModeRef.value
},
set (value) {
_displayModeRef.value = value
window.localStorage.setItem('mode', value)
}
})
// locale
const localeNameRef = useMemo({
get () { get () {
return route.path.startsWith('/zh-CN') ? 'zh-CN' : 'en-US' return route.path.startsWith('/zh-CN') ? 'zh-CN' : 'en-US'
}, },
@ -43,16 +31,10 @@ export function siteSetup () {
router.push(changeLangInPath(route.fullPath, locale)) router.push(changeLangInPath(route.fullPath, locale))
} }
}) })
const localeRef = computed(() => { dateLocaleRef = useMemo(() => {
return localeNameRef.value === 'zh-CN' ? zhCN : enUS
})
// useMemo
const dateLocaleRef = useMemo(() => {
return route.params.lang === 'zh-CN' ? dateZhCN : dateEnUS return route.params.lang === 'zh-CN' ? dateZhCN : dateEnUS
}) })
// theme themeNameRef = useMemo({
const osThemeRef = useOsTheme()
const themeNameRef = useMemo({
get () { get () {
switch (route.params.theme) { switch (route.params.theme) {
case 'os-theme': case 'os-theme':
@ -67,19 +49,45 @@ export function siteSetup () {
router.push(changeThemeInPath(route.fullPath, theme)) router.push(changeThemeInPath(route.fullPath, theme))
} }
}) })
}
// display mode
const _displayModeRef = ref(window.localStorage.getItem('mode') ?? 'debug')
const displayModeRef = computed({
get () {
return _displayModeRef.value
},
set (value) {
_displayModeRef.value = value
window.localStorage.setItem('mode', value)
}
})
// locale
let localeNameRef = null
const localeRef = computed(() => {
return localeNameRef.value === 'zh-CN' ? zhCN : enUS
})
// useMemo
let dateLocaleRef = null
// theme
const osThemeRef = useOsTheme()
let themeNameRef = null
const themeRef = computed(() => { const themeRef = computed(() => {
const { value } = themeNameRef const { value } = themeNameRef
return value === 'dark' ? darkTheme : null return value === 'dark' ? darkTheme : null
}) })
// config provider // config provider
const configProviderNameRef = ref( const configProviderNameRef = ref(process.env.TUSIMPLE ? 'tusimple' : 'default')
process.env.TUSIMPLE ? 'tusimple' : 'default'
)
const configProviderRef = computed(() => { const configProviderRef = computed(() => {
return configProviderNameRef.value === 'tusimple' return configProviderNameRef.value === 'tusimple'
? TsConfigProvider ? TsConfigProvider
: NConfigProvider : NConfigProvider
}) })
// options // options
const docOptionsRef = computed(() => const docOptionsRef = computed(() =>
createDocumentationMenuOptions({ createDocumentationMenuOptions({
@ -108,18 +116,8 @@ export function siteSetup () {
traverse(componentOptionsRef.value) traverse(componentOptionsRef.value)
return flattenedItems return flattenedItems
}) })
provide(
storeKey, export function siteSetup () {
reactive({
themeName: themeNameRef,
configProviderName: configProviderNameRef,
localeName: localeNameRef,
displayMode: displayModeRef,
docOptions: docOptionsRef,
componentOptions: componentOptionsRef,
flattenedDocOptions: flattenedDocOptionsRef
})
)
i18n.provide(computed(() => localeNameRef.value)) i18n.provide(computed(() => localeNameRef.value))
const isMobileRef = useIsMobile() const isMobileRef = useIsMobile()
return { return {
@ -146,29 +144,29 @@ function changeThemeInPath (path, theme) {
} }
export function useDisplayMode () { export function useDisplayMode () {
return toRef(inject(storeKey), 'displayMode') return displayModeRef
} }
export function useLocaleName () { export function useLocaleName () {
return toRef(inject(storeKey), 'localeName') return localeNameRef
} }
export function useThemeName () { export function useThemeName () {
return toRef(inject(storeKey), 'themeName') return themeNameRef
} }
export function useDocOptions () { export function useDocOptions () {
return toRef(inject(storeKey), 'docOptions') return docOptionsRef
} }
export function useComponentOptions () { export function useComponentOptions () {
return toRef(inject(storeKey), 'componentOptions') return componentOptionsRef
} }
export function useFlattenedDocOptions () { export function useFlattenedDocOptions () {
return toRef(inject(storeKey), 'flattenedDocOptions') return flattenedDocOptionsRef
} }
export function useConfigProviderName () { export function useConfigProviderName () {
return toRef(inject(storeKey), 'configProviderName') return configProviderNameRef
} }