@@ -12,7 +12,7 @@ export const { THEMES = [] } = getConfig().publicRuntimeConfig
12
12
* @param {* } themeQuery
13
13
* @returns
14
14
*/
15
- export const getGlobalLayoutByTheme = ( themeQuery ) => {
15
+ export const getGlobalLayoutByTheme = themeQuery => {
16
16
if ( themeQuery !== BLOG . THEME ) {
17
17
return dynamic ( ( ) => import ( `@/themes/${ themeQuery } ` ) . then ( m => m [ getLayoutNameByPath ( - 1 ) ] ) , { ssr : true } )
18
18
} else {
@@ -29,22 +29,26 @@ export const getGlobalLayoutByTheme = (themeQuery) => {
29
29
export const getLayoutByTheme = ( { router, theme } ) => {
30
30
const themeQuery = getQueryParam ( router . asPath , 'theme' ) || theme
31
31
if ( themeQuery !== BLOG . THEME ) {
32
- return dynamic ( ( ) => import ( `@/themes/${ themeQuery } ` ) . then ( m => {
33
- setTimeout ( ( ) => {
34
- checkThemeDOM ( )
35
- } , 500 ) ;
32
+ return dynamic (
33
+ ( ) =>
34
+ import ( `@/themes/${ themeQuery } ` ) . then ( m => {
35
+ setTimeout ( ( ) => {
36
+ checkThemeDOM ( )
37
+ } , 500 )
36
38
37
- const components = m [ getLayoutNameByPath ( router . pathname , router . asPath ) ]
38
- if ( components ) {
39
- return components
40
- } else {
41
- return m . LayoutSlug
42
- }
43
- } ) , { ssr : true } )
39
+ const components = m [ getLayoutNameByPath ( router . pathname , router . asPath ) ]
40
+ if ( components ) {
41
+ return components
42
+ } else {
43
+ return m . LayoutSlug
44
+ }
45
+ } ) ,
46
+ { ssr : true }
47
+ )
44
48
} else {
45
49
setTimeout ( ( ) => {
46
50
checkThemeDOM ( )
47
- } , 100 ) ;
51
+ } , 100 )
48
52
const components = ThemeComponents [ getLayoutNameByPath ( router . pathname , router . asPath ) ]
49
53
if ( components ) {
50
54
return components
@@ -59,12 +63,12 @@ export const getLayoutByTheme = ({ router, theme }) => {
59
63
* @param {* } path
60
64
* @returns
61
65
*/
62
- const getLayoutNameByPath = ( path ) => {
66
+ const getLayoutNameByPath = path => {
63
67
if ( LAYOUT_MAPPINGS [ path ] ) {
64
- return LAYOUT_MAPPINGS [ path ] ;
68
+ return LAYOUT_MAPPINGS [ path ]
65
69
} else {
66
70
// 没有特殊处理的路径返回默认layout名称
67
- return 'LayoutSlug' ;
71
+ return 'LayoutSlug'
68
72
}
69
73
}
70
74
@@ -90,20 +94,20 @@ const checkThemeDOM = () => {
90
94
* @param updateDarkMode 更改主题ChangeState函数
91
95
* @description 读取cookie中存的用户主题
92
96
*/
93
- export const initDarkMode = ( updateDarkMode , defaultDarkMode ) => {
97
+ export const initDarkMode = ( updateDarkMode , defaultDarkMode ) => {
94
98
// 查看用户设备浏览器是否深色模型
95
99
let newDarkMode = isPreferDark ( )
96
100
97
101
// 查看localStorage中用户记录的是否深色模式
98
102
const userDarkMode = loadDarkModeFromLocalStorage ( )
99
103
if ( userDarkMode ) {
100
- newDarkMode = userDarkMode
104
+ newDarkMode = JSON . parse ( userDarkMode )
101
105
}
102
-
106
+
103
107
// 如果站点强制设置默认深色,则优先级改过用
104
- //if( defaultDarkMode) {
105
- // newDarkMode = defaultDarkMode
106
- // }
108
+ if ( typeof defaultDarkMode === 'boolean' && defaultDarkMode ) {
109
+ newDarkMode = defaultDarkMode
110
+ }
107
111
108
112
// url查询条件中是否深色模式
109
113
const queryMode = getQueryVariable ( 'mode' )
@@ -128,7 +132,11 @@ export function isPreferDark() {
128
132
// 系统深色模式或时间是夜间时,强行置为夜间模式
129
133
const date = new Date ( )
130
134
const prefersDarkMode = window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches
131
- return prefersDarkMode || ( BLOG . APPEARANCE_DARK_TIME && ( date . getHours ( ) >= BLOG . APPEARANCE_DARK_TIME [ 0 ] || date . getHours ( ) < BLOG . APPEARANCE_DARK_TIME [ 1 ] ) )
135
+ return (
136
+ prefersDarkMode ||
137
+ ( BLOG . APPEARANCE_DARK_TIME &&
138
+ ( date . getHours ( ) >= BLOG . APPEARANCE_DARK_TIME [ 0 ] || date . getHours ( ) < BLOG . APPEARANCE_DARK_TIME [ 1 ] ) )
139
+ )
132
140
}
133
141
return false
134
142
}
@@ -142,9 +150,9 @@ export const loadDarkModeFromLocalStorage = () => {
142
150
}
143
151
144
152
/**
145
- * 保存深色模式
146
- * @param newTheme
147
- */
148
- export const saveDarkModeToLocalStorage = ( newTheme ) => {
153
+ * 保存深色模式
154
+ * @param newTheme
155
+ */
156
+ export const saveDarkModeToLocalStorage = newTheme => {
149
157
localStorage . setItem ( 'darkMode' , newTheme )
150
158
}
0 commit comments