@@ -7,12 +7,21 @@ import { cn } from 'astro-pure/utils'
77
88import 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 >
0 commit comments