动态配置

This commit is contained in:
tangly1024
2023-11-01 23:13:25 +08:00
parent ef6e9e0d95
commit 3e58c6b637
8 changed files with 43 additions and 45 deletions

View File

@@ -25,7 +25,7 @@ export const siteConfig = (key) => {
if (global) {
val = global.NOTION_CONFIG?.[key]
siteInfo = global.siteInfo
console.log('当前变量', key, val)
// console.log('当前变量', key, val)
}
if (!val) {
@@ -38,7 +38,7 @@ export const siteConfig = (key) => {
val = siteInfo?.icon // 封面图取Notion的封面
break
case 'TITLE':
val = siteConfig('TITLE') // 标题取Notion中的标题
val = siteInfo?.title // 标题取Notion中的标题
break
}
}
@@ -47,6 +47,6 @@ export const siteConfig = (key) => {
if (!val) {
val = BLOG[key]
}
console.log('配置', key, val)
console.log('实际配置', key, val)
return val
}

View File

@@ -2,33 +2,53 @@ import { generateLocaleDict, initLocale } from './lang'
import { createContext, useContext, useEffect, useState } from 'react'
import { useRouter } from 'next/router'
import { THEMES, initDarkMode } from '@/themes/theme'
import NProgress from 'nprogress'
import { getQueryVariable, isBrowser } from './utils'
import BLOG from '@/blog.config'
import NProgress from 'nprogress'
import { isBrowser } from './utils'
const GlobalContext = createContext()
/**
* 全局变量Provider包括语言本地化、样式主题、搜索词
* 定义全局变量,包括语言、主题、深色模式、加载状态
* @param children
* @returns {JSX.Element}
* @constructor
*/
export function GlobalContextProvider(props) {
const { children, siteInfo, categoryOptions, tagOptions, NOTION_CONFIG } = props
console.log('config', NOTION_CONFIG)
const router = useRouter()
const [lang, updateLang] = useState(BLOG.LANG) // 默认语言
const [locale, updateLocale] = useState(generateLocaleDict(BLOG.LANG)) // 默认语言
const [theme, setTheme] = useState(BLOG.THEME) // 默认博客主题
const [isDarkMode, updateDarkMode] = useState(BLOG.APPEARANCE === 'dark') // 默认深色模式
const [lang, updateLang] = useState(NOTION_CONFIG.LANG || BLOG.LANG) // 默认语言
const [locale, updateLocale] = useState(generateLocaleDict(NOTION_CONFIG.LANG || BLOG.LANG)) // 默认语言
const [theme, setTheme] = useState(NOTION_CONFIG.THEME || BLOG.THEME) // 默认博客主题
const [isDarkMode, updateDarkMode] = useState(NOTION_CONFIG.APPEARANCE || BLOG.APPEARANCE === 'dark') // 默认深色模式
const [onLoading, setOnLoading] = useState(false) // 抓取文章数据
// 切换主题
function switchTheme() {
const currentIndex = THEMES.indexOf(theme)
const newIndex = currentIndex < THEMES.length - 1 ? currentIndex + 1 : 0
const newTheme = THEMES[newIndex]
const query = router.query
query.theme = newTheme
router.push({ pathname: router.pathname, query })
return newTheme
}
useEffect(() => {
initLocale(lang, locale, updateLang, updateLocale)
initDarkMode(updateDarkMode)
initTheme()
checkThemeDOM()
}, [])
// 加载默认主题
// useEffect(() => {
// const queryTheme = getQueryVariable('theme') || theme
// setTheme(queryTheme)
// }, [router])
// 加载进度条
useEffect(() => {
const handleStart = (url) => {
NProgress.start()
@@ -43,8 +63,6 @@ export function GlobalContextProvider(props) {
NProgress.done()
setOnLoading(false)
}
const queryTheme = getQueryVariable('theme') || BLOG.THEME
setTheme(queryTheme)
router.events.on('routeChangeStart', handleStart)
router.events.on('routeChangeError', handleStop)
router.events.on('routeChangeComplete', handleStop)
@@ -55,22 +73,13 @@ export function GlobalContextProvider(props) {
}
}, [router])
// 切换主题
function switchTheme() {
const currentIndex = THEMES.indexOf(theme)
const newIndex = currentIndex < THEMES.length - 1 ? currentIndex + 1 : 0
const newTheme = THEMES[newIndex]
const query = router.query
query.theme = newTheme
router.push({ pathname: router.pathname, query })
return newTheme
}
return (
<GlobalContext.Provider value={{
NOTION_CONFIG,
onLoading,
setOnLoading,
lang,
updateLang,
locale,
updateLocale,
isDarkMode,
@@ -91,7 +100,7 @@ export function GlobalContextProvider(props) {
* 切换主题时的特殊处理
* @param {*} setTheme
*/
const initTheme = () => {
const checkThemeDOM = () => {
if (isBrowser) {
setTimeout(() => {
const elements = document.querySelectorAll('[id^="theme-"]')

View File

@@ -65,7 +65,7 @@ export function generateLocaleDict(langString) {
*/
export function initLocale(lang, locale, changeLang, changeLocale) {
if (isBrowser) {
const queryLang = getQueryVariable('lang') || loadLangFromCookies() || window.navigator.language
const queryLang = getQueryVariable('lang') || loadLangFromCookies()
let currentLang = lang
if (queryLang !== lang) {
currentLang = queryLang

View File

@@ -127,7 +127,7 @@ export async function getConfigMapFromConfigPage(allPages) {
// 只导入生效的配置
if (config.enable) {
// console.log('[覆盖代码配置]', config.key)
console.log('[Notion配置]', config.key, config.value)
notionConfig[config.key] = config.value
}
}