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