From fc99d334db80fbf4f5cadc2433da0dc90e8606a7 Mon Sep 17 00:00:00 2001 From: anghunk Date: Thu, 22 Jan 2026 17:49:28 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E8=AF=84=E8=AE=BA=E7=BB=84=E4=BB=B6):=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=AE=9A=E4=B9=89CSS=E6=A0=B7?= =?UTF-8?q?=E5=BC=8F=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增_customStyleElement属性和_applyCustomCss方法,用于根据配置动态加载自定义CSS样式 --- docs/widget/src/core/CWDComments.js | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/docs/widget/src/core/CWDComments.js b/docs/widget/src/core/CWDComments.js index 35e0e92..2b05637 100644 --- a/docs/widget/src/core/CWDComments.js +++ b/docs/widget/src/core/CWDComments.js @@ -43,6 +43,7 @@ export class CWDComments { this.commentForm = null; this.commentList = null; this.formContainer = null; + this.customStyleElement = null; this.store = null; this.unsubscribe = null; this.likeState = { @@ -126,6 +127,7 @@ export class CWDComments { if (this.config.theme) { this.mountPoint.setAttribute('data-theme', this.config.theme); } + this._applyCustomCss(); } (async () => { @@ -549,6 +551,37 @@ export class CWDComments { this.store.loadComments(); } + this._applyCustomCss(); + } + + _applyCustomCss() { + if (!this.shadowRoot) { + return; + } + const rawUrl = + this.config && typeof this.config.customCssUrl === 'string' + ? this.config.customCssUrl + : ''; + const url = rawUrl.trim(); + if (!url) { + if (this.customStyleElement && this.customStyleElement.parentNode) { + this.customStyleElement.parentNode.removeChild(this.customStyleElement); + } + this.customStyleElement = null; + return; + } + if (!this.customStyleElement) { + const link = document.createElement('link'); + link.rel = 'stylesheet'; + link.href = url; + this.shadowRoot.appendChild(link); + this.customStyleElement = link; + return; + } + this.customStyleElement.href = url; + if (this.customStyleElement.parentNode !== this.shadowRoot) { + this.shadowRoot.appendChild(this.customStyleElement); + } } _initLikeButton(header) {