feat/theme-switch

This commit is contained in:
tangly1024.com
2023-06-19 19:03:09 +08:00
parent 133fdd7c77
commit a042dc859a
28 changed files with 353 additions and 385 deletions

View File

@@ -3,6 +3,7 @@ import { useEffect, useState } from 'react'
import Select from './Select'
import { useGlobal } from '@/lib/global'
import { ALL_THEME } from '@/lib/theme'
import { useRouter } from 'next/router'
/**
*
@@ -10,16 +11,14 @@ import { ALL_THEME } from '@/lib/theme'
*/
const DebugPanel = () => {
const [show, setShow] = useState(false)
const { changeTheme, switchTheme, locale } = useGlobal()
const { theme, switchTheme, locale } = useGlobal()
const router = useRouter()
const [siteConfig, updateSiteConfig] = useState({})
// const [themeConfig, updateThemeConfig] = useState({})
const [debugTheme, updateDebugTheme] = useState(BLOG.THEME)
// 主题下拉框
const themeOptions = ALL_THEME.map(t => ({ value: t, text: t }))
useEffect(() => {
changeTheme(BLOG.THEME)
updateSiteConfig(Object.assign({}, BLOG))
// updateThemeConfig(Object.assign({}, ThemeMap[BLOG.THEME].THEME_CONFIG))
}, [])
@@ -29,15 +28,13 @@ const DebugPanel = () => {
}
function handleChangeDebugTheme() {
const newTheme = switchTheme()
// updateThemeConfig(Object.assign({}, ThemeMap[newTheme].THEME_CONFIG))
updateDebugTheme(newTheme)
switchTheme()
}
function handleUpdateDebugTheme(e) {
changeTheme(e)
// updateThemeConfig(Object.assign({}, ThemeMap[theme].THEME_CONFIG))
updateDebugTheme(e)
function handleUpdateDebugTheme(newTheme) {
console.log('触发切换主题', newTheme)
const query = { ...router.query, theme: newTheme }
router.push({ pathname: router.pathname, query })
}
function filterResult(text) {
@@ -75,7 +72,7 @@ const DebugPanel = () => {
<div className='flex'>
<Select
label={locale.COMMON.THEME_SWITCH}
value={debugTheme}
value={theme}
options={themeOptions}
onChange={handleUpdateDebugTheme}
/>

View File

@@ -2,15 +2,21 @@ import { useGlobal } from '@/lib/global'
import React from 'react'
import { Draggable } from './Draggable'
import { ALL_THEME } from '@/lib/theme'
import { useRouter } from 'next/router'
/**
*
* @returns 主题切换
*/
const ThemeSwitch = () => {
const { theme, changeTheme } = useGlobal()
const { theme } = useGlobal()
const router = useRouter()
// 修改当前路径url中的 theme 参数
// 例如 http://localhost?theme=hexo 跳转到 http://localhost?theme=newTheme
const onSelectChange = (e) => {
changeTheme(e.target.value)
const newTheme = e.target.value
const query = { ...router.query, theme: newTheme }
router.push({ pathname: router.pathname, query })
}
return (<>

View File

@@ -1,11 +1,11 @@
import { generateLocaleDict, initLocale } from './lang'
import { createContext, useContext, useEffect, useState } from 'react'
import Router, { useRouter } from 'next/router'
import { useRouter } from 'next/router'
import BLOG from '@/blog.config'
import { ALL_THEME, initDarkMode, initTheme, saveThemeToCookies } from '@/lib/theme'
import { ALL_THEME, initDarkMode } from '@/lib/theme'
import NProgress from 'nprogress'
import LoadingCover from '@/components/LoadingCover'
import { isBrowser } from './utils'
import { getQueryParam, isBrowser } from './utils'
const GlobalContext = createContext()
@@ -16,18 +16,17 @@ const GlobalContext = createContext()
* @constructor
*/
export function GlobalContextProvider({ children }) {
const router = useRouter()
const [lang, updateLang] = useState(BLOG.LANG) // 默认语言
const [locale, updateLocale] = useState(generateLocaleDict(BLOG.LANG)) // 默认语言
const [theme, setTheme] = useState(BLOG.THEME) // 默认博客主题
const [theme, setTheme] = useState(getQueryParam(router.asPath, 'theme') || BLOG.THEME) // 默认博客主题
const [isDarkMode, updateDarkMode] = useState(BLOG.APPEARANCE === 'dark') // 默认深色模式
const [onLoading, setOnLoading] = useState(false) // 抓取文章数据
const [onReading, setOnReading] = useState(false) // 网页资源加载
const router = useRouter()
useEffect(() => {
initLocale(lang, locale, updateLang, updateLocale)
initDarkMode(isDarkMode, updateDarkMode)
initTheme(theme, changeTheme)
if (isBrowser()) {
// 监听用户刷新页面
const handleBeforeUnload = (event) => {
@@ -45,10 +44,17 @@ export function GlobalContextProvider({ children }) {
useEffect(() => {
const handleStart = (url) => {
NProgress.start()
const { theme } = router.query
if (theme && !url.includes(`theme=${theme}`)) {
// TODO
const newUrl = `${url}${url.includes('?') ? '&' : '?'}theme=${theme}`
router.push(newUrl)
}
setOnLoading(true)
}
const handleStop = () => {
NProgress.done()
setTheme(getQueryParam(router.asPath, 'theme') || BLOG.THEME)
setOnLoading(false)
}
@@ -62,24 +68,16 @@ export function GlobalContextProvider({ children }) {
}
}, [router])
// 切换主题
function switchTheme() {
const currentIndex = ALL_THEME.indexOf(theme)
const newIndex = currentIndex < ALL_THEME.length - 1 ? currentIndex + 1 : 0
const newTheme = ALL_THEME[newIndex]
changeTheme(newTheme)
const query = { ...router.query, theme: newTheme }
router.push({ pathname: router.pathname, query })
return newTheme
}
function changeTheme(theme) {
Router.query.theme = ''
if (ALL_THEME.indexOf(theme) > -1) {
setTheme(theme)
} else {
setTheme(BLOG.THEME)
}
saveThemeToCookies(theme)
}
return (
<GlobalContext.Provider value={{
onLoading,
@@ -91,7 +89,6 @@ export function GlobalContextProvider({ children }) {
theme,
setTheme,
switchTheme,
changeTheme,
setOnReading
}}>
<LoadingCover onReading={onReading} setOnReading={setOnReading}/>

View File

@@ -1,6 +1,7 @@
import cookie from 'react-cookies'
import BLOG from '@/blog.config'
import { isBrowser, getQueryVariable } from './utils'
import { getQueryParam, getQueryVariable } from './utils'
import dynamic from 'next/dynamic'
/**
* 所有主题枚举
@@ -16,6 +17,62 @@ export const ALL_THEME = [
'simple'
]
/**
* 加载主题文件
* @param {*} router
* @returns
*/
export const getLayoutByTheme = (router) => {
const theme = getQueryParam(router.asPath, 'theme') || BLOG.THEME
let Layout = null
// 根据路由 pages的文件名加载主题文件
switch (router.pathname) {
case '/':
Layout = dynamic(() => import(`@/themes/${theme}/LayoutIndex`), { ssr: true })
break
case '/page/[page]':
Layout = dynamic(() => import(`@/themes/${theme}/LayoutPage`), { ssr: true })
break
case '/archive':
Layout = dynamic(() => import(`@/themes/${theme}/LayoutArchive`), { ssr: true })
break
case '/search':
Layout = dynamic(() => import(`@/themes/${theme}/LayoutSearch`), { ssr: true })
break
case '/search/[keyword]':
Layout = dynamic(() => import(`@/themes/${theme}/LayoutSearch`), { ssr: true })
break
case '/search/[keyword]/page/[page]':
Layout = dynamic(() => import(`@/themes/${theme}/LayoutSearch`), { ssr: true })
break
case '/404':
Layout = dynamic(() => import(`@/themes/${theme}/Layout404`), { ssr: true })
break
case '/tag':
Layout = dynamic(() => import(`@/themes/${theme}/LayoutTagIndex`), { ssr: true })
break
case '/tag/[tag]':
Layout = dynamic(() => import(`@/themes/${theme}/LayoutTag`), { ssr: true })
break
case '/tag/[tag]/page/[page]':
Layout = dynamic(() => import(`@/themes/${theme}/LayoutTag`), { ssr: true })
break
case '/category':
Layout = dynamic(() => import(`@/themes/${theme}/LayoutCategoryIndex`), { ssr: true })
break
case '/category/[category]':
Layout = dynamic(() => import(`@/themes/${theme}/LayoutCategory`), { ssr: true })
break
case '/category/[category]/page/[page]':
Layout = dynamic(() => import(`@/themes/${theme}/LayoutCategory`), { ssr: true })
break
default:
Layout = dynamic(() => import(`@/themes/${theme}/LayoutSlug`), { ssr: true })
break
}
return Layout
}
/**
* 初始化主题 , 优先级 query > cookies > systemPrefer
* @param isDarkMode
@@ -34,24 +91,6 @@ export const initDarkMode = (isDarkMode, updateDarkMode) => {
document.getElementsByTagName('html')[0].setAttribute('class', isDarkMode ? 'dark' : 'light')
}
/**
* 初始化主题, 优先级 query > cookies > blog.config.js
* @param {*} theme
* @param {*} changeTheme
*/
export const initTheme = (theme, changeTheme) => {
if (isBrowser()) {
// const queryTheme = getQueryVariable('theme') || loadThemeFromCookies() || BLOG.THEME
const queryTheme = getQueryVariable('theme') || BLOG.THEME
let currentTheme = theme
if (queryTheme !== theme && ALL_THEME.indexOf(queryTheme) > -1) {
currentTheme = queryTheme
}
changeTheme(currentTheme)
saveThemeToCookies(currentTheme)
}
}
/**
* 是否优先深色模式, 根据系统深色模式以及当前时间判断
* @returns {*}

View File

@@ -51,16 +51,27 @@ export function loadExternalResource(url, type) {
* @param {}} variable
* @returns
*/
export function getQueryVariable(variable) {
export function getQueryVariable(key) {
const query = isBrowser() ? 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] }
if (pair[0] === key) { return pair[1] }
}
return (false)
}
/**
* 获取 URL 中指定参数的值
* @param {string} url
* @param {string} param
* @returns {string|null}
*/
export function getQueryParam(url, param) {
const searchParams = new URLSearchParams(url.split('?')[1])
return searchParams.get(param)
}
/**
* 深度合并两个对象
* @param target

View File

@@ -1,15 +1,5 @@
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import { useGlobal } from '@/lib/global'
import dynamic from 'next/dynamic'
import Loading from '@/components/Loading'
import { Suspense, useEffect, useState } from 'react'
import BLOG from '@/blog.config'
const layout = 'Layout404'
/**
* 加载默认主题
*/
const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`), { ssr: true })
/**
* 404
@@ -17,22 +7,13 @@ const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`),
* @returns
*/
const NoFound = props => {
const { theme, siteInfo } = useGlobal()
const { Layout } = props
const { siteInfo } = useGlobal()
const meta = { title: `${props?.siteInfo?.title} | 页面找不到啦`, image: siteInfo?.pageCover }
const [Layout, setLayout] = useState(DefaultLayout)
useEffect(() => {
const loadLayout = async () => {
const newLayout = await dynamic(() => import(`@/themes/${theme}/${layout}`))
setLayout(newLayout)
}
loadLayout()
}, [theme])
props = { ...props, meta }
return <Suspense fallback={<Loading/>}>
<Layout {...props} />
</Suspense>
return <Layout {...props} />
}
export async function getStaticProps () {

View File

@@ -1,7 +1,6 @@
import BLOG from '@/blog.config'
import { getPostBlocks } from '@/lib/notion'
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import { useGlobal } from '@/lib/global'
import { Suspense, useEffect, useState } from 'react'
import { idToUuid } from 'notion-utils'
import { useRouter } from 'next/router'
@@ -9,35 +8,16 @@ import { isBrowser } from '@/lib/utils'
import { getNotion } from '@/lib/notion/getNotion'
import { getPageTableOfContents } from '@/lib/notion/getPageTableOfContents'
import md5 from 'js-md5'
import dynamic from 'next/dynamic'
import Loading from '@/components/Loading'
const layout = 'LayoutSlug'
/**
* 懒加载默认主题
*/
const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`), { ssr: true })
/**
* 根据notion的slug访问页面
* @param {*} props
* @returns
*/
const Slug = props => {
const { theme, setOnLoading } = useGlobal()
const { post, siteInfo } = props
const { post, siteInfo, Layout } = props
const router = useRouter()
const [Layout, setLayout] = useState(DefaultLayout)
// 切换主题
useEffect(() => {
const loadLayout = async () => {
const newLayout = await dynamic(() => import(`@/themes/${theme}/${layout}`))
setLayout(newLayout)
}
loadLayout()
}, [theme])
// 文章锁🔐
const [lock, setLock] = useState(post?.password && post?.password !== '')
@@ -57,7 +37,6 @@ const Slug = props => {
// 文章加载
useEffect(() => {
setOnLoading(false)
// 404
if (!post) {
setTimeout(() => {

View File

@@ -19,6 +19,8 @@ import dynamic from 'next/dynamic'
// 自定义样式css和js引入
import ExternalScript from '@/components/ExternalScript'
import { useRouter } from 'next/router'
import { getLayoutByTheme } from '@/lib/theme'
// 各种扩展插件 动画等
const ExternalPlugins = dynamic(() => import('@/components/ExternalPlugins'))
@@ -31,11 +33,14 @@ const MyApp = ({ Component, pageProps }) => {
}
}, [])
// 根据页面路径加载不同Layout文件
const Layout = getLayoutByTheme(useRouter())
return (
<GlobalContextProvider>
<Component {...pageProps} />
<ExternalPlugins {...pageProps}/>
<ExternalScript/>
<Component {...pageProps} Layout={Layout} />
<ExternalPlugins {...pageProps} />
<ExternalScript />
</GlobalContextProvider>
)
}

View File

@@ -1,29 +1,11 @@
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import React, { Suspense, useEffect, useState } from 'react'
import React from 'react'
import { useGlobal } from '@/lib/global'
import dynamic from 'next/dynamic'
import BLOG from '@/blog.config'
import Loading from '@/components/Loading'
const layout = 'LayoutArchive'
/**
* 加载默认主题
*/
const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`), { ssr: true })
const ArchiveIndex = props => {
const { siteInfo } = props
const { theme, locale } = useGlobal()
const [Layout, setLayout] = useState(DefaultLayout)
// 切换主题
useEffect(() => {
const loadLayout = async () => {
const newLayout = await dynamic(() => import(`@/themes/${theme}/${layout}`))
setLayout(newLayout)
}
loadLayout()
}, [theme])
const { siteInfo, Layout } = props
const { locale } = useGlobal()
const meta = {
title: `${locale.NAV.ARCHIVE} | ${siteInfo?.title}`,
@@ -35,9 +17,7 @@ const ArchiveIndex = props => {
props = { ...props, meta }
return <Suspense fallback={<Loading/>}>
<Layout {...props} />
</Suspense>
return <Layout {...props} />
}
export async function getStaticProps() {

View File

@@ -1,15 +1,7 @@
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import React, { Suspense, useEffect, useState } from 'react'
import React from 'react'
import { useGlobal } from '@/lib/global'
import dynamic from 'next/dynamic'
import BLOG from '@/blog.config'
import Loading from '@/components/Loading'
const layout = 'LayoutCategory'
/**
* 加载默认主题
*/
const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`), { ssr: true })
/**
* 分类页
@@ -17,18 +9,8 @@ const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`),
* @returns
*/
export default function Category(props) {
const { siteInfo } = props
const { locale, theme } = useGlobal()
const [Layout, setLayout] = useState(DefaultLayout)
// 切换主题
useEffect(() => {
const loadLayout = async () => {
const newLayout = await dynamic(() => import(`@/themes/${theme}/${layout}`))
setLayout(newLayout)
}
loadLayout()
}, [theme])
const { siteInfo, Layout } = props
const { locale } = useGlobal()
const meta = {
title: `${props.category} | ${locale.COMMON.CATEGORY} | ${
@@ -42,9 +24,7 @@ export default function Category(props) {
props = { ...props, meta }
return <Suspense fallback={<Loading/>}>
<Layout {...props} />
</Suspense>
return <Layout {...props} />
}
export async function getStaticProps({ params: { category } }) {

View File

@@ -1,16 +1,7 @@
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import React, { Suspense, useEffect, useState } from 'react'
import React from 'react'
import { useGlobal } from '@/lib/global'
import dynamic from 'next/dynamic'
import BLOG from '@/blog.config'
import Loading from '@/components/Loading'
const layout = 'LayoutCategory'
/**
* 加载默认主题
*/
const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`), { ssr: true })
/**
* 分类页
@@ -18,18 +9,8 @@ const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`),
* @returns
*/
export default function Category(props) {
const { theme } = useGlobal()
const { siteInfo } = props
const { siteInfo, Layout } = props
const { locale } = useGlobal()
const [Layout, setLayout] = useState(DefaultLayout)
// 切换主题
useEffect(() => {
const loadLayout = async () => {
const newLayout = await dynamic(() => import(`@/themes/${theme}/${layout}`))
setLayout(newLayout)
}
loadLayout()
}, [theme])
const meta = {
title: `${props.category} | ${locale.COMMON.CATEGORY} | ${
@@ -43,9 +24,7 @@ export default function Category(props) {
props = { ...props, meta }
return <Suspense fallback={<Loading/>}>
<Layout {...props} />
</Suspense>
return <Layout {...props} />
}
export async function getStaticProps({ params: { category, page } }) {

View File

@@ -1,16 +1,7 @@
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import React, { Suspense, useEffect, useState } from 'react'
import React from 'react'
import { useGlobal } from '@/lib/global'
import dynamic from 'next/dynamic'
import BLOG from '@/blog.config'
import Loading from '@/components/Loading'
const layout = 'LayoutCategoryIndex'
/**
* 加载默认主题
*/
const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`), { ssr: true })
/**
* 分类首页
@@ -18,19 +9,8 @@ const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`),
* @returns
*/
export default function Category(props) {
const { theme } = useGlobal()
const { locale } = useGlobal()
const { siteInfo } = props
const [Layout, setLayout] = useState(DefaultLayout)
// 切换主题
useEffect(() => {
const loadLayout = async () => {
const newLayout = await dynamic(() => import(`@/themes/${theme}/${layout}`))
setLayout(newLayout)
}
loadLayout()
}, [theme])
const { siteInfo, Layout } = props
const meta = {
title: `${locale.COMMON.CATEGORY} | ${siteInfo?.title}`,
@@ -41,9 +21,7 @@ export default function Category(props) {
}
props = { ...props, meta }
return <Suspense fallback={<Loading/>}>
<Layout {...props} />
</Suspense>
return <Layout {...props} />
}
export async function getStaticProps() {

View File

@@ -1,19 +1,8 @@
import BLOG from '@/blog.config'
import { getPostBlocks } from '@/lib/notion'
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import { useGlobal } from '@/lib/global'
import { generateRss } from '@/lib/rss'
import { generateRobotsTxt } from '@/lib/robots.txt'
import dynamic from 'next/dynamic'
import { Suspense, useEffect, useState } from 'react'
import Loading from '@/components/Loading'
const layout = 'LayoutIndex'
/**
* 加载默认主题
*/
const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`), { ssr: true })
/**
* 首页布局
@@ -21,21 +10,8 @@ const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`),
* @returns
*/
const Index = props => {
// 动态切换主题
const { theme } = useGlobal()
const [Layout, setLayout] = useState(DefaultLayout)
// 切换主题
useEffect(() => {
const loadLayout = async () => {
const newLayout = await dynamic(() => import(`@/themes/${theme}/${layout}`))
setLayout(newLayout)
}
loadLayout()
}, [theme])
return <Suspense fallback={<Loading />}>
<Layout {...props} />
</Suspense>
const { Layout } = props
return <Layout {...props} />
}
/**

View File

@@ -1,17 +1,6 @@
import BLOG from '@/blog.config'
import { getPostBlocks } from '@/lib/notion'
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import { useGlobal } from '@/lib/global'
import dynamic from 'next/dynamic'
import { Suspense, useEffect, useState } from 'react'
import Loading from '@/components/Loading'
const layout = 'LayoutPage'
/**
* 加载默认主题
*/
const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`), { ssr: true })
/**
* 文章列表分页
@@ -19,18 +8,7 @@ const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`),
* @returns
*/
const Page = props => {
const { theme } = useGlobal()
const { siteInfo } = props
const [Layout, setLayout] = useState(DefaultLayout)
// 切换主题
useEffect(() => {
const loadLayout = async () => {
const newLayout = await dynamic(() => import(`@/themes/${theme}/${layout}`))
setLayout(newLayout)
}
loadLayout()
}, [theme])
const { siteInfo, Layout } = props
const meta = {
title: `${props?.page} | Page | ${siteInfo?.title}`,
description: siteInfo?.description,
@@ -41,9 +19,7 @@ const Page = props => {
props = { ...props, meta }
return <Suspense fallback={<Loading />}>
<Layout {...props} />
</Suspense>
return <Layout {...props} />
}
export async function getStaticPaths() {

View File

@@ -2,28 +2,10 @@ import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import { useGlobal } from '@/lib/global'
import { getDataFromCache } from '@/lib/cache/cache_manager'
import BLOG from '@/blog.config'
import dynamic from 'next/dynamic'
import { Suspense, useEffect, useState } from 'react'
import Loading from '@/components/Loading'
const layout = 'LayoutSearch'
/**
* 加载默认主题
*/
const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`), { ssr: true })
const Index = props => {
const { keyword, siteInfo } = props
const { locale, theme } = useGlobal()
const [Layout, setLayout] = useState(DefaultLayout)
// 切换主题
useEffect(() => {
const loadLayout = async () => {
const newLayout = await dynamic(() => import(`@/themes/${theme}/${layout}`))
setLayout(newLayout)
}
loadLayout()
}, [theme])
const { keyword, siteInfo, Layout } = props
const { locale } = useGlobal()
const meta = {
title: `${keyword || ''}${keyword ? ' | ' : ''}${locale.NAV.SEARCH} | ${siteInfo?.title}`,
@@ -35,9 +17,7 @@ const Index = props => {
props = { ...props, meta }
return <Suspense fallback={<Loading/>}>
<Layout {...props} />
</Suspense>
return <Layout {...props} />
}
/**

View File

@@ -1,29 +1,11 @@
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import { useGlobal } from '@/lib/global'
import { getDataFromCache } from '@/lib/cache/cache_manager'
import dynamic from 'next/dynamic'
import BLOG from '@/blog.config'
import { Suspense, useEffect, useState } from 'react'
import Loading from '@/components/Loading'
const layout = 'LayoutSearch'
/**
* 加载默认主题
*/
const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`), { ssr: true })
const Index = props => {
const { keyword, siteInfo } = props
const { locale, theme } = useGlobal()
const [Layout, setLayout] = useState(DefaultLayout)
// 切换主题
useEffect(() => {
const loadLayout = async () => {
const newLayout = await dynamic(() => import(`@/themes/${theme}/${layout}`))
setLayout(newLayout)
}
loadLayout()
}, [theme])
const { keyword, siteInfo, Layout } = props
const { locale } = useGlobal()
const meta = {
title: `${keyword || ''}${keyword ? ' | ' : ''}${locale.NAV.SEARCH} | ${siteInfo?.title}`,
@@ -35,9 +17,7 @@ const Index = props => {
props = { ...props, meta, currentSearch: keyword }
return <Suspense fallback={<Loading/>}>
<Layout {...props} />
</Suspense>
return <Layout {...props} />
}
/**

View File

@@ -2,31 +2,14 @@ import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import { useGlobal } from '@/lib/global'
import { useRouter } from 'next/router'
import BLOG from '@/blog.config'
import dynamic from 'next/dynamic'
import { Suspense, useEffect, useState } from 'react'
import Loading from '@/components/Loading'
const layout = 'LayoutSearch'
/**
* 加载默认主题
*/
const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`), { ssr: true })
const Search = props => {
const { posts, siteInfo } = props
const { theme, locale } = useGlobal()
const [Layout, setLayout] = useState(DefaultLayout)
const { posts, siteInfo, Layout } = props
const { locale } = useGlobal()
const router = useRouter()
const keyword = getSearchKey(router)
// 切换主题
useEffect(() => {
const loadLayout = async () => {
const newLayout = await dynamic(() => import(`@/themes/${theme}/${layout}`))
setLayout(newLayout)
}
loadLayout()
}, [theme])
let filteredPosts
// 静态过滤
if (keyword) {
@@ -51,9 +34,7 @@ const Search = props => {
props = { ...props, meta, posts: filteredPosts }
return <Suspense fallback={<Loading />}>
<Layout {...props} />
</Suspense>
return <Layout {...props} />
}
/**

View File

@@ -1,29 +1,15 @@
import { useGlobal } from '@/lib/global'
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import BLOG from '@/blog.config'
import dynamic from 'next/dynamic'
import { Suspense, useEffect, useState } from 'react'
import Loading from '@/components/Loading'
const layout = 'LayoutTag'
/**
* 加载默认主题
* 标签下的文章列表
* @param {*} props
* @returns
*/
const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`), { ssr: true })
const Tag = props => {
const { theme } = useGlobal()
const { locale } = useGlobal()
const { tag, siteInfo } = props
const [Layout, setLayout] = useState(DefaultLayout)
// 切换主题
useEffect(() => {
const loadLayout = async () => {
const newLayout = await dynamic(() => import(`@/themes/${theme}/${layout}`))
setLayout(newLayout)
}
loadLayout()
}, [theme])
const { tag, siteInfo, Layout } = props
const meta = {
title: `${tag} | ${locale.COMMON.TAGS} | ${siteInfo?.title}`,
@@ -34,9 +20,7 @@ const Tag = props => {
}
props = { ...props, meta }
return <Suspense fallback={<Loading/>}>
<Layout {...props} />
</Suspense>
return <Layout {...props} />
}
export async function getStaticProps({ params: { tag } }) {

View File

@@ -1,29 +1,10 @@
import { useGlobal } from '@/lib/global'
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import BLOG from '@/blog.config'
import dynamic from 'next/dynamic'
import { Suspense, useEffect, useState } from 'react'
import Loading from '@/components/Loading'
const layout = 'LayoutTag'
/**
* 加载默认主题
*/
const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`), { ssr: true })
const Tag = props => {
const { theme } = useGlobal()
const { locale } = useGlobal()
const { tag, siteInfo } = props
const [Layout, setLayout] = useState(DefaultLayout)
// 切换主题
useEffect(() => {
const loadLayout = async () => {
const newLayout = await dynamic(() => import(`@/themes/${theme}/${layout}`))
setLayout(newLayout)
}
loadLayout()
}, [theme])
const { tag, siteInfo, Layout } = props
const meta = {
title: `${tag} | ${locale.COMMON.TAGS} | ${siteInfo?.title}`,
@@ -34,9 +15,7 @@ const Tag = props => {
}
props = { ...props, meta }
return <Suspense fallback={<Loading/>}>
<Layout {...props} />
</Suspense>
return <Layout {...props} />
}
export async function getStaticProps({ params: { tag, page } }) {

View File

@@ -1,14 +1,6 @@
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import { Suspense, useEffect, useState } from 'react'
import { useGlobal } from '@/lib/global'
import BLOG from '@/blog.config'
import dynamic from 'next/dynamic'
import Loading from '@/components/Loading'
const layout = 'LayoutTagIndex'
/**
* 默认主题
*/
const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`), { ssr: true })
/**
* 标签首页
@@ -17,17 +9,7 @@ const DefaultLayout = dynamic(() => import(`@/themes/${BLOG.THEME}/${layout}`),
*/
const TagIndex = props => {
const { locale } = useGlobal()
const { siteInfo } = props
const { theme } = useGlobal()
const [Layout, setLayout] = useState(DefaultLayout)
// 切换主题
useEffect(() => {
const loadLayout = async () => {
const newLayout = await dynamic(() => import(`@/themes/${theme}/${layout}`))
setLayout(newLayout)
}
loadLayout()
}, [theme])
const { siteInfo, Layout } = props
const meta = {
title: `${locale.COMMON.TAGS} | ${siteInfo?.title}`,
@@ -38,9 +20,7 @@ const TagIndex = props => {
}
props = { ...props, meta }
return <Suspense fallback={<Loading/>}>
<Layout {...props} />
</Suspense>
return <Layout {...props} />
}
export async function getStaticProps() {

25
themes/example/index.js Normal file
View File

@@ -0,0 +1,25 @@
import CONFIG_EXAMPLE from './config_example'
import { LayoutIndex } from './LayoutIndex'
import { LayoutSearch } from './LayoutSearch'
import { LayoutArchive } from './LayoutArchive'
import { LayoutSlug } from './LayoutSlug'
import { Layout404 } from './Layout404'
import { LayoutCategory } from './LayoutCategory'
import { LayoutCategoryIndex } from './LayoutCategoryIndex'
import { LayoutPage } from './LayoutPage'
import { LayoutTag } from './LayoutTag'
import { LayoutTagIndex } from './LayoutTagIndex'
export {
CONFIG_EXAMPLE as THEME_CONFIG,
LayoutIndex,
LayoutSearch,
LayoutArchive,
LayoutSlug,
Layout404,
LayoutCategory,
LayoutCategoryIndex,
LayoutPage,
LayoutTag,
LayoutTagIndex
}

25
themes/fukasawa/index.js Normal file
View File

@@ -0,0 +1,25 @@
import CONFIG_FUKA from './config_fuka'
import { LayoutIndex } from './LayoutIndex'
import { LayoutSearch } from './LayoutSearch'
import { LayoutArchive } from './LayoutArchive'
import { LayoutSlug } from './LayoutSlug'
import { Layout404 } from './Layout404'
import { LayoutCategory } from './LayoutCategory'
import { LayoutCategoryIndex } from './LayoutCategoryIndex'
import { LayoutPage } from './LayoutPage'
import { LayoutTag } from './LayoutTag'
import { LayoutTagIndex } from './LayoutTagIndex'
export {
CONFIG_FUKA as THEME_CONFIG,
LayoutIndex,
LayoutSearch,
LayoutArchive,
LayoutSlug,
Layout404,
LayoutCategory,
LayoutCategoryIndex,
LayoutPage,
LayoutTag,
LayoutTagIndex
}

25
themes/hexo/index.js Normal file
View File

@@ -0,0 +1,25 @@
import CONFIG_HEXO from './config_hexo'
import { LayoutIndex } from './LayoutIndex'
import { LayoutSearch } from './LayoutSearch'
import { LayoutArchive } from './LayoutArchive'
import { LayoutSlug } from './LayoutSlug'
import { Layout404 } from './Layout404'
import { LayoutCategory } from './LayoutCategory'
import { LayoutCategoryIndex } from './LayoutCategoryIndex'
import { LayoutPage } from './LayoutPage'
import { LayoutTag } from './LayoutTag'
import { LayoutTagIndex } from './LayoutTagIndex'
export {
CONFIG_HEXO as THEME_CONFIG,
LayoutIndex,
LayoutSearch,
LayoutArchive,
LayoutSlug,
Layout404,
LayoutCategory,
LayoutCategoryIndex,
LayoutPage,
LayoutTag,
LayoutTagIndex
}

25
themes/matery/indexj.js Normal file
View File

@@ -0,0 +1,25 @@
import CONFIG_MATERY from './config_matery'
import { LayoutIndex } from './LayoutIndex'
import { LayoutSearch } from './LayoutSearch'
import { LayoutArchive } from './LayoutArchive'
import { LayoutSlug } from './LayoutSlug'
import { Layout404 } from './Layout404'
import { LayoutCategory } from './LayoutCategory'
import { LayoutCategoryIndex } from './LayoutCategoryIndex'
import { LayoutPage } from './LayoutPage'
import { LayoutTag } from './LayoutTag'
import { LayoutTagIndex } from './LayoutTagIndex'
export {
CONFIG_MATERY as THEME_CONFIG,
LayoutIndex,
LayoutSearch,
LayoutArchive,
LayoutSlug,
Layout404,
LayoutCategory,
LayoutCategoryIndex,
LayoutPage,
LayoutTag,
LayoutTagIndex
}

25
themes/medium/index.js Normal file
View File

@@ -0,0 +1,25 @@
import CONFIG_MEDIUM from './config_medium'
import { LayoutIndex } from './LayoutIndex'
import { LayoutSearch } from './LayoutSearch'
import { LayoutArchive } from './LayoutArchive'
import { LayoutSlug } from './LayoutSlug'
import { Layout404 } from './Layout404'
import { LayoutCategory } from './LayoutCategory'
import { LayoutCategoryIndex } from './LayoutCategoryIndex'
import { LayoutPage } from './LayoutPage'
import { LayoutTag } from './LayoutTag'
import { LayoutTagIndex } from './LayoutTagIndex'
export {
CONFIG_MEDIUM as THEME_CONFIG,
LayoutIndex,
LayoutSearch,
LayoutArchive,
LayoutSlug,
Layout404,
LayoutCategory,
LayoutCategoryIndex,
LayoutPage,
LayoutTag,
LayoutTagIndex
}

25
themes/next/index.js Normal file
View File

@@ -0,0 +1,25 @@
import CONFIG_NEXT from './config_next'
import { LayoutIndex } from './LayoutIndex'
import { LayoutSearch } from './LayoutSearch'
import { LayoutArchive } from './LayoutArchive'
import { LayoutSlug } from './LayoutSlug'
import { Layout404 } from './Layout404'
import { LayoutCategory } from './LayoutCategory'
import { LayoutCategoryIndex } from './LayoutCategoryIndex'
import { LayoutPage } from './LayoutPage'
import { LayoutTag } from './LayoutTag'
import { LayoutTagIndex } from './LayoutTagIndex'
export {
CONFIG_NEXT as THEME_CONFIG,
LayoutIndex,
LayoutSearch,
LayoutArchive,
LayoutSlug,
Layout404,
LayoutCategory,
LayoutCategoryIndex,
LayoutPage,
LayoutTag,
LayoutTagIndex
}

25
themes/nobelium/index.js Normal file
View File

@@ -0,0 +1,25 @@
import CONFIG_NOBELIUM from './config_nobelium'
import { LayoutIndex } from './LayoutIndex'
import { LayoutSearch } from './LayoutSearch'
import { LayoutArchive } from './LayoutArchive'
import { LayoutSlug } from './LayoutSlug'
import { Layout404 } from './Layout404'
import { LayoutCategory } from './LayoutCategory'
import { LayoutCategoryIndex } from './LayoutCategoryIndex'
import { LayoutPage } from './LayoutPage'
import { LayoutTag } from './LayoutTag'
import { LayoutTagIndex } from './LayoutTagIndex'
export {
CONFIG_NOBELIUM as THEME_CONFIG,
LayoutIndex,
LayoutSearch,
LayoutArchive,
LayoutSlug,
Layout404,
LayoutCategory,
LayoutCategoryIndex,
LayoutPage,
LayoutTag,
LayoutTagIndex
}

25
themes/simple/index.js Normal file
View File

@@ -0,0 +1,25 @@
import CONFIG_SIMPLE from './config_simple'
import { LayoutIndex } from './LayoutIndex'
import { LayoutSearch } from './LayoutSearch'
import { LayoutArchive } from './LayoutArchive'
import { LayoutSlug } from './LayoutSlug'
import { Layout404 } from './Layout404'
import { LayoutCategory } from './LayoutCategory'
import { LayoutCategoryIndex } from './LayoutCategoryIndex'
import { LayoutPage } from './LayoutPage'
import { LayoutTag } from './LayoutTag'
import { LayoutTagIndex } from './LayoutTagIndex'
export {
CONFIG_SIMPLE as THEME_CONFIG,
LayoutIndex,
LayoutSearch,
LayoutArchive,
LayoutSlug,
Layout404,
LayoutCategory,
LayoutCategoryIndex,
LayoutPage,
LayoutTag,
LayoutTagIndex
}