Skip to content

Commit 079e029

Browse files
author
cole
committed
feat: theme cache
1 parent fd6191f commit 079e029

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/hooks/useTheme.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import { computed, ref, unref } from 'vue'
22
import { theme as antTheme } from 'ant-design-vue'
3+
import { localCache, THEME__LOCAL } from '@/utils/storage'
4+
5+
const defaultValues = {
6+
theme: 'dark',
7+
primary: 'blue',
8+
compact: false
9+
}
310

411
function useTheme () {
12+
const themeCache = localCache.getObj(THEME__LOCAL)
513
const { darkAlgorithm, compactAlgorithm } = antTheme
614
const { token } = antTheme.useToken()
715

8-
// @todo 添加缓存
9-
const themeConfig = ref({
10-
theme: 'dark',
11-
primary: 'blue',
12-
compact: false
13-
})
16+
const themeConfig = ref(themeCache || defaultValues)
1417

1518
const themeProvider = computed(() => {
1619
const { theme, primary, compact } = unref(themeConfig)
@@ -26,6 +29,7 @@ function useTheme () {
2629

2730
function setThemeConfig (value) {
2831
themeConfig.value = value
32+
localCache.setObj(THEME__LOCAL, value)
2933
}
3034

3135
return { themeConfig, themeProvider, setThemeConfig }

src/utils/storage.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export const sessionCache = new GalleryCache(sessionStorage)
5959

6060
export const TAGS__LOCAL = 'tags'
6161
export const LOCALE__LOCAL = 'locale'
62+
export const THEME__LOCAL = 'theme'
6263

6364
// 用户账号
6465
export const USERNAME__LOCAL = 'username'

0 commit comments

Comments
 (0)