动态主题初版本完成

This commit is contained in:
tangly1024
2022-03-09 17:28:47 +08:00
parent 69422fff7f
commit 24007eed1a
17 changed files with 58 additions and 49 deletions

View File

@@ -5,7 +5,6 @@
"@/*": ["./*"],
"@/components/*": ["components/*"],
"@/data/*": ["data/*"],
"@/layouts/*": ["theme/*"],
"@/lib/*": ["lib/*"],
"@/styles/*": ["styles/*"]
}

View File

@@ -3,7 +3,6 @@ import { useContext, createContext, useState } from 'react'
import Router from 'next/router'
import { initDarkMode } from './theme'
import BLOG from '@/blog.config'
import * as ThemeMap from '@/themes'
const GlobalContext = createContext()
let hasInit = false
@@ -18,8 +17,6 @@ export function GlobalContextProvider ({ children }) {
const [isDarkMode, updateDarkMode] = useState(false)
const [onLoading, changeLoadingState] = useState(false)
const [theme, setTheme] = useState(BLOG.THEME)
console.log('ThemeMap', ThemeMap)
const ThemeComponents = ThemeMap[theme]
Router.events.on('routeChangeStart', (...args) => {
changeLoadingState(true)
@@ -39,7 +36,7 @@ export function GlobalContextProvider ({ children }) {
}, 100)
return (
<GlobalContext.Provider value={{ onLoading, locale, isDarkMode, updateDarkMode, theme, setTheme, ThemeComponents }}>
<GlobalContext.Provider value={{ onLoading, locale, isDarkMode, updateDarkMode, theme, setTheme }}>
{children}
</GlobalContext.Provider>
)

View File

@@ -1,4 +1,5 @@
import { useGlobal } from '@/lib/global'
import * as ThemeMap from '@/themes'
/**
* 自定义404界面
@@ -7,7 +8,7 @@ import { useGlobal } from '@/lib/global'
*/
export default function Custom404 (props) {
const { ThemeComponents } = useGlobal()
const { theme } = useGlobal()
const ThemeComponents = ThemeMap[theme]
return <ThemeComponents.Layout404 {...props}/>
}

View File

@@ -3,6 +3,7 @@ import { getPostBlocks } from '@/lib/notion'
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import { useGlobal } from '@/lib/global'
import Custom404 from './404'
import * as ThemeMap from '@/themes'
/**
* 根据notion的slug访问页面针对类型为Page的页面
@@ -10,7 +11,8 @@ import Custom404 from './404'
* @returns
*/
const Slug = (props) => {
const { ThemeComponents } = useGlobal()
const { theme } = useGlobal()
const ThemeComponents = ThemeMap[theme]
if (!props.post) {
return <Custom404 {...props} />
}

View File

@@ -1,9 +1,11 @@
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import React from 'react'
import { useGlobal } from '@/lib/global'
import * as ThemeMap from '@/themes'
const ArchiveIndex = (props) => {
const { ThemeComponents } = useGlobal()
const { theme } = useGlobal()
const ThemeComponents = ThemeMap[theme]
return <ThemeComponents.LayoutArchive {...props}/>
}

View File

@@ -3,6 +3,7 @@ import { getPostBlocks } from '@/lib/notion'
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import { useGlobal } from '@/lib/global'
import Custom404 from '../404'
import * as ThemeMap from '@/themes'
/**
* 根据notion的slug访问页面
@@ -10,7 +11,8 @@ import Custom404 from '../404'
* @returns
*/
const Slug = (props) => {
const { ThemeComponents } = useGlobal()
const { theme } = useGlobal()
const ThemeComponents = ThemeMap[theme]
if (!props.post) {
return <Custom404 {...props} />
}

View File

@@ -1,9 +1,11 @@
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import React from 'react'
import { useGlobal } from '@/lib/global'
import * as ThemeMap from '@/themes'
export default function Category (props) {
const { ThemeComponents } = useGlobal()
const { theme } = useGlobal()
const ThemeComponents = ThemeMap[theme]
return <ThemeComponents.LayoutCategory {...props} />
}

View File

@@ -1,9 +1,11 @@
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import React from 'react'
import { useGlobal } from '@/lib/global'
import * as ThemeMap from '@/themes'
export default function Category (props) {
const { ThemeComponents } = useGlobal()
const { theme } = useGlobal()
const ThemeComponents = ThemeMap[theme]
return <ThemeComponents.LayoutCategoryIndex {...props}/>
}

View File

@@ -1,12 +1,11 @@
import BLOG from '@/blog.config'
import { getPostBlocks } from '@/lib/notion'
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import * as ThemeMap from '@/themes'
import { useGlobal } from '@/lib/global'
const Index = (props) => {
const { ThemeComponents } = useGlobal()
// return <ThemeComponents.Next.LayoutIndex {...props}/>
console.log(ThemeComponents)
// return <></>
const { theme } = useGlobal()
const ThemeComponents = ThemeMap[theme]
return <ThemeComponents.LayoutIndex {...props}/>
}

View File

@@ -3,9 +3,11 @@ import { getPostBlocks } from '@/lib/notion'
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import { useGlobal } from '@/lib/global'
import Custom404 from '../404'
import * as ThemeMap from '@/themes'
const Page = (props) => {
const { ThemeComponents } = useGlobal()
const { theme } = useGlobal()
const ThemeComponents = ThemeMap[theme]
if (!props?.meta) {
return <Custom404 {...props} />
}

View File

@@ -2,6 +2,7 @@ import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import BLOG from '@/blog.config'
import { useGlobal } from '@/lib/global'
import { getDataFromCache } from '@/lib/cache/cache_manager'
import * as ThemeMap from '@/themes'
const Index = (props) => {
const { keyword } = props
@@ -12,7 +13,8 @@ const Index = (props) => {
type: 'website'
}
const { ThemeComponents } = useGlobal()
const { theme } = useGlobal()
const ThemeComponents = ThemeMap[theme]
return <ThemeComponents.LayoutSearch {...props} meta={meta} currentSearch={keyword} />
}

View File

@@ -2,31 +2,7 @@ import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import BLOG from '@/blog.config'
import { useGlobal } from '@/lib/global'
import { useRouter } from 'next/router'
/**
* 浏览器前端搜索
*/
export async function getStaticProps () {
const {
allPosts,
categories,
tags,
postCount,
latestPosts,
customNav
} = await getGlobalNotionData({ from: 'search-props', pageType: ['Post'] })
return {
props: {
posts: allPosts,
tags,
categories,
postCount,
latestPosts,
customNav
},
revalidate: 1
}
}
import * as ThemeMap from '@/themes'
const Search = (props) => {
const { posts } = props
@@ -51,11 +27,30 @@ const Search = (props) => {
type: 'website'
}
const { ThemeComponents } = useGlobal()
const { theme } = useGlobal()
const ThemeComponents = ThemeMap[theme]
return <ThemeComponents.LayoutSearch {...props} posts={filteredPosts} meta={meta} currentSearch={searchKey} />
}
/**
* 浏览器前端搜索
*/
export async function getStaticProps () {
const { allPosts, categories, tags, postCount, latestPosts, customNav } = await getGlobalNotionData({ from: 'search-props', pageType: ['Post'] })
return {
props: {
posts: allPosts,
tags,
categories,
postCount,
latestPosts,
customNav
},
revalidate: 1
}
}
function getSearchKey () {
const router = useRouter()
if (router.query && router.query.s) {

View File

@@ -1,8 +1,10 @@
import { useGlobal } from '@/lib/global'
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import * as ThemeMap from '@/themes'
const Tag = (props) => {
const { ThemeComponents } = useGlobal()
const { theme } = useGlobal()
const ThemeComponents = ThemeMap[theme]
return <ThemeComponents.LayoutTag {...props} />
}

View File

@@ -1,9 +1,11 @@
import { getGlobalNotionData } from '@/lib/notion/getNotionData'
import React from 'react'
import { useGlobal } from '@/lib/global'
import * as ThemeMap from '@/themes'
const TagIndex = (props) => {
const { ThemeComponents } = useGlobal()
const { theme } = useGlobal()
const ThemeComponents = ThemeMap[theme]
return <ThemeComponents.LayoutTagIndex {...props} />
}

View File

@@ -1,4 +1,4 @@
import LayoutBase from '../Empty/LayoutBase'
import LayoutBase from './LayoutBase'
export const LayoutPage = (props) => {
const { page } = props

View File

@@ -1,6 +1,6 @@
import Link from 'next/link'
import React from 'react'
import JumpToTopButton from '@/themes/Medium/components/JumpToTopButton'
import JumpToTopButton from './JumpToTopButton'
export default function BottomMenuBar ({ className }) {
return (

View File

@@ -1,6 +1,6 @@
import LogoBar from '@/themes/Medium/components/LogoBar'
import Link from 'next/link'
import { useRouter } from 'next/router'
import LogoBar from './LogoBar'
/**
* 顶部导航栏 + 菜单