From 862ccfcbddc555f4f1703faf5953ae6fa370f13e Mon Sep 17 00:00:00 2001 From: tianyaxiang Date: Tue, 4 Feb 2025 17:47:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BC=96=E8=BE=91=E6=97=B6?= =?UTF-8?q?=E6=BB=9A=E5=8A=A8=E6=9D=A1=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/editor/WechatEditor.tsx | 1 + src/components/editor/components/DesktopEditor.tsx | 7 +++++++ src/components/editor/components/MobileEditor.tsx | 7 +++++++ 3 files changed, 15 insertions(+) diff --git a/src/components/editor/WechatEditor.tsx b/src/components/editor/WechatEditor.tsx index 47566b3..388814b 100644 --- a/src/components/editor/WechatEditor.tsx +++ b/src/components/editor/WechatEditor.tsx @@ -105,6 +105,7 @@ export default function WechatEditor() { setValue(newValue) handleEditorChange(newValue) + // 使用 requestAnimationFrame 确保在下一帧恢复滚动位置和光标位置 requestAnimationFrame(() => { if (textareaRef.current) { textareaRef.current.scrollTop = currentPosition.scrollTop diff --git a/src/components/editor/components/DesktopEditor.tsx b/src/components/editor/components/DesktopEditor.tsx index c8fc9c3..7415a3e 100644 --- a/src/components/editor/components/DesktopEditor.tsx +++ b/src/components/editor/components/DesktopEditor.tsx @@ -61,8 +61,15 @@ export function DesktopEditor({ ref={textareaRef} value={value} onChange={e => { + const scrollTop = e.target.scrollTop; onValueChange(e.target.value) onEditorChange(e.target.value) + // 保持滚动位置 + requestAnimationFrame(() => { + if (textareaRef.current) { + textareaRef.current.scrollTop = scrollTop; + } + }); }} onKeyDown={onKeyDown} onScroll={e => { diff --git a/src/components/editor/components/MobileEditor.tsx b/src/components/editor/components/MobileEditor.tsx index 6e98936..c3ea9ae 100644 --- a/src/components/editor/components/MobileEditor.tsx +++ b/src/components/editor/components/MobileEditor.tsx @@ -55,8 +55,15 @@ export function MobileEditor({ ref={textareaRef} value={value} onChange={e => { + const scrollTop = e.target.scrollTop; onValueChange(e.target.value) onEditorChange(e.target.value) + // 保持滚动位置 + requestAnimationFrame(() => { + if (textareaRef.current) { + textareaRef.current.scrollTop = scrollTop; + } + }); }} onScroll={e => { if (textareaRef.current) {