Skip to content

Commit 4b01119

Browse files
committed
优化 waline 评论组件,支持单页面配置是否启用评论
1 parent 00341d2 commit 4b01119

3 files changed

Lines changed: 26 additions & 6 deletions

File tree

src/components/waline/Comment.astro

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@ import { cn } from 'astro-pure/utils'
77
88
import commentFlag from '@/assets/other/comment_flag.png'
99
10-
const { class: className } = Astro.props
10+
interface Props {
11+
class?: string
12+
/** 是否启用评论,优先级高于全局配置 */
13+
enable?: boolean
14+
}
15+
16+
const { class: className, enable } = Astro.props
17+
18+
// 优先使用传入的 enable,如果没有则使用全局配置
19+
const isEnabled = enable !== undefined ? enable : config.integ.waline.enable
1120
---
1221

1322
{
14-
config.integ.waline.enable && (
15-
<comment-component>
23+
isEnabled && (
24+
<comment-component data-enabled={isEnabled}>
1625
<div id='waline' class={cn('not-prose', className)} style={`--comment-flag-url: url(${commentFlag.src})`}>
1726
Comment seems to stuck. Try to refresh?✨
1827
</div>
@@ -33,6 +42,16 @@ const { class: className } = Astro.props
3342
}
3443

3544
connectedCallback() {
45+
// 组件已经渲染说明服务端已经判断为启用,直接初始化
46+
// 这里保留检查以防御性编程
47+
const componentEnabled = this.dataset.enabled !== undefined
48+
? this.dataset.enabled === 'true'
49+
: undefined
50+
const isEnabled = componentEnabled !== undefined ? componentEnabled : walineConfig.enable
51+
52+
// 如果禁用了,则不初始化
53+
if (!isEnabled) return
54+
3655
// Prevent Vue log errors
3756
;(globalThis as unknown as { __VUE_OPTIONS_API__: boolean }).__VUE_OPTIONS_API__ = true
3857
;(globalThis as unknown as { __VUE_PROD_DEVTOOLS__: boolean }).__VUE_PROD_DEVTOOLS__ = false
@@ -54,7 +73,8 @@ const { class: className } = Astro.props
5473
}
5574
}
5675

57-
if (walineConfig.enable) customElements.define('comment-component', Comment)
76+
// 始终注册组件,由组件内部判断是否启用
77+
customElements.define('comment-component', Comment)
5878
</script>
5979

6080
<style>

src/pages/about/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ Current support tools as follow:
217217
- Site hosting: [EdgeOne Pages(Tencent)](https://edgeone.cloud.tencent.com)
218218
- CI/CD: [EdgeOne](https://edgeone.cloud.tencent.com)
219219
- Resources speedup: [EdgeOne](https://edgeone.cloud.tencent.com)
220-
- Comment system: [Waline](https://waline.js.org)
220+
- Comment system: [Waline](https://waline.js.org), 欢迎来[[msgboard|留言板]]留言~
221221
- TotalPV: [Vercount](https://github.com/EvanNotFound/vercount)
222222
- PagePV & Comment: [Waline](https://waline.js.org/next/guide/features/pageview.html)
223223
- Icons & OgImage: from [Iconify](https://iconify.design/) & [lobehub](https://lobehub.com/icons), some icons from ai

src/pages/msgboard/index.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const meta = {
2525
<div id='content' class='animate mt-8'>
2626
<DanmakuComments />
2727
<div class="mt-8">
28-
<Comment />
28+
<Comment enable={true}/>
2929
</div>
3030
</div>
3131
</main>

0 commit comments

Comments
 (0)