forked from notionnext-org/NotionNext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme.js
More file actions
348 lines (316 loc) · 10.4 KB
/
Copy paththeme.js
File metadata and controls
348 lines (316 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import BLOG, { LAYOUT_MAPPINGS } from '@/blog.config'
import getConfig from 'next/config'
import dynamic from 'next/dynamic'
import { useRouter } from 'next/router'
import { getQueryParam, getQueryVariable, isBrowser } from '../lib/utils'
// 在next.config.js中扫描所有主题
export const { THEMES = [] } = getConfig()?.publicRuntimeConfig || {}
const baseLayoutCache = new Map()
const layoutByThemeCache = new Map()
let domFixTimer = null
const LayoutLoading = () => (
<div className='min-h-screen w-full bg-[#f6f6f1] dark:bg-black' />
)
const EmptyBaseLayout = ({ children }) => <>{children}</>
const EmptyPageLayout = () => null
const IndexLayoutLoading = () => (
<div className='pt-10 md:pt-18 w-full bg-[#f6f6f1] dark:bg-black'>
<div className='mx-auto w-full max-w-screen-3xl px-4 py-10 lg:px-0'>
<div className='grid gap-10 xl:grid-cols-2'>
<section className='space-y-5'>
<div className='h-80 w-full animate-pulse bg-gray-200 dark:bg-gray-800' />
<div className='h-10 w-4/5 animate-pulse bg-gray-200 dark:bg-gray-800' />
<div className='h-4 w-2/3 animate-pulse bg-gray-200 dark:bg-gray-800' />
<div className='h-4 w-24 animate-pulse bg-gray-200 dark:bg-gray-800' />
</section>
<section className='space-y-6'>
<div className='h-48 w-full animate-pulse bg-gray-200 dark:bg-gray-800' />
{[0, 1].map(item => (
<div
key={item}
className='flex gap-6 border-t border-gray-300 pt-6 dark:border-gray-800'>
<div className='min-w-0 flex-1 space-y-3'>
<div className='h-6 w-4/5 animate-pulse bg-gray-200 dark:bg-gray-800' />
<div className='h-4 w-2/3 animate-pulse bg-gray-200 dark:bg-gray-800' />
<div className='h-4 w-20 animate-pulse bg-gray-200 dark:bg-gray-800' />
</div>
<div className='h-32 w-32 shrink-0 animate-pulse bg-gray-200 dark:bg-gray-800' />
</div>
))}
</section>
</div>
<section className='mt-12'>
<div className='flex items-center justify-between'>
<div className='h-7 w-28 animate-pulse bg-gray-200 dark:bg-gray-800' />
<div className='h-5 w-24 animate-pulse bg-gray-200 dark:bg-gray-800' />
</div>
<div className='mt-6 grid gap-8 md:grid-cols-2 xl:grid-cols-4'>
{[0, 1, 2, 3].map(item => (
<div
key={item}
className='space-y-4 border-t border-gray-300 pt-5 dark:border-gray-800'>
<div className='h-5 w-3/4 animate-pulse bg-gray-200 dark:bg-gray-800' />
<div className='h-4 w-24 animate-pulse bg-gray-200 dark:bg-gray-800' />
</div>
))}
</div>
</section>
</div>
</div>
)
const getLayoutLoading = layoutName => {
if (layoutName === 'LayoutIndex') {
return IndexLayoutLoading
}
return LayoutLoading
}
const normalizeThemeName = themeValue => {
if (!themeValue || typeof themeValue !== 'string') return BLOG.THEME
const firstTheme = themeValue.split(',')[0].trim()
if (!firstTheme) return BLOG.THEME
return THEMES.includes(firstTheme) ? firstTheme : BLOG.THEME
}
const getFallbackThemeName = themeName => {
const preferred = normalizeThemeName(BLOG.THEME)
if (preferred && preferred !== themeName) return preferred
if (THEMES.includes('example') && themeName !== 'example') return 'example'
return THEMES.find(item => item !== themeName) || null
}
const getThemeExport = (mod, exportName) => {
if (mod?.[exportName]) return mod[exportName]
if (mod?.default?.[exportName]) return mod.default[exportName]
if (exportName === 'LayoutBase' && typeof mod?.default === 'function') {
return mod.default
}
return null
}
const scheduleFixThemeDOM = (delay = 120) => {
if (!isBrowser) return
if (domFixTimer) {
clearTimeout(domFixTimer)
}
domFixTimer = setTimeout(() => {
fixThemeDOM()
domFixTimer = null
}, delay)
}
async function importThemeConfig(themeFolderName) {
try {
const mod = await import(`@/themes/${themeFolderName}`)
return getThemeExport(mod, 'THEME_CONFIG')
} catch (err) {
console.error(`Failed to load theme config "${themeFolderName}":`, err)
return null
}
}
async function importThemeLayout(themeFolderName, layoutName) {
try {
const mod = await import(`@/themes/${themeFolderName}`)
return (
getThemeExport(mod, layoutName) ||
getThemeExport(mod, 'LayoutSlug') ||
null
)
} catch (err) {
console.error(`Failed to load theme "${themeFolderName}":`, err)
return null
}
}
async function resolveThemeLayout(themeName, layoutName, emptyLayout) {
let Layout = await importThemeLayout(themeName, layoutName)
if (Layout) return Layout
const fallback = getFallbackThemeName(themeName)
if (fallback) {
Layout = await importThemeLayout(fallback, layoutName)
if (Layout) {
console.warn(
`[theme] "${themeName}" missing "${layoutName}", using fallback "${fallback}".`
)
return Layout
}
}
console.warn(`[theme] "${themeName}" missing "${layoutName}", using empty layout.`)
return emptyLayout
}
/**
* 获取主题配置(始终动态加载,与运行时 BLOG.THEME / URL ?theme 一致;不依赖编译期别名)。
* @param {string} themeQuery - 主题查询参数(支持多个主题用逗号分隔)
* @returns {Promise<object|null>} 主题配置对象
*/
export const getThemeConfig = async themeQuery => {
const themeName = normalizeThemeName(themeQuery)
let cfg = await importThemeConfig(themeName)
if (cfg) {
return cfg
}
const fallback = normalizeThemeName(BLOG.THEME)
if (fallback !== themeName) {
cfg = await importThemeConfig(fallback)
if (cfg) {
console.warn(
`[theme] "${themeName}" config unavailable, using fallback "${fallback}".`
)
return cfg
}
}
console.warn('[theme] No theme configuration could be loaded, using empty config.')
return {}
}
/**
* 获取当前主题(query 主题优先,且做合法性校验)
*/
const getCurrentTheme = (router, fallbackTheme) => {
const queryTheme = getQueryParam(router?.asPath, 'theme')
if (queryTheme) {
return normalizeThemeName(queryTheme)
}
return normalizeThemeName(fallbackTheme || BLOG.THEME)
}
/**
* 加载全局布局
* @param {*} theme
* @returns
*/
export const getBaseLayoutByTheme = theme => {
const normalizedTheme = normalizeThemeName(theme)
if (baseLayoutCache.has(normalizedTheme)) {
return baseLayoutCache.get(normalizedTheme)
}
const DynamicBaseLayout = dynamic(
() =>
resolveThemeLayout(normalizedTheme, 'LayoutBase', EmptyBaseLayout),
{ ssr: true }
)
baseLayoutCache.set(normalizedTheme, DynamicBaseLayout)
return DynamicBaseLayout
}
/**
* 动态获取布局
* @param {*} props
*/
export const DynamicLayout = props => {
const { theme, layoutName } = props
const SelectedLayout = useLayoutByTheme({ layoutName, theme })
return <SelectedLayout {...props} />
}
/**
* 加载主题文件
* @param {*} layoutName
* @param {*} theme
* @returns
*/
export const useLayoutByTheme = ({ layoutName, theme }) => {
const router = useRouter()
const themeQuery = getCurrentTheme(router, theme)
const cacheKey = `${themeQuery}:${layoutName}`
if (layoutByThemeCache.has(cacheKey)) {
scheduleFixThemeDOM(themeQuery === BLOG.THEME ? 80 : 240)
return layoutByThemeCache.get(cacheKey)
}
const loadLayout = () =>
resolveThemeLayout(themeQuery, layoutName, EmptyPageLayout)
const DynamicLayoutComponent = dynamic(loadLayout, {
ssr: true,
loading: getLayoutLoading(layoutName)
})
layoutByThemeCache.set(cacheKey, DynamicLayoutComponent)
scheduleFixThemeDOM(themeQuery === BLOG.THEME ? 80 : 240)
return DynamicLayoutComponent
}
/**
* 根据路径 获取对应的layout名称
* @param {*} path
* @returns
*/
const getLayoutNameByPath = path => {
const layoutName = LAYOUT_MAPPINGS[path] || 'LayoutSlug'
// console.log('path-layout',path,layoutName)
return layoutName
}
/**
* 切换主题时的特殊处理
* 删除多余的元素
*/
const fixThemeDOM = () => {
if (isBrowser) {
const elements = document.querySelectorAll('[id^="theme-"]')
if (elements?.length > 1) {
for (let i = 0; i < elements.length - 1; i++) {
if (
elements[i] &&
elements[i].parentNode &&
elements[i].parentNode.contains(elements[i])
) {
elements[i].parentNode.removeChild(elements[i])
}
}
elements[0]?.scrollIntoView()
}
}
}
/**
* 初始化主题 , 优先级 query > cookies > systemPrefer
* @param isDarkMode
* @param updateDarkMode 更改主题ChangeState函数
* @description 读取cookie中存的用户主题
*/
export const initDarkMode = (updateDarkMode, defaultDarkMode) => {
// 查看用户设备浏览器是否深色模型
let newDarkMode = isPreferDark()
// 查看localStorage中用户记录的是否深色模式
const userDarkMode = loadDarkModeFromLocalStorage()
if (userDarkMode) {
newDarkMode = userDarkMode === 'dark' || userDarkMode === 'true'
saveDarkModeToLocalStorage(newDarkMode) // 用户手动的才保存
}
// 如果站点强制设置默认深色,则优先级改过用
if (defaultDarkMode === 'true') {
newDarkMode = true
}
// url查询条件中是否深色模式
const queryMode = getQueryVariable('mode')
if (queryMode) {
newDarkMode = queryMode === 'dark'
}
updateDarkMode(newDarkMode)
document
.getElementsByTagName('html')[0]
.setAttribute('class', newDarkMode ? 'dark' : 'light')
}
/**
* 是否优先深色模式, 根据系统深色模式以及当前时间判断
* @returns {*}
*/
export function isPreferDark() {
if (BLOG.APPEARANCE === 'dark') {
return true
}
if (BLOG.APPEARANCE === 'auto') {
// 系统深色模式或时间是夜间时,强行置为夜间模式
const date = new Date()
const prefersDarkMode = window.matchMedia(
'(prefers-color-scheme: dark)'
).matches
return (
prefersDarkMode ||
(BLOG.APPEARANCE_DARK_TIME &&
(date.getHours() >= BLOG.APPEARANCE_DARK_TIME[0] ||
date.getHours() < BLOG.APPEARANCE_DARK_TIME[1]))
)
}
return false
}
/**
* 读取深色模式
* @returns {*}
*/
export const loadDarkModeFromLocalStorage = () => {
return localStorage.getItem('darkMode')
}
/**
* 保存深色模式
* @param newTheme
*/
export const saveDarkModeToLocalStorage = newTheme => {
localStorage.setItem('darkMode', newTheme)
}