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) {