Skip to content

Commit

Permalink
feat(site): switch theme styles through routing
Browse files Browse the repository at this point in the history
  • Loading branch information
shenjunjian committed Nov 28, 2024
1 parent d3fcc60 commit 27b04d3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 25 deletions.
33 changes: 9 additions & 24 deletions examples/sites/src/tools/useTheme.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { watch, computed } from 'vue'
import TinyThemeTool from '@opentiny/vue-theme/theme-tool'
import { hooks } from '@opentiny/vue-common'
import designSaasConfig from '@opentiny/vue-design-saas'
import { router } from '@/router'
import { appData } from './appData'
import {
THEME_ROUTE_MAP,
CURRENT_THEME_KEY,
DEFAULT_THEME,
AURORA_THEME,
SMB_THEME,
INFINITY_THEME,
getKeyByValue
} from '../const'
import { THEME_ROUTE_MAP, CURRENT_THEME_KEY, DEFAULT_THEME, AURORA_THEME, SMB_THEME, INFINITY_THEME } from '../const'
import glaciers from '@/assets/images/glaciers.png'
import glaciersIcon from '@/assets/images/glaciers-icon.png'

Expand All @@ -24,13 +15,7 @@ import oceanicIcon from '@/assets/images/oceanic-icon.png'

import starrySky from '@/assets/images/starry-sky.png'
import starrySkyIcon from '@/assets/images/starry-sky-icon.png'

const themeMap = {
[DEFAULT_THEME]: null,
[AURORA_THEME]: null,
[SMB_THEME]: null,
[INFINITY_THEME]: null
}
import TinyThemeTool, { tinyOldTheme } from '@opentiny/vue-theme/theme-tool'

const isEn = appData.lang === 'enUS'

Expand Down Expand Up @@ -72,17 +57,13 @@ const designConfigMap = {
[SMB_THEME]: {}
}

const theme = new TinyThemeTool()

const defaultThemeKey = DEFAULT_THEME
const currentThemeKey = hooks.ref(defaultThemeKey)

watch(
() => currentThemeKey.value,
(newVal) => {
localStorage.setItem(CURRENT_THEME_KEY, newVal)
// 先屏蔽,等themeTool重构完成
// theme.changeTheme(themeMap[newVal])
}
)

Expand All @@ -106,6 +87,7 @@ const changeTheme = (themeKey) => {
const getThemeData = () => JSON.parse(JSON.stringify(themeData))

let initWatchRoute = false
let loadedTheme = false
const watchRoute = () => {
if (initWatchRoute) {
return
Expand All @@ -114,14 +96,17 @@ const watchRoute = () => {
watch(
() => router.currentRoute.value.params.theme,
(val) => {
const themeKey = getKeyByValue(THEME_ROUTE_MAP, val)
currentThemeKey.value = themeKey || defaultThemeKey
if (!loadedTheme && val === 'old-theme') {
const themeTool = new TinyThemeTool()
themeTool.changeTheme(tinyOldTheme)
loadedTheme = true
}
}
)
}

export default function useTheme() {
watchRoute()
!initWatchRoute && watchRoute()
return {
getThemeData,
changeTheme,
Expand Down
Loading

0 comments on commit 27b04d3

Please sign in to comment.