global关于主题配置的读取

This commit is contained in:
tangly1024.com
2024-06-27 16:19:50 +08:00
parent d41e1b51b5
commit 10138f8796
3 changed files with 29 additions and 1 deletions

View File

@@ -55,8 +55,8 @@ export const siteConfig = (key, defaultVal = null, extendConfig = {}) => {
let siteInfo = null
if (global) {
val = global.NOTION_CONFIG?.[key]
siteInfo = global.siteInfo
val = global.NOTION_CONFIG?.[key] || global.THEME_CONFIG?.[key]
}
if (!val) {

View File

@@ -1,5 +1,6 @@
import {
THEMES,
getThemeConfig,
initDarkMode,
saveDarkModeToLocalStorage
} from '@/themes/theme'
@@ -29,11 +30,14 @@ export function GlobalContextProvider(props) {
tagOptions,
NOTION_CONFIG
} = props
const [lang, updateLang] = useState(NOTION_CONFIG?.LANG || LANG) // 默认语言
const [locale, updateLocale] = useState(
generateLocaleDict(NOTION_CONFIG?.LANG || LANG)
) // 默认语言
const [theme, setTheme] = useState(NOTION_CONFIG?.THEME || THEME) // 默认博客主题
const [THEME_CONFIG, SET_THEME_CONFIG] = useState(null) //主题配置
const defaultDarkMode = NOTION_CONFIG?.APPEARANCE || APPEARANCE
const [isDarkMode, updateDarkMode] = useState(defaultDarkMode === 'dark') // 默认深色模式
const [onLoading, setOnLoading] = useState(false) // 抓取文章数据
@@ -54,6 +58,12 @@ export function GlobalContextProvider(props) {
return newTheme
}
// 抓取主题配置
const updateThemeConfig = async theme => {
const config = await getThemeConfig(theme)
SET_THEME_CONFIG(config)
}
// 切换深色模式
const toggleDarkMode = () => {
const newStatus = !isDarkMode
@@ -99,6 +109,9 @@ export function GlobalContextProvider(props) {
setOnLoading(false)
}
const currentTheme = router?.query?.theme || theme
updateThemeConfig(currentTheme)
router.events.on('routeChangeStart', handleStart)
router.events.on('routeChangeError', handleStop)
router.events.on('routeChangeComplete', handleStop)
@@ -114,6 +127,7 @@ export function GlobalContextProvider(props) {
value={{
fullWidth,
NOTION_CONFIG,
THEME_CONFIG,
toggleDarkMode,
onLoading,
setOnLoading,

View File

@@ -7,6 +7,20 @@ import { getQueryParam, getQueryVariable, isBrowser } from '../lib/utils'
// 在next.config.js中扫描所有主题
export const { THEMES = [] } = getConfig().publicRuntimeConfig
/**
* 获取主体配置
*/
export const getThemeConfig = async themeQuery => {
if (themeQuery && themeQuery !== BLOG.THEME) {
const THEME_CONFIG = await import(`@/themes/${themeQuery}`).then(
m => m.THEME_CONFIG
)
return THEME_CONFIG
} else {
return ThemeComponents?.THEME_CONFIG
}
}
/**
* 加载全局布局
* @param {*} themeQuery