From 6ce8915e6c5ac220804a09e97156ccccc03ed1e4 Mon Sep 17 00:00:00 2001 From: xfy Date: Tue, 16 Jun 2026 16:13:46 +0800 Subject: [PATCH] =?UTF-8?q?fix(editor):=20=E4=BF=AE=E5=A4=8D=20focus()=20?= =?UTF-8?q?=E6=8A=8A=E6=BA=90=E7=A0=81=E8=A7=86=E5=9B=BE=E6=BB=9A=E5=8A=A8?= =?UTF-8?q?=E5=88=B0=E5=BA=95=E9=83=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 切换到源码模式时 sourceTextarea.focus() 会触发浏览器将光标 所在行滚动进可视区域,光标默认在内容末尾,导致 textarea 被 拉到底部覆盖已设的滚动位置。改为 focus 后立即恢复 scrollTop。 同时移除调试日志。 --- libs/tiptap-editor/src/index.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libs/tiptap-editor/src/index.ts b/libs/tiptap-editor/src/index.ts index b5949f0..90f1b05 100644 --- a/libs/tiptap-editor/src/index.ts +++ b/libs/tiptap-editor/src/index.ts @@ -166,7 +166,11 @@ class TiptapEditorInstance { proseMirrorDom.style.display = 'none' this.sourceTextarea.hidden = false this.applyScrollRatio(this.sourceTextarea, pmRatio) + // focus() 会触发浏览器把光标所在行滚动进可视区域,光标默认在内容末尾, + // 会把 textarea 拉到底部覆盖上面设好的滚动位置。因此先记录、focus 后再恢复。 + const scrollTopBeforeFocus = this.sourceTextarea.scrollTop this.sourceTextarea.focus() + this.sourceTextarea.scrollTop = scrollTopBeforeFocus this.toggleButton.textContent = '✎' this.toggleButton.title = '切换富文本' this.isSourceMode = true