整理filterNavPages

This commit is contained in:
tangly1024
2023-07-30 11:44:18 +08:00
parent aca80a7750
commit 1353acf8c7
8 changed files with 78 additions and 49 deletions

View File

@@ -10,12 +10,24 @@ import NavPostItem from './NavPostItem'
* @constructor
*/
const NavPostList = (props) => {
const { filteredPostGroups } = props
const { filteredNavPages } = props
const router = useRouter()
let selectedSth = false
const groupedArray = filteredNavPages?.reduce((groups, item) => {
const categoryName = item?.category ? item?.category : '' // 将category转换为字符串
const lastGroup = groups[groups.length - 1] // 获取最后一个分组
if (!lastGroup || lastGroup?.category !== categoryName) { // 如果当前元素的category与上一个元素不同则创建新分组
groups.push({ category: categoryName, items: [] })
}
groups[groups.length - 1].items.push(item) // 将元素加入对应的分组
return groups
}, [])
// 处理是否选中
filteredPostGroups?.map((group) => {
groupedArray?.map((group) => {
let groupSelected = false
for (const post of group?.items) {
if (router.asPath.split('?')[0] === '/' + post.slug) {
@@ -28,16 +40,16 @@ const NavPostList = (props) => {
})
// 如果都没有选中默认打开第一个
if (!selectedSth && filteredPostGroups && filteredPostGroups?.length > 0) {
filteredPostGroups[0].selected = true
if (!selectedSth && groupedArray && groupedArray?.length > 0) {
groupedArray[0].selected = true
}
if (!filteredPostGroups || filteredPostGroups.length === 0) {
if (!groupedArray || groupedArray.length === 0) {
return <NavPostListEmpty />
} else {
return <div id='posts-wrapper' className='w-full flex-grow'>
{/* 文章列表 */}
{filteredPostGroups?.map((group, index) => <NavPostItem key={index} group={group} onHeightChange={props.onHeightChange}/>)}
{groupedArray?.map((group, index) => <NavPostItem key={index} group={group} onHeightChange={props.onHeightChange}/>)}
</div>
}
}

View File

@@ -10,7 +10,21 @@ import NavPostList from './NavPostList'
*/
const PageNavDrawer = (props) => {
const { pageNavVisible, changePageNavVisible } = useGitBookGlobal()
const { filteredPostGroups } = props
const { filteredNavPages } = props
const groupedArray = filteredNavPages.reduce((groups, item) => {
const categoryName = item?.category ? item?.category : '' // 将category转换为字符串
const lastGroup = groups[groups.length - 1] // 获取最后一个分组
if (!lastGroup || lastGroup?.category !== categoryName) { // 如果当前元素的category与上一个元素不同则创建新分组
groups.push({ category: categoryName, items: [] })
}
groups[groups.length - 1].items.push(item) // 将元素加入对应的分组
return groups
}, [])
const switchVisible = () => {
changePageNavVisible(!pageNavVisible)
}
@@ -23,7 +37,7 @@ const PageNavDrawer = (props) => {
' overflow-y-hidden shadow-card w-72 duration-200 fixed left-1 top-16 rounded py-2 bg-white dark:bg-gray-600'}>
<div className='dark:text-gray-400 text-gray-600 h-96 overflow-y-scroll p-3'>
{/* 所有文章列表 */}
<NavPostList filteredPostGroups={filteredPostGroups} />
<NavPostList groupedArray={groupedArray} />
</div>
</div>
</div>

View File

@@ -5,7 +5,7 @@ let lock = false
const SearchInput = ({ currentSearch, cRef, className }) => {
const searchInputRef = useRef()
const { setFilterPosts, allNavPages } = useGitBookGlobal()
const { setFilteredNavPages, allNavPages } = useGitBookGlobal()
useImperativeHandle(cRef, () => {
return {
@@ -17,31 +17,44 @@ const SearchInput = ({ currentSearch, cRef, className }) => {
const handleSearch = () => {
let keyword = searchInputRef.current.value
const filterPosts = []
if (keyword) {
keyword = keyword.trim()
} else {
setFilterPosts(allNavPages)
setFilteredNavPages(allNavPages)
}
const filterAllNavPages = deepClone(allNavPages)
for (const filterGroup of filterAllNavPages) {
for (let i = filterGroup.items.length - 1; i >= 0; i--) {
const post = filterGroup.items[i]
const articleInfo = post.title + ''
const hit = articleInfo.toLowerCase().indexOf(keyword.toLowerCase()) > -1
if (!hit) {
// 删除
filterGroup.items.splice(i, 1)
}
}
if (filterGroup.items && filterGroup.items.length > 0) {
filterPosts.push(filterGroup)
// for (const filterGroup of filterAllNavPages) {
// for (let i = filterGroup.items.length - 1; i >= 0; i--) {
// const post = filterGroup.items[i]
// const articleInfo = post.title + ''
// const hit = articleInfo.toLowerCase().indexOf(keyword.toLowerCase()) > -1
// if (!hit) {
// // 删除
// filterGroup.items.splice(i, 1)
// }
// }
// if (filterGroup.items && filterGroup.items.length > 0) {
// filterPosts.push(filterGroup)
// }
// }
for (let i = filterAllNavPages.length - 1; i >= 0; i--) {
const post = filterAllNavPages[i]
const articleInfo = post.title + ''
const hit = articleInfo.toLowerCase().indexOf(keyword.toLowerCase()) > -1
if (!hit) {
// 删除
filterAllNavPages.splice(i, 1)
}
}
// 更新完
setFilterPosts(filterPosts)
setFilteredNavPages(filterAllNavPages)
}
/**
* 回车键
* @param {*} e
*/
const handleKeyUp = (e) => {
if (e.keyCode === 13) { // 回车
handleSearch(searchInputRef.current.value)
@@ -49,6 +62,10 @@ const SearchInput = ({ currentSearch, cRef, className }) => {
cleanSearch()
}
}
/**
* 清理搜索
*/
const cleanSearch = () => {
searchInputRef.current.value = ''
handleSearch()