Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(site): switch theme styles through routing #2574

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading