From ae6d80c9cc53b0d27587f9bf606e840264d40f2e Mon Sep 17 00:00:00 2001 From: xfy Date: Tue, 16 Jun 2026 15:58:50 +0800 Subject: [PATCH] =?UTF-8?q?fix(editor):=20=E4=BF=AE=E5=A4=8D=E6=BB=9A?= =?UTF-8?q?=E5=8A=A8=E6=AF=94=E4=BE=8B=E5=90=8C=E6=AD=A5=E5=A4=B1=E6=95=88?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E5=88=87=E6=8D=A2=E5=88=B0=E5=BA=95=E9=83=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原 syncScrollRatio 封装在 display:'none' 之后重复读取源容器 scrollTop,此时浏览器已将其归零,导致比例计算为 0。改为提前 读取比例并直接应用,移除二次封装与调试日志。 --- libs/tiptap-editor/src/index.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/libs/tiptap-editor/src/index.ts b/libs/tiptap-editor/src/index.ts index 3a7f5d5..b5949f0 100644 --- a/libs/tiptap-editor/src/index.ts +++ b/libs/tiptap-editor/src/index.ts @@ -161,10 +161,11 @@ class TiptapEditorInstance { if (!this.isSourceMode) { // 富文本 → 源码:导出当前 Markdown 到 textarea this.sourceTextarea.value = this.editor.getMarkdown() + // 必须在 display:'none' 之前读取滚动比例——隐藏后 scrollTop 会被浏览器归零 + const pmRatio = this.getScrollRatio(proseMirrorDom) proseMirrorDom.style.display = 'none' this.sourceTextarea.hidden = false - // 按滚动比例同步到源码视图,保持视觉位置一致 - this.syncScrollRatio(proseMirrorDom, this.sourceTextarea) + this.applyScrollRatio(this.sourceTextarea, pmRatio) this.sourceTextarea.focus() this.toggleButton.textContent = '✎' this.toggleButton.title = '切换富文本' @@ -207,13 +208,6 @@ class TiptapEditorInstance { el.scrollTop = max * ratio } - /** - * 便捷封装:从源容器读取比例并应用到目标容器。 - */ - private syncScrollRatio(from: HTMLElement, to: HTMLElement): void { - this.applyScrollRatio(to, this.getScrollRatio(from)) - } - setMarkdown(content: string): void { this.editor?.commands.setContent(content, { emitUpdate: false, contentType: 'markdown' }) }