mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-16 07:26:47 +00:00
fix/theme-switch-repeat
This commit is contained in:
@@ -27,6 +27,7 @@ export function GlobalContextProvider({ children }) {
|
||||
useEffect(() => {
|
||||
initLocale(lang, locale, updateLang, updateLocale)
|
||||
initDarkMode(isDarkMode, updateDarkMode)
|
||||
initTheme()
|
||||
if (isBrowser()) {
|
||||
// 监听用户刷新页面
|
||||
const handleBeforeUnload = (event) => {
|
||||
@@ -55,10 +56,8 @@ export function GlobalContextProvider({ children }) {
|
||||
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)
|
||||
@@ -98,4 +97,20 @@ export function GlobalContextProvider({ children }) {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换主题时的特殊处理
|
||||
* @param {*} setTheme
|
||||
*/
|
||||
const initTheme = () => {
|
||||
if (isBrowser()) {
|
||||
setTimeout(() => {
|
||||
const elements = document.querySelectorAll('[id^="theme-"]')
|
||||
if (elements?.length > 1) {
|
||||
// 删除前面的元素,只保留最后一个元素
|
||||
elements[0].parentNode.removeChild(elements[0])
|
||||
elements.pop()
|
||||
}
|
||||
}, 500)
|
||||
}
|
||||
}
|
||||
export const useGlobal = () => useContext(GlobalContext)
|
||||
|
||||
Reference in New Issue
Block a user