From 1a6a93b8c5619922d7cff993803503bedf9f8bac Mon Sep 17 00:00:00 2001 From: anghunk Date: Wed, 28 Jan 2026 13:58:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(widget):=20=E4=BF=AE=E5=A4=8D=E5=88=86?= =?UTF-8?q?=E9=A1=B5=E5=88=87=E6=8D=A2=E6=97=B6=E6=BB=9A=E5=8A=A8=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=E6=9C=AA=E9=87=8D=E7=BD=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改 store 订阅回调,增加 prevState 参数以检测页面变化。当检测到分页页码变更时,自动平滑滚动至评论区域顶部,提升用户体验。 --- docs/widget/src/core/CWDComments.js | 30 +++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/docs/widget/src/core/CWDComments.js b/docs/widget/src/core/CWDComments.js index 0cfe2cc..e2be762 100644 --- a/docs/widget/src/core/CWDComments.js +++ b/docs/widget/src/core/CWDComments.js @@ -179,8 +179,8 @@ export class CWDComments { typeof api.likeComment === 'function' ? api.likeComment.bind(api) : undefined, ); - this.unsubscribe = this.store.store.subscribe((state) => { - this._onStateChange(state); + this.unsubscribe = this.store.store.subscribe((state, prevState) => { + this._onStateChange(state, prevState); }); this._render(); @@ -496,6 +496,17 @@ export class CWDComments { currentUser: state.form, }); } + + const pageChanged = + prevState && + prevState.pagination && + state && + state.pagination && + state.pagination.page !== prevState.pagination.page; + + if (pageChanged) { + this._scrollToCommentsTop(); + } } /** @@ -553,8 +564,8 @@ export class CWDComments { this.store = createCommentStore(this.config, api.fetchComments.bind(api), api.submitComment.bind(api)); - this.unsubscribe = this.store.store.subscribe((state) => { - this._onStateChange(state); + this.unsubscribe = this.store.store.subscribe((state, prevState) => { + this._onStateChange(state, prevState); }); this.store.loadComments(); @@ -562,6 +573,17 @@ export class CWDComments { this._applyCustomCss(); } + _scrollToCommentsTop() { + if (typeof window === 'undefined') { + return; + } + const target = this.hostElement || this.mountPoint; + if (!target || typeof target.scrollIntoView !== 'function') { + return; + } + target.scrollIntoView({ behavior: 'smooth', block: 'start' }); + } + _applyCustomCss() { if (!this.shadowRoot) { return;