Skip to content

Commit 8287a6a

Browse files
committed
2 parents f2d792d + 023fe9e commit 8287a6a

File tree

21 files changed

+257
-163
lines changed

21 files changed

+257
-163
lines changed

.env.local

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# 环境变量 @see https://www.nextjs.cn/docs/basic-features/environment-variables
2-
NEXT_PUBLIC_VERSION=4.4.4
2+
NEXT_PUBLIC_VERSION=4.4.5
33

44

55
# 可在此添加环境变量,去掉最左边的(# )注释即可

components/Comment.js

+112-48
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import dynamic from 'next/dynamic'
21
import Tabs from '@/components/Tabs'
2+
import { siteConfig } from '@/lib/config'
33
import { isBrowser, isSearchEngineBot } from '@/lib/utils'
4+
import dynamic from 'next/dynamic'
45
import { useRouter } from 'next/router'
6+
import { useEffect, useRef, useState } from 'react'
57
import Artalk from './Artalk'
6-
import { siteConfig } from '@/lib/config'
78

89
const WalineComponent = dynamic(
910
() => {
@@ -57,11 +58,14 @@ const ValineComponent = dynamic(() => import('@/components/ValineComponent'), {
5758

5859
/**
5960
* 评论组件
61+
* 只有当前组件在浏览器可见范围内才会加载内容
6062
* @param {*} param0
6163
* @returns
6264
*/
63-
const Comment = ({ siteInfo, frontMatter, className }) => {
65+
const Comment = ({ frontMatter, className }) => {
6466
const router = useRouter()
67+
const [shouldLoad, setShouldLoad] = useState(false)
68+
const commentRef = useRef(null)
6569

6670
const COMMENT_ARTALK_SERVER = siteConfig('COMMENT_ARTALK_SERVER')
6771
const COMMENT_TWIKOO_ENV_ID = siteConfig('COMMENT_TWIKOO_ENV_ID')
@@ -73,65 +77,125 @@ const Comment = ({ siteInfo, frontMatter, className }) => {
7377
const COMMENT_GITALK_CLIENT_ID = siteConfig('COMMENT_GITALK_CLIENT_ID')
7478
const COMMENT_WEBMENTION_ENABLE = siteConfig('COMMENT_WEBMENTION_ENABLE')
7579

76-
if (isSearchEngineBot()) {
77-
return null
78-
}
80+
useEffect(() => {
81+
// Check if the component is visible in the viewport
82+
const observer = new IntersectionObserver(entries => {
83+
entries.forEach(entry => {
84+
if (entry.isIntersecting) {
85+
setShouldLoad(true)
86+
observer.unobserve(entry.target)
87+
}
88+
})
89+
})
90+
91+
if (commentRef.current) {
92+
observer.observe(commentRef.current)
93+
}
94+
95+
return () => {
96+
if (commentRef.current) {
97+
observer.unobserve(commentRef.current)
98+
}
99+
}
100+
}, [frontMatter])
79101

80102
// 当连接中有特殊参数时跳转到评论区
81-
if (isBrowser && ('giscus' in router.query || router.query.target === 'comment')) {
103+
if (
104+
isBrowser &&
105+
('giscus' in router.query || router.query.target === 'comment')
106+
) {
82107
setTimeout(() => {
83108
const url = router.asPath.replace('?target=comment', '')
84109
history.replaceState({}, '', url)
85-
document?.getElementById('comment')?.scrollIntoView({ block: 'start', behavior: 'smooth' })
110+
document
111+
?.getElementById('comment')
112+
?.scrollIntoView({ block: 'start', behavior: 'smooth' })
86113
}, 1000)
87114
}
88115

89116
if (!frontMatter) {
90117
return <>Loading...</>
91118
}
92119

120+
if (isSearchEngineBot()) {
121+
return null
122+
}
123+
93124
return (
94-
<div key={frontMatter?.id} id='comment' className={`comment mt-5 text-gray-800 dark:text-gray-300 ${className || ''}`}>
95-
<Tabs>
96-
{COMMENT_ARTALK_SERVER && (<div key='Artalk'>
97-
<Artalk />
98-
</div>)}
99-
100-
{COMMENT_TWIKOO_ENV_ID && (<div key='Twikoo'>
101-
<TwikooCompenent />
102-
</div>)}
103-
104-
{COMMENT_WALINE_SERVER_URL && (<div key='Waline'>
105-
<WalineComponent />
106-
</div>)}
107-
108-
{COMMENT_VALINE_APP_ID && (<div key='Valine' name='reply'>
109-
<ValineComponent path={frontMatter.id} />
110-
</div>)}
111-
112-
{COMMENT_GISCUS_REPO && (
113-
<div key="Giscus">
114-
<GiscusComponent className="px-2" />
115-
</div>
116-
)}
117-
118-
{COMMENT_CUSDIS_APP_ID && (<div key='Cusdis'>
119-
<CusdisComponent frontMatter={frontMatter} />
120-
</div>)}
121-
122-
{COMMENT_UTTERRANCES_REPO && (<div key='Utterance'>
123-
<UtterancesComponent issueTerm={frontMatter.id} className='px-2' />
124-
</div>)}
125-
126-
{COMMENT_GITALK_CLIENT_ID && (<div key='GitTalk'>
127-
<GitalkComponent frontMatter={frontMatter} />
128-
</div>)}
129-
130-
{COMMENT_WEBMENTION_ENABLE && (<div key='WebMention'>
131-
<WebMentionComponent frontMatter={frontMatter} className="px-2" />
132-
</div>)}
133-
</Tabs>
125+
<div
126+
key={frontMatter?.id}
127+
id='comment'
128+
ref={commentRef}
129+
className={`comment mt-5 text-gray-800 dark:text-gray-300 ${className || ''}`}>
130+
{/* 延迟加载评论区 */}
131+
{!shouldLoad && (
132+
<div className='text-center'>
133+
Loading...
134+
<i className='fas fa-spinner animate-spin text-3xl ' />
134135
</div>
136+
)}
137+
138+
{shouldLoad && (
139+
<Tabs>
140+
{COMMENT_ARTALK_SERVER && (
141+
<div key='Artalk'>
142+
<Artalk />
143+
</div>
144+
)}
145+
146+
{COMMENT_TWIKOO_ENV_ID && (
147+
<div key='Twikoo'>
148+
<TwikooCompenent />
149+
</div>
150+
)}
151+
152+
{COMMENT_WALINE_SERVER_URL && (
153+
<div key='Waline'>
154+
<WalineComponent />
155+
</div>
156+
)}
157+
158+
{COMMENT_VALINE_APP_ID && (
159+
<div key='Valine' name='reply'>
160+
<ValineComponent path={frontMatter.id} />
161+
</div>
162+
)}
163+
164+
{COMMENT_GISCUS_REPO && (
165+
<div key='Giscus'>
166+
<GiscusComponent className='px-2' />
167+
</div>
168+
)}
169+
170+
{COMMENT_CUSDIS_APP_ID && (
171+
<div key='Cusdis'>
172+
<CusdisComponent frontMatter={frontMatter} />
173+
</div>
174+
)}
175+
176+
{COMMENT_UTTERRANCES_REPO && (
177+
<div key='Utterance'>
178+
<UtterancesComponent
179+
issueTerm={frontMatter.id}
180+
className='px-2'
181+
/>
182+
</div>
183+
)}
184+
185+
{COMMENT_GITALK_CLIENT_ID && (
186+
<div key='GitTalk'>
187+
<GitalkComponent frontMatter={frontMatter} />
188+
</div>
189+
)}
190+
191+
{COMMENT_WEBMENTION_ENABLE && (
192+
<div key='WebMention'>
193+
<WebMentionComponent frontMatter={frontMatter} className='px-2' />
194+
</div>
195+
)}
196+
</Tabs>
197+
)}
198+
</div>
135199
)
136200
}
137201

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "notion-next",
3-
"version": "4.4.4",
3+
"version": "4.4.5",
44
"homepage": "https://github.com/tangly1024/NotionNext.git",
55
"license": "MIT",
66
"repository": {

themes/commerce/components/Catalog.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const Catalog = ({ toc }) => {
7979
notion-table-of-contents-item-indent-level-${tocItem.indentLevel} `}
8080
>
8181
<span style={{ display: 'inline-block', marginLeft: tocItem.indentLevel * 16 }}
82-
className={`${activeSection === id && ' font-bold text-red-600'}`}
82+
className={`truncate ${activeSection === id ? 'font-bold text-red-600': ''}`}
8383
>
8484
{tocItem.text}
8585
</span>

themes/fukasawa/components/Catalog.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const Catalog = ({ toc }) => {
7171
notion-table-of-contents-item-indent-level-${tocItem.indentLevel} `}
7272
>
7373
<span style={{ display: 'inline-block', marginLeft: tocItem.indentLevel * 16 }}
74-
className={`${activeSection === id && ' font-bold text-red-400 underline'}`}
74+
className={`truncate ${activeSection === id ? 'font-bold text-red-400 underline' : ''}`}
7575
title={tocItem.text}
7676
>
7777
{tocItem.text}

themes/gitbook/components/BlogPostListPage.js

-34
This file was deleted.

themes/gitbook/components/Catalog.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const Catalog = ({ post }) => {
7474
notion-table-of-contents-item-indent-level-${tocItem.indentLevel} `}
7575
>
7676
<span style={{ display: 'inline-block', marginLeft: tocItem.indentLevel * 16 }}
77-
className={`${activeSection === id && ' font-bold text-gray-500 underline'}`}
77+
className={`truncate ${activeSection === id ? 'font-bold text-gray-500 underline' : ''}`}
7878
>
7979
{tocItem.text}
8080
</span>

themes/gitbook/components/NavPostItem.js

+40-24
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import BlogPostCard from './BlogPostCard'
2-
import { useState } from 'react'
3-
import Collapse from '@/components/Collapse'
41
import Badge from '@/components/Badge'
2+
import Collapse from '@/components/Collapse'
53
import { siteConfig } from '@/lib/config'
64
import CONFIG from '../config'
5+
import BlogPostCard from './BlogPostCard'
76

87
/**
98
* 导航列表
@@ -12,36 +11,53 @@ import CONFIG from '../config'
1211
* @returns {JSX.Element}
1312
* @constructor
1413
*/
15-
const NavPostItem = (props) => {
16-
const { group } = props
17-
const [isOpen, changeIsOpen] = useState(group?.selected)
14+
const NavPostItem = props => {
15+
const { group, expanded, toggleItem } = props // 接收传递的展开状态和切换函数
16+
// const [isOpen, setIsOpen] = useState(expanded) // 使用展开状态作为组件内部状态
1817

18+
// 当展开状态改变时触发切换函数,并根据传入的展开状态更新内部状态
1919
const toggleOpenSubMenu = () => {
20-
changeIsOpen(!isOpen)
20+
toggleItem() // 调用父组件传递的切换函数
21+
// setIsOpen(!expanded) // 更新内部状态为传入的展开状态的相反值
2122
}
2223

2324
const groupHasLatest = group?.items?.some(post => post.isLatest)
2425

2526
if (group?.category) {
26-
return <>
27-
<div onClick={toggleOpenSubMenu}
28-
className='select-none relative flex justify-between text-sm cursor-pointer p-2 hover:bg-gray-50 rounded-md dark:hover:bg-gray-600' key={group?.category}>
29-
<span>{group?.category}</span>
30-
<div className='inline-flex items-center select-none pointer-events-none '><i className={`px-2 fas fa-chevron-left transition-all opacity-50 duration-200 ${isOpen ? '-rotate-90' : ''}`}></i></div>
31-
{groupHasLatest && siteConfig('GITBOOK_LATEST_POST_RED_BADGE', false, CONFIG) && !isOpen && <Badge/>}
27+
return (
28+
<>
29+
<div
30+
onClick={toggleOpenSubMenu}
31+
className='select-none relative flex justify-between text-sm cursor-pointer p-2 hover:bg-gray-50 rounded-md dark:hover:bg-gray-600'
32+
key={group?.category}>
33+
<span>{group?.category}</span>
34+
<div className='inline-flex items-center select-none pointer-events-none '>
35+
<i
36+
className={`px-2 fas fa-chevron-left transition-all opacity-50 duration-200 ${expanded ? '-rotate-90' : ''}`}></i>
37+
</div>
38+
{groupHasLatest &&
39+
siteConfig('GITBOOK_LATEST_POST_RED_BADGE', false, CONFIG) &&
40+
!expanded && <Badge />}
41+
</div>
42+
<Collapse isOpen={expanded} onHeightChange={props.onHeightChange}>
43+
{group?.items?.map(post => (
44+
<div key={post.id} className='ml-3 border-l'>
45+
<BlogPostCard className='text-sm ml-3' post={post} />
3246
</div>
33-
<Collapse isOpen={isOpen} onHeightChange={props.onHeightChange}>
34-
{group?.items?.map(post => (<div key={post.id} className='ml-3 border-l'>
35-
<BlogPostCard className='text-sm ml-3' post={post} /></div>))
36-
}
37-
</Collapse>
38-
</>
47+
))}
48+
</Collapse>
49+
</>
50+
)
3951
} else {
40-
return <>
41-
{group?.items?.map(post => (<div key={post.id} >
42-
<BlogPostCard className='text-sm py-2' post={post} /></div>))
43-
}
44-
</>
52+
return (
53+
<>
54+
{group?.items?.map(post => (
55+
<div key={post.id}>
56+
<BlogPostCard className='text-sm py-2' post={post} />
57+
</div>
58+
))}
59+
</>
60+
)
4561
}
4662
}
4763

0 commit comments

Comments
 (0)