mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-16 15:09:28 +00:00
标题处理、标签页分页处理
This commit is contained in:
@@ -3,6 +3,7 @@ import Pagination from '@/components/Pagination'
|
||||
import BLOG from '@/blog.config'
|
||||
|
||||
import { useRouter } from 'next/router'
|
||||
import BlogPostListEmpty from '@/components/BlogPostListEmpty'
|
||||
|
||||
/**
|
||||
* 文章列表分页表格
|
||||
@@ -13,13 +14,6 @@ import { useRouter } from 'next/router'
|
||||
* @constructor
|
||||
*/
|
||||
const BlogPostList = ({ page = 1, posts = [], tags }) => {
|
||||
if (!posts) {
|
||||
return <div>
|
||||
<div className='grid 2xl:grid-cols-4 xl:grid-cols-4 lg:grid-cols-3 md:grid-cols-2 grid-cols-1 gap-3'>
|
||||
<p className='text-gray-500 dark:text-gray-300'>No posts found.</p>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
let filteredBlogPosts = posts
|
||||
|
||||
// 处理查询过滤 支持标签、关键词过滤
|
||||
@@ -46,31 +40,32 @@ const BlogPostList = ({ page = 1, posts = [], tags }) => {
|
||||
showNext = page * BLOG.postsPerPage < totalPosts
|
||||
}
|
||||
|
||||
return <main id='post-list-wrapper' className='pt-16 md:pt-28 px-2 md:px-20'>
|
||||
{(!page || page === 1) && (<div className='py-5' />)}
|
||||
if (!postsToShow || postsToShow.length === 0) {
|
||||
return <BlogPostListEmpty />
|
||||
} else {
|
||||
return <div id='post-list-wrapper' className='pt-16 md:pt-28 px-2 md:px-20'>
|
||||
{(!page || page === 1) && (<div className='py-5' />)}
|
||||
|
||||
{(page && page !== 1) && (
|
||||
<div className='pb-5'>
|
||||
<div className='dark:text-gray-200 flex justify-between py-1'>
|
||||
{page && page !== 1 && (<span>页 {page} / {totalPages}</span>)}
|
||||
{(page && page !== 1) && (
|
||||
<div className='pb-5'>
|
||||
<div className='dark:text-gray-200 flex justify-between py-1'>
|
||||
{page && page !== 1 && (<span>页 {page} / {totalPages}</span>)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
)}
|
||||
|
||||
<div className=''>
|
||||
{/* 文章列表 */}
|
||||
<div className='grid 2xl:grid-cols-4 xl:grid-cols-4 lg:grid-cols-3 md:grid-cols-2 grid-cols-1 gap-3'>
|
||||
{!postsToShow.length && (
|
||||
<p className='text-gray-500 dark:text-gray-300'>No posts found.</p>
|
||||
)}
|
||||
{postsToShow.map(post => (
|
||||
<BlogPost key={post.id} post={post} tags={tags} />
|
||||
))}
|
||||
</div>
|
||||
<div>
|
||||
{/* 文章列表 */}
|
||||
<div className='grid 2xl:grid-cols-4 xl:grid-cols-4 lg:grid-cols-3 md:grid-cols-2 grid-cols-1 gap-3'>
|
||||
{postsToShow.map(post => (
|
||||
<BlogPost key={post.id} post={post} tags={tags} />
|
||||
))}
|
||||
</div>
|
||||
|
||||
<Pagination page={page} showNext={showNext} />
|
||||
<Pagination page={page} showNext={showNext} />
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
}
|
||||
}
|
||||
|
||||
export default BlogPostList
|
||||
|
||||
13
components/BlogPostListEmpty.js
Normal file
13
components/BlogPostListEmpty.js
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* 空白博客 列表
|
||||
* @returns {JSX.Element}
|
||||
* @constructor
|
||||
*/
|
||||
const BlogPostListEmpty = () => {
|
||||
return <div className='w-full h-full flex justify-center mx-auto'>
|
||||
<div className='align-middle text-center my-auto'>
|
||||
<p className='text-gray-500 dark:text-gray-300'>No posts found.</p>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
export default BlogPostListEmpty
|
||||
@@ -1,10 +1,91 @@
|
||||
import BlogPost from '@/components/BlogPost'
|
||||
import Pagination from '@/components/Pagination'
|
||||
import BLOG from '@/blog.config'
|
||||
|
||||
import { useRouter } from 'next/router'
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
import throttle from 'lodash.throttle'
|
||||
import BlogPostListEmpty from '@/components/BlogPostListEmpty'
|
||||
|
||||
/**
|
||||
* 博客列表滚动分页
|
||||
* @param posts 所有文章
|
||||
* @param tags 所有标签
|
||||
* @param targetRef 指向父容器,用于计算下拉滚动的高度
|
||||
* @returns {JSX.Element}
|
||||
* @constructor
|
||||
*/
|
||||
const BlogPostListScrollPagination = ({ posts = [], tags, targetRef }) => {
|
||||
let filteredBlogPosts = posts
|
||||
|
||||
// 处理查询过滤 支持标签、关键词过滤
|
||||
let currentSearch = ''
|
||||
const router = useRouter()
|
||||
if (router.query && router.query.s) {
|
||||
currentSearch = router.query.s
|
||||
filteredBlogPosts = posts.filter(post => {
|
||||
const tagContent = post.tags ? post.tags.join(' ') : ''
|
||||
const searchContent = post.title + post.summary + tagContent + post.slug
|
||||
return searchContent.toLowerCase().includes(currentSearch.toLowerCase())
|
||||
})
|
||||
}
|
||||
|
||||
const [page, updatePage] = useState(1)
|
||||
const initPosts = getPostByPage(page, filteredBlogPosts, BLOG.postsPerPage)
|
||||
const [postsToShow, updatePostToShow] = useState(useRef(initPosts).current)
|
||||
|
||||
let hasMore = false
|
||||
if (filteredBlogPosts) {
|
||||
const totalPosts = filteredBlogPosts.length
|
||||
hasMore = page * BLOG.postsPerPage < totalPosts
|
||||
}
|
||||
const handleGetMore = function () {
|
||||
if (!hasMore) return
|
||||
updatePage(page + 1)
|
||||
updatePostToShow(postsToShow.concat(getPostByPage(page + 1, filteredBlogPosts, BLOG.postsPerPage)))
|
||||
}
|
||||
|
||||
// 监听滚动自动分页加载
|
||||
const scrollTrigger = useCallback(throttle(() => {
|
||||
const scrollS = window.scrollY + window.outerHeight
|
||||
const clientHeight = targetRef ? (targetRef.current ? (targetRef.current.clientHeight) : 0) : 0
|
||||
if (scrollS > clientHeight + 10) {
|
||||
handleGetMore()
|
||||
}
|
||||
}, 500))
|
||||
|
||||
// 监听滚动
|
||||
useEffect(() => {
|
||||
window.addEventListener('scroll', scrollTrigger)
|
||||
return () => {
|
||||
window.removeEventListener('scroll', scrollTrigger)
|
||||
}
|
||||
})
|
||||
|
||||
if (!postsToShow || postsToShow.length === 0) {
|
||||
return <BlogPostListEmpty />
|
||||
} else {
|
||||
return <div id='post-list-wrapper' className='pt-16 md:pt-28 px-2 md:px-20'>
|
||||
<div>
|
||||
{/* 文章列表 */}
|
||||
<div className='grid 2xl:grid-cols-4 xl:grid-cols-4 lg:grid-cols-3 md:grid-cols-2 grid-cols-1 gap-3'>
|
||||
{postsToShow.map(post => (
|
||||
<BlogPost key={post.id} post={post} tags={tags} />
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className='flex'>
|
||||
{hasMore
|
||||
? (<div className='w-full my-4 py-4 bg-gray-200 text-center cursor-pointer'
|
||||
onClick={handleGetMore}> 加载更多 </div>)
|
||||
: (
|
||||
<div className='w-full my-4 py-4 bg-gray-200 text-center'> 加载完了😰 </div>
|
||||
)}
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定页码的文章
|
||||
@@ -19,98 +100,4 @@ const getPostByPage = function (page, totalPosts, postsPerPage) {
|
||||
postsPerPage * page
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 博客列表滚动分页
|
||||
* @param posts 所有文章
|
||||
* @param tags 所有标签
|
||||
* @returns {JSX.Element}
|
||||
* @constructor
|
||||
*/
|
||||
const BlogPostListScrollPagination = ({ posts = [], tags, targetRef }) => {
|
||||
if (!posts) {
|
||||
return <div>
|
||||
<div className='grid 2xl:grid-cols-4 xl:grid-cols-4 lg:grid-cols-3 md:grid-cols-2 grid-cols-1 gap-3'>
|
||||
<p className='text-gray-500 dark:text-gray-300'>No posts found.</p>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
let filteredBlogPosts = posts
|
||||
|
||||
// 处理查询过滤 支持标签、关键词过滤
|
||||
let currentSearch = ''
|
||||
const router = useRouter()
|
||||
if (router.query && router.query.s) {
|
||||
currentSearch = router.query.s
|
||||
filteredBlogPosts = posts.filter(post => {
|
||||
const tagContent = post.tags ? post.tags.join(' ') : ''
|
||||
const searchContent = post.title + post.summary + tagContent + post.slug
|
||||
return searchContent.toLowerCase().includes(currentSearch.toLowerCase())
|
||||
})
|
||||
}
|
||||
const [page, updatePage] = useState(1)
|
||||
const [postsToShow, updatePostToShow] = useState(getPostByPage(page, filteredBlogPosts, BLOG.postsPerPage))
|
||||
|
||||
let showNext = false
|
||||
if (filteredBlogPosts) {
|
||||
const totalPosts = filteredBlogPosts.length
|
||||
showNext = page * BLOG.postsPerPage < totalPosts
|
||||
}
|
||||
const [loading, updateLoading] = useState(false)
|
||||
const handleGetMore = function () {
|
||||
if (!showNext) {
|
||||
// 完了
|
||||
return
|
||||
}
|
||||
if (loading) {
|
||||
// 加载中
|
||||
return
|
||||
}
|
||||
updateLoading(true)
|
||||
updatePage(page + 1)
|
||||
updatePostToShow(postsToShow.concat(getPostByPage(page + 1, filteredBlogPosts, BLOG.postsPerPage)))
|
||||
updateLoading(false)
|
||||
}
|
||||
|
||||
// 监听滚动自动分页加载
|
||||
const scrollTrigger = useCallback(throttle(() => {
|
||||
const scrollS = window.scrollY + window.outerHeight
|
||||
const clientHeight = targetRef ? (targetRef.current.clientHeight) : 0
|
||||
if (scrollS > clientHeight + 10) {
|
||||
handleGetMore()
|
||||
}
|
||||
}, 500))
|
||||
|
||||
// 监听滚动
|
||||
useEffect(() => {
|
||||
window.addEventListener('scroll', scrollTrigger)
|
||||
return () => {
|
||||
window.removeEventListener('scroll', scrollTrigger)
|
||||
}
|
||||
})
|
||||
|
||||
return <main id='post-list-wrapper' className='pt-16 md:pt-28 px-2 md:px-20'>
|
||||
<div className=''>
|
||||
{/* 文章列表 */}
|
||||
<div className='grid 2xl:grid-cols-4 xl:grid-cols-4 lg:grid-cols-3 md:grid-cols-2 grid-cols-1 gap-3'>
|
||||
{!postsToShow.length && (
|
||||
<p className='text-gray-500 dark:text-gray-300'>No posts found.</p>
|
||||
)}
|
||||
{postsToShow.map(post => (
|
||||
<BlogPost key={post.id} post={post} tags={tags} />
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className='flex'>
|
||||
{showNext
|
||||
? (<div className='w-full my-4 py-4 bg-gray-200 text-center cursor-pointer' onClick={ handleGetMore}> 加载更多 </div>)
|
||||
: (
|
||||
<div className='w-full my-4 py-4 bg-gray-200 text-center' > 加载完了😰 </div>
|
||||
)}
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
}
|
||||
|
||||
export default BlogPostListScrollPagination
|
||||
@@ -1,6 +1,5 @@
|
||||
import BLOG from '@/blog.config'
|
||||
import Head from 'next/head'
|
||||
import ThirdPartyScript from '@/components/ThirdPartyScript'
|
||||
|
||||
const CommonHead = ({ meta }) => {
|
||||
const url = BLOG.path.length ? `${BLOG.link}/${BLOG.path}` : BLOG.link
|
||||
|
||||
@@ -33,20 +33,25 @@ const JumpToTop = ({ targetRef, showPercent = true }) => {
|
||||
}, [show])
|
||||
|
||||
return (
|
||||
<div
|
||||
className={(show ? 'animate__fadeInUp' : 'animate__fadeOutUp') + ' rounded-full animate__animated animate__faster shadow-xl'}>
|
||||
<div
|
||||
style={{ backgroundColor: 'rgb(56, 144, 255)' }}
|
||||
className='rounded-full dark:bg-gray-600 bg-white cursor-pointer '
|
||||
onClick={() => window.scrollTo({ top: 0, behavior: 'smooth' })}>
|
||||
{showPercent && (
|
||||
<div style={{ backgroundColor: 'rgb(56, 144, 255)' }} className='text-gray-100 absolute rounded-full dark:text-gray-200 dark:bg-gray-600 z-20 hover:opacity-0 w-11 py-3 text-center'>
|
||||
<span>{percent}%</span>
|
||||
</div>
|
||||
)}
|
||||
<div className='text-2xl'>
|
||||
<a className='text-gray-100 fa fa-arrow-up p-3 transform hover:scale-125 duration-200 '
|
||||
title={locale.POST.TOP} />
|
||||
<div className='right-0 space-x-2 fixed flex bottom-24 px-5 py-1 duration-500'>
|
||||
<div className='flex-wrap'>
|
||||
<div
|
||||
className={(show ? 'animate__fadeInUp' : 'animate__fadeOutUp') + ' rounded-full animate__animated animate__faster shadow-xl'}>
|
||||
<div
|
||||
style={{ backgroundColor: 'rgb(56, 144, 255)' }}
|
||||
className='rounded-full dark:bg-gray-600 bg-white cursor-pointer '
|
||||
onClick={() => window.scrollTo({ top: 0, behavior: 'smooth' })}>
|
||||
{showPercent && (
|
||||
<div style={{ backgroundColor: 'rgb(56, 144, 255)' }}
|
||||
className='text-gray-100 absolute rounded-full dark:text-gray-200 dark:bg-gray-600 z-20 hover:opacity-0 w-11 py-3 text-center'>
|
||||
<span>{percent}%</span>
|
||||
</div>
|
||||
)}
|
||||
<div className='text-2xl'>
|
||||
<a className='text-gray-100 fa fa-arrow-up p-3 transform hover:scale-125 duration-200 '
|
||||
title={locale.POST.TOP} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user