From 4f42a2faff4c21f876c049bc80ffd8c4ab8920f3 Mon Sep 17 00:00:00 2001 From: freefire Date: Sat, 8 Aug 2026 15:09:43 +0800 Subject: [PATCH] =?UTF-8?q?fix(comment):=20=E4=BF=AE=E5=A4=8D=E8=AF=84?= =?UTF-8?q?=E8=AE=BA=E6=A1=86=E6=8F=90=E4=BA=A4=E5=90=8E=E9=87=8D=E7=BD=AE?= =?UTF-8?q?=E5=92=8C=E8=81=9A=E7=84=A6=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 为CommentEditor的focus方法添加preventScroll可选参数并透传调用 2. 使用ref记录上次提交计数,避免首次挂载时重复清空输入框 3. 仅在submitCount实际变化时重置表单并聚焦编辑器 --- frontend/src/components/CommentBox.tsx | 4 ++++ frontend/src/components/CommentEditor.tsx | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/frontend/src/components/CommentBox.tsx b/frontend/src/components/CommentBox.tsx index a954d4c..b005cc7 100644 --- a/frontend/src/components/CommentBox.tsx +++ b/frontend/src/components/CommentBox.tsx @@ -30,6 +30,7 @@ export default function CommentBox({ user, replyTo, inline, submitting, submitCo const [content, setContent] = useState(''); const [isPrivate, setIsPrivate] = useState(false); const editorRef = useRef(null); + const prevSubmitCountRef = useRef(submitCount); useEffect(() => { if (inline && replyTo) { @@ -38,6 +39,9 @@ export default function CommentBox({ user, replyTo, inline, submitting, submitCo }, [replyTo?.id, inline]); useEffect(() => { + // 跳过首次挂载,仅在 submitCount 真正变化(评论成功提交)时清空并聚焦 + if (prevSubmitCountRef.current === submitCount) return; + prevSubmitCountRef.current = submitCount; setContent(''); setIsPrivate(false); editorRef.current?.focus(); diff --git a/frontend/src/components/CommentEditor.tsx b/frontend/src/components/CommentEditor.tsx index 5518e5d..7ce81d3 100644 --- a/frontend/src/components/CommentEditor.tsx +++ b/frontend/src/components/CommentEditor.tsx @@ -26,7 +26,7 @@ import type { Sticker } from '../data/stickers'; export interface CommentEditorHandle { getHTML: () => string; isEmpty: () => boolean; - focus: () => void; + focus: (options?: { preventScroll?: boolean }) => void; } interface Props { @@ -160,7 +160,7 @@ const CommentEditor = forwardRef(function CommentEdi useImperativeHandle(ref, () => ({ getHTML: () => editor ? sanitizeHtml(editor.getHTML()) : value, isEmpty: () => editor ? isEditorEmpty(editor) : !value.trim(), - focus: () => { editor?.commands.focus(); }, + focus: (options?: { preventScroll?: boolean }) => { editor?.commands.focus(options); }, }), [editor, value]); const insertSticker = useCallback((sticker: Sticker) => {