mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-06-04 15:10:23 +00:00
fix/theme-switch-repeat
This commit is contained in:
@@ -27,6 +27,7 @@ export function GlobalContextProvider({ children }) {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
initLocale(lang, locale, updateLang, updateLocale)
|
initLocale(lang, locale, updateLang, updateLocale)
|
||||||
initDarkMode(isDarkMode, updateDarkMode)
|
initDarkMode(isDarkMode, updateDarkMode)
|
||||||
|
initTheme()
|
||||||
if (isBrowser()) {
|
if (isBrowser()) {
|
||||||
// 监听用户刷新页面
|
// 监听用户刷新页面
|
||||||
const handleBeforeUnload = (event) => {
|
const handleBeforeUnload = (event) => {
|
||||||
@@ -55,10 +56,8 @@ export function GlobalContextProvider({ children }) {
|
|||||||
NProgress.done()
|
NProgress.done()
|
||||||
setOnLoading(false)
|
setOnLoading(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
const queryTheme = getQueryVariable('theme') || BLOG.THEME
|
const queryTheme = getQueryVariable('theme') || BLOG.THEME
|
||||||
setTheme(queryTheme)
|
setTheme(queryTheme)
|
||||||
|
|
||||||
router.events.on('routeChangeStart', handleStart)
|
router.events.on('routeChangeStart', handleStart)
|
||||||
router.events.on('routeChangeError', handleStop)
|
router.events.on('routeChangeError', handleStop)
|
||||||
router.events.on('routeChangeComplete', 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)
|
export const useGlobal = () => useContext(GlobalContext)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import cookie from 'react-cookies'
|
import cookie from 'react-cookies'
|
||||||
import BLOG from '@/blog.config'
|
import BLOG from '@/blog.config'
|
||||||
import { getQueryParam, getQueryVariable, isBrowser } from '../lib/utils'
|
import { getQueryParam, getQueryVariable } from '../lib/utils'
|
||||||
import dynamic from 'next/dynamic'
|
import dynamic from 'next/dynamic'
|
||||||
// 使用 __THEME__ 变量来动态导入主题组件
|
// 使用 __THEME__ 变量来动态导入主题组件
|
||||||
import * as ThemeComponents from '@theme-components'
|
import * as ThemeComponents from '@theme-components'
|
||||||
@@ -21,14 +21,6 @@ export const getLayoutByTheme = (router) => {
|
|||||||
const themeQuery = getQueryParam(router.asPath, 'theme') || BLOG.THEME
|
const themeQuery = getQueryParam(router.asPath, 'theme') || BLOG.THEME
|
||||||
const layout = getLayoutNameByPath(router.pathname)
|
const layout = getLayoutNameByPath(router.pathname)
|
||||||
if (themeQuery !== BLOG.THEME) {
|
if (themeQuery !== BLOG.THEME) {
|
||||||
setTimeout(() => {
|
|
||||||
if (isBrowser()) {
|
|
||||||
const element = document?.getElementById('theme-' + themeQuery)
|
|
||||||
element?.scrollIntoView({
|
|
||||||
behavior: 'smooth'
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}, 500)
|
|
||||||
return dynamic(() => import(`@/themes/${themeQuery}/${layout}`), { ssr: true })
|
return dynamic(() => import(`@/themes/${themeQuery}/${layout}`), { ssr: true })
|
||||||
} else {
|
} else {
|
||||||
return ThemeComponents[layout]
|
return ThemeComponents[layout]
|
||||||
|
|||||||
Reference in New Issue
Block a user