部分参数配置化

This commit is contained in:
tangly1024.com
2023-11-03 17:10:59 +08:00
parent 545c071f81
commit f145d8b28c
23 changed files with 267 additions and 208 deletions

View File

@@ -1,6 +1,6 @@
import cookie from 'react-cookies'
import BLOG from '@/blog.config'
import { getQueryParam, getQueryVariable } from '../lib/utils'
import { getQueryParam, getQueryVariable, isBrowser } from '../lib/utils'
import dynamic from 'next/dynamic'
import getConfig from 'next/config'
import * as ThemeComponents from '@theme-components'
@@ -15,13 +15,38 @@ export const { THEMES = [] } = getConfig().publicRuntimeConfig
export const getLayoutByTheme = ({ router, theme }) => {
const themeQuery = getQueryParam(router.asPath, 'theme') || theme
const layoutName = getLayoutNameByPath(router.pathname)
if (themeQuery !== BLOG.THEME) {
return dynamic(() => import(`@/themes/${themeQuery}`).then(m => m[layoutName]), { ssr: true })
return dynamic(() => import(`@/themes/${themeQuery}`).then(m => {
setTimeout(() => {
checkThemeDOM()
}, 500);
return m[layoutName]
}), { ssr: true })
} else {
setTimeout(() => {
checkThemeDOM()
}, 100);
return ThemeComponents[layoutName]
}
}
/**
* 切换主题时的特殊处理
*/
const checkThemeDOM = () => {
if (isBrowser) {
const elements = document.querySelectorAll('[id^="theme-"]')
if (elements?.length > 1) {
elements[elements.length - 1].scrollIntoView()
// 删除前面的元素,只保留最后一个元素
for (let i = 0; i < elements.length - 1; i++) {
elements[i].parentNode.removeChild(elements[i])
}
}
}
}
/**
* 根据路径 获取对应的layout
* @param {*} path