优化初始化主题

This commit is contained in:
tangly1024
2022-03-30 11:15:39 +08:00
parent 946e1cefd4
commit 753a44226b
4 changed files with 68 additions and 64 deletions

View File

@@ -45,10 +45,6 @@ export function GlobalContextProvider ({ children }) {
useEffect(() => {
if (!hasInit) {
const userTheme = getQueryVariable('theme')
if (userTheme && ALL_THEME.indexOf(userTheme) > -1) {
setTheme(userTheme)
}
initLocale(locale, updateLocale)
initDarkMode(isDarkMode, updateDarkMode)
initTheme(theme, changeTheme)
@@ -63,50 +59,4 @@ export function GlobalContextProvider ({ children }) {
)
}
/**
* 查询url中的query参数
* @param {}} variable
* @returns
*/
function getQueryVariable (variable) {
const query = window.location.search.substring(1)
const vars = query.split('&')
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split('=')
if (pair[0] === variable) { return pair[1] }
}
return (false)
}
/**
* 深度合并两个对象
* @param target
* @param sources
*/
export function mergeDeep (target, ...sources) {
if (!sources.length) return target
const source = sources.shift()
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} })
mergeDeep(target[key], source[key])
} else {
Object.assign(target, { [key]: source[key] })
}
}
}
return mergeDeep(target, ...sources)
}
/**
* 对象检查
* @param item
* @returns {boolean}
*/
export function isObject (item) {
return (item && typeof item === 'object' && !Array.isArray(item))
}
export const useGlobal = () => useContext(GlobalContext)

View File

@@ -2,7 +2,7 @@ import zhCN from './lang/zh-CN'
import enUS from './lang/en-US'
import zhHK from './lang/zh-HK'
import zhTW from './lang/zh-TW'
import { mergeDeep } from '@/lib/global'
import { mergeDeep } from './utils'
const lang = {
'en-US': enUS,
'zh-CN': zhCN,

View File

@@ -1,37 +1,45 @@
import cookie from 'react-cookies'
import BLOG from '@/blog.config'
import { ALL_THEME } from '@/themes'
import { getQueryVariable } from './utils'
/**
* 初始化主题
* 初始化主题 , 优先级 query > cookies > systemPrefer
* @param isDarkMode
* @param updateDarkMode 更改主题ChangeState函数
* @description 读取cookie中存的用户主题
*/
export const initDarkMode = (isDarkMode, updateDarkMode) => {
if (typeof window !== 'undefined') {
if (!isDarkMode) {
isDarkMode = isPreferDark()
}
updateDarkMode(isDarkMode)
saveDarkModeToCookies(isDarkMode)
document.getElementsByTagName('html')[0].setAttribute('class', isDarkMode ? 'dark' : 'light')
const queryMode = getQueryVariable('mode')
if (queryMode) {
isDarkMode = queryMode === 'dark'
} else if (!isDarkMode) {
isDarkMode = isPreferDark()
}
updateDarkMode(isDarkMode)
saveDarkModeToCookies(isDarkMode)
document.getElementsByTagName('html')[0].setAttribute('class', isDarkMode ? 'dark' : 'light')
}
/**
* 从cookie中读取 用户默认主题
* 初始化主题, 优先级 query > cookies > blog.config.js
* @param {*} theme
* @param {*} changeTheme
*/
export const initTheme = (theme, changeTheme) => {
const userTheme = loadThemeFromCookies()
if (userTheme !== theme) {
changeTheme(userTheme)
const queryTheme = getQueryVariable('theme')
if (queryTheme && ALL_THEME.indexOf(queryTheme) > -1) {
changeTheme(queryTheme)
} else {
const userTheme = loadThemeFromCookies()
if (userTheme !== theme) {
changeTheme(userTheme)
}
}
}
/**
* 是否优先深色模式
* 是否优先深色模式 根据系统深色模式以及当前时间判断
* @returns {*}
*/
export function isPreferDark () {

View File

@@ -25,3 +25,49 @@ export function loadExternalResource (url, type) {
}
})
}
/**
* 查询url中的query参数
* @param {}} variable
* @returns
*/
export function getQueryVariable (variable) {
const query = window.location.search.substring(1)
const vars = query.split('&')
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split('=')
if (pair[0] === variable) { return pair[1] }
}
return (false)
}
/**
* 深度合并两个对象
* @param target
* @param sources
*/
export function mergeDeep (target, ...sources) {
if (!sources.length) return target
const source = sources.shift()
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} })
mergeDeep(target[key], source[key])
} else {
Object.assign(target, { [key]: source[key] })
}
}
}
return mergeDeep(target, ...sources)
}
/**
* 对象检查
* @param item
* @returns {boolean}
*/
export function isObject (item) {
return (item && typeof item === 'object' && !Array.isArray(item))
}