From bcddec57481364afbd111d4b679481fe06f69663 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Thu, 29 Jun 2023 15:55:23 +0800 Subject: [PATCH] gitbook-collapse --- components/Collapse.js | 2 +- themes/gitbook/LayoutBase.js | 6 +-- themes/gitbook/LayoutCategory.js | 4 +- themes/gitbook/LayoutSearch.js | 4 +- themes/gitbook/LayoutTag.js | 4 +- themes/gitbook/components/BlogPostCard.js | 14 ++---- themes/gitbook/components/BlogPostListPage.js | 4 +- .../gitbook/components/BlogPostListScroll.js | 35 -------------- themes/gitbook/components/NavPostItem.js | 39 ++++++++++++++++ themes/gitbook/components/NavPostList.js | 46 +++++++++++++++++++ ...ogPostListEmpty.js => NavPostListEmpty.js} | 4 +- themes/gitbook/components/PageNavDrawer.js | 4 +- themes/gitbook/components/SearchInput.js | 6 +-- 13 files changed, 107 insertions(+), 65 deletions(-) delete mode 100644 themes/gitbook/components/BlogPostListScroll.js create mode 100644 themes/gitbook/components/NavPostItem.js create mode 100644 themes/gitbook/components/NavPostList.js rename themes/gitbook/components/{BlogPostListEmpty.js => NavPostListEmpty.js} (78%) diff --git a/components/Collapse.js b/components/Collapse.js index 99629604..d9942968 100644 --- a/components/Collapse.js +++ b/components/Collapse.js @@ -84,7 +84,7 @@ const Collapse = props => { }, [props.isOpen]) return ( -
+
{props.children}
) diff --git a/themes/gitbook/LayoutBase.js b/themes/gitbook/LayoutBase.js index d0312871..10a9f7bd 100644 --- a/themes/gitbook/LayoutBase.js +++ b/themes/gitbook/LayoutBase.js @@ -9,7 +9,7 @@ import SearchInput from './components/SearchInput' import { useGlobal } from '@/lib/global' import Live2D from '@/components/Live2D' import BLOG from '@/blog.config' -import BlogPostListScroll from './components/BlogPostListScroll' +import NavPostList from './components/NavPostList' import ArticleInfo from './components/ArticleInfo' import Catalog from './components/Catalog' import { useRouter } from 'next/router' @@ -61,10 +61,10 @@ const LayoutBase = (props) => {
{slotLeft} - + {/* 所有文章列表 */} - + diff --git a/themes/gitbook/LayoutCategory.js b/themes/gitbook/LayoutCategory.js index a947b70e..8c22757e 100644 --- a/themes/gitbook/LayoutCategory.js +++ b/themes/gitbook/LayoutCategory.js @@ -1,5 +1,5 @@ import LayoutBase from './LayoutBase' -import BlogPostListScroll from './components/BlogPostListScroll' +import NavPostList from './components/NavPostList' import BlogPostListPage from './components/BlogPostListPage' import BLOG from '@/blog.config' @@ -8,7 +8,7 @@ export const LayoutCategory = props => { const slotTop =
分类:
{category}
return - {BLOG.POST_LIST_STYLE === 'page' ? : } + {BLOG.POST_LIST_STYLE === 'page' ? : } } diff --git a/themes/gitbook/LayoutSearch.js b/themes/gitbook/LayoutSearch.js index eaecbd84..f67fd1b0 100644 --- a/themes/gitbook/LayoutSearch.js +++ b/themes/gitbook/LayoutSearch.js @@ -7,7 +7,7 @@ import { useEffect } from 'react' import { isBrowser } from '@/lib/utils' import BLOG from '@/blog.config' import Mark from 'mark.js' -import BlogPostListScroll from './components/BlogPostListScroll' +import NavPostList from './components/NavPostList' import BlogPostListPage from './components/BlogPostListPage' import { useRouter } from 'next/router' @@ -45,7 +45,7 @@ export const LayoutSearch = (props) => {
{currentSearch &&
- {BLOG.POST_LIST_STYLE === 'page' ? : } + {BLOG.POST_LIST_STYLE === 'page' ? : }
} } diff --git a/themes/gitbook/LayoutTag.js b/themes/gitbook/LayoutTag.js index bedb51ef..03e33788 100644 --- a/themes/gitbook/LayoutTag.js +++ b/themes/gitbook/LayoutTag.js @@ -1,6 +1,6 @@ import LayoutBase from './LayoutBase' import BLOG from '@/blog.config' -import BlogPostListScroll from './components/BlogPostListScroll' +import NavPostList from './components/NavPostList' import BlogPostListPage from './components/BlogPostListPage' export const LayoutTag = (props) => { @@ -8,7 +8,7 @@ export const LayoutTag = (props) => { const slotTop =
标签:
{tag}
return - {BLOG.POST_LIST_STYLE === 'page' ? : } + {BLOG.POST_LIST_STYLE === 'page' ? : } } diff --git a/themes/gitbook/components/BlogPostCard.js b/themes/gitbook/components/BlogPostCard.js index d80c94bd..bb47fcc2 100644 --- a/themes/gitbook/components/BlogPostCard.js +++ b/themes/gitbook/components/BlogPostCard.js @@ -7,18 +7,10 @@ const BlogPostCard = ({ post, className }) => { const router = useRouter() const currentSelected = router.asPath.split('?')[0] === '/' + post.slug return ( -
+
- -
- {post.title} -
- + + {post.title}
diff --git a/themes/gitbook/components/BlogPostListPage.js b/themes/gitbook/components/BlogPostListPage.js index db74b74e..a77b288b 100644 --- a/themes/gitbook/components/BlogPostListPage.js +++ b/themes/gitbook/components/BlogPostListPage.js @@ -1,6 +1,6 @@ import BlogPostCard from './BlogPostCard' import BLOG from '@/blog.config' -import BlogPostListEmpty from './BlogPostListEmpty' +import NavPostListEmpty from './NavPostListEmpty' import PaginationSimple from './PaginationSimple' /** @@ -15,7 +15,7 @@ const BlogPostListPage = ({ page = 1, posts = [], postCount }) => { const totalPage = Math.ceil(postCount / BLOG.POSTS_PER_PAGE) if (!posts || posts.length === 0) { - return + return } return ( diff --git a/themes/gitbook/components/BlogPostListScroll.js b/themes/gitbook/components/BlogPostListScroll.js deleted file mode 100644 index 038b7a1d..00000000 --- a/themes/gitbook/components/BlogPostListScroll.js +++ /dev/null @@ -1,35 +0,0 @@ -import BlogPostCard from './BlogPostCard' -import BlogPostListEmpty from './BlogPostListEmpty' -import React, { useRef } from 'react' - -/** - * 博客列表滚动分页 - * @param posts 所有文章 - * @param tags 所有标签 - * @returns {JSX.Element} - * @constructor - */ -const BlogPostListScroll = ({ posts = [], currentSearch }) => { - const targetRef = useRef(null) - const filteredPosts = Object.assign(posts) - - if (!filteredPosts || filteredPosts.length === 0) { - return - } else { - return
- {/* 文章列表 */} - {filteredPosts?.map((group, index) => { - if (group.category) { - return
-
{group.category}
- {group.items?.map(post => (
))} -
- } else { - return
{group.items?.map(post => ())}
- } - })} -
- } -} - -export default BlogPostListScroll diff --git a/themes/gitbook/components/NavPostItem.js b/themes/gitbook/components/NavPostItem.js new file mode 100644 index 00000000..5ddc7ecf --- /dev/null +++ b/themes/gitbook/components/NavPostItem.js @@ -0,0 +1,39 @@ +import BlogPostCard from './BlogPostCard' +import React, { useState } from 'react' +import Collapse from '@/components/Collapse' + +/** + * 导航列表 + * @param posts 所有文章 + * @param tags 所有标签 + * @returns {JSX.Element} + * @constructor + */ +const NavPostItem = (props) => { + const { group } = props + const [isOpen, changeIsOpen] = useState(group?.selected) + + const toggleOpenSubMenu = () => { + changeIsOpen(!isOpen) + } + + if (group.category) { + return <> +
+ {group.category} +
+
+ + {group.items?.map(post => (
+
)) + } +
+ + } else { + return
{group.items?.map(post => ())}
+ } +} + +export default NavPostItem diff --git a/themes/gitbook/components/NavPostList.js b/themes/gitbook/components/NavPostList.js new file mode 100644 index 00000000..0ef304d2 --- /dev/null +++ b/themes/gitbook/components/NavPostList.js @@ -0,0 +1,46 @@ +import NavPostListEmpty from './NavPostListEmpty' +import { useRouter } from 'next/router' +import NavPostItem from './NavPostItem' + +/** + * 博客列表滚动分页 + * @param posts 所有文章 + * @param tags 所有标签 + * @returns {JSX.Element} + * @constructor + */ +const NavPostList = (props) => { + const { posts = [], currentSearch } = props + const filteredPosts = Object.assign(posts) + const router = useRouter() + let selectedSth = false + + // 处理是否选中 + filteredPosts.map((group) => { + let groupSelected = false + for (const post of group?.items) { + if (router.asPath.split('?')[0] === '/' + post.slug) { + groupSelected = true + selectedSth = true + } + } + group.selected = groupSelected + return null + }) + + // 如果都没有选中默认打开第一个 + if (!selectedSth && filteredPosts && filteredPosts.length > 0) { + filteredPosts[0].selected = true + } + + if (!filteredPosts || filteredPosts.length === 0) { + return + } else { + return
+ {/* 文章列表 */} + {filteredPosts?.map((group, index) => )} +
+ } +} + +export default NavPostList diff --git a/themes/gitbook/components/BlogPostListEmpty.js b/themes/gitbook/components/NavPostListEmpty.js similarity index 78% rename from themes/gitbook/components/BlogPostListEmpty.js rename to themes/gitbook/components/NavPostListEmpty.js index 86977fd0..207599db 100644 --- a/themes/gitbook/components/BlogPostListEmpty.js +++ b/themes/gitbook/components/NavPostListEmpty.js @@ -4,9 +4,9 @@ * @returns {JSX.Element} * @constructor */ -const BlogPostListEmpty = ({ currentSearch }) => { +const NavPostListEmpty = ({ currentSearch }) => { return

没有找到文章 {(currentSearch &&

{currentSearch}
)}

} -export default BlogPostListEmpty +export default NavPostListEmpty diff --git a/themes/gitbook/components/PageNavDrawer.js b/themes/gitbook/components/PageNavDrawer.js index 5fa6d114..dce89671 100644 --- a/themes/gitbook/components/PageNavDrawer.js +++ b/themes/gitbook/components/PageNavDrawer.js @@ -1,5 +1,5 @@ import { useGitBookGlobal } from '../LayoutBase' -import BlogPostListScroll from './BlogPostListScroll' +import NavPostList from './NavPostList' /** * 悬浮抽屉 页面内导航 @@ -22,7 +22,7 @@ const PageNavDrawer = ({ post, cRef }) => { ' overflow-y-hidden shadow-card w-72 duration-200 fixed left-1 top-16 rounded py-2 bg-white dark:bg-gray-600'}>
{/* 所有文章列表 */} - +
diff --git a/themes/gitbook/components/SearchInput.js b/themes/gitbook/components/SearchInput.js index b1f15a74..d35d2bc4 100644 --- a/themes/gitbook/components/SearchInput.js +++ b/themes/gitbook/components/SearchInput.js @@ -75,11 +75,11 @@ const SearchInput = ({ currentSearch, cRef, className }) => { lock = false } - return
+ return
{ defaultValue={currentSearch} /> -