Skip to content

Commit

Permalink
feat: 文档底部上/下一页切换
Browse files Browse the repository at this point in the history
  • Loading branch information
c0dedance committed Oct 24, 2023
1 parent b907d9a commit b7b70c2
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 1 deletion.
5 changes: 5 additions & 0 deletions e2e/playground/basic/docs/guide/configure-site.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function ConfigureSite() {
return (
<h2>configure-site</h2>
)
}
6 changes: 5 additions & 1 deletion e2e/playground/basic/docs/rpress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ export default defineConfig({
link: '/guide/start'
},
{
text: '配置站点',
text: '用法',
link: '/guide/usage'
},
{
text: '配置站点',
link: '/guide/configure-site'
}
]
}
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/theme-default/Layout/DocLayout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useLocation } from 'react-router-dom'
import { Content, usePageData } from '../../../'
import { Sidebar } from '../../components/Sidebar/'
import { DocFooter } from '../../components/DocFooter'
import styles from './index.module.scss'

export function DocLayout() {
Expand All @@ -22,6 +23,7 @@ export function DocLayout() {
<div className="island-doc">
<Content />
</div>
<DocFooter />
</div>
</div>
</div>
Expand Down
44 changes: 44 additions & 0 deletions src/runtime/theme-default/components/DocFooter/index.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.prev,
.next {
width: 50%;
}

.pager-link {
display: block;
border: 1px solid var(--island-c-divider-light);
border-radius: 8px;
padding: 8px 16px 8px;
width: 100%;
height: 100%;
transition: border-color 0.25s;
}

.pager-link:hover {
border-color: var(--island-c-brand);
}

.pager-link:hover .title {
color: var(--island-c-brand-dark);
}

.pager-link.next {
margin-left: auto;
text-align: right;
}

.desc {
display: block;
line-height: 20px;
font-size: 12px;
font-weight: 500;
color: var(--island-c-text-2);
}

.title {
display: block;
line-height: 20px;
font-size: 14px;
font-weight: 500;
color: var(--island-c-brand);
transition: color 0.25s;
}
31 changes: 31 additions & 0 deletions src/runtime/theme-default/components/DocFooter/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { usePrevNextPage } from '../../utils/usePrevNextPage'
import styles from './index.module.scss'

export function DocFooter() {
const { prevPage, nextPage } = usePrevNextPage()
return (
<footer mt="8">
<div flex="~" gap="2" divider-top="~" pt="6">
<div flex="~ col" className={styles.prev}>
{prevPage && (
<a href={prevPage.link} className={styles.pagerLink}>
<span className={styles.desc}>上一页</span>
<span className={styles.title}>{prevPage.text}</span>
</a>
)}
</div>
<div flex="~ col" className={styles.next}>
{nextPage && (
<a
href={nextPage.link}
className={`${styles.pagerLink} ${styles.next}`}
>
<span className={styles.desc}>下一页</span>
<span className={styles.title}>{nextPage.text}</span>
</a>
)}
</div>
</div>
</footer>
)
}
34 changes: 34 additions & 0 deletions src/runtime/theme-default/utils/usePrevNextPage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useMemo } from 'react'
import { useLocation } from 'react-router-dom'
import { usePageData } from '../../'
import type { SidebarItem } from 'shared/types'

export function usePrevNextPage() {
const { pathname } = useLocation()
const { siteData } = usePageData()
const sidebar = siteData.themeConfig?.sidebar || {}

const flattenTitles = useMemo(() => {
const res: SidebarItem[] = []
// 遍历 Sidebar 数据,收集所有的文章信息,并平铺到一个数组里面
Object.keys(sidebar).forEach((key) => {
const groups = sidebar[key] || []
groups.forEach((group) => {
group.items.forEach((item) => {
res.push(item)
})
})
})
return res
}, [sidebar])

const pageIndex = flattenTitles.findIndex((item) => item.link === pathname)

const prevPage = flattenTitles[pageIndex - 1] || null
const nextPage = flattenTitles[pageIndex + 1] || null

return {
prevPage,
nextPage,
}
}

0 comments on commit b7b70c2

Please sign in to comment.