fix(frontend): skip tracking effect update when caret is at end of text

Root cause: after onCategoryEnter sets value="@ai:", the MentionInput sync
effect sets innerHTML with the new content. The browser caret may be at
position 2 (not moved from the user's perspective), but prevCursorRef was
4. The tracking effect read DOM caret=2 and overwrote ms.setCursorOffset(4)
→ cursor jumped to position 2.

Fix: skip tracking effect update when:
- count (DOM caret) is at end of text (ms.value.length), AND
- prevCursorRef was also at end of text
This means the caret hasn't actually moved from the user's POV (just
positioned by the browser after innerHTML), so don't update state.
This commit is contained in:
ZhenYi 2026-04-18 11:39:16 +08:00
parent 126ffda4fe
commit 4330325bfc

View File

@ -104,9 +104,14 @@ const ChatInputArea = memo(function ChatInputArea({
count += node.length; count += node.length;
} }
if (count !== prevCursorRef.current) { if (count !== prevCursorRef.current) {
// Skip update when caret is at end of text (programmatic value change
// that already positioned the caret — the caret hasn't moved from the
// user's perspective). Only update on real user-initiated cursor movement.
if (count !== ms.value.length || prevCursorRef.current !== ms.value.length) {
prevCursorRef.current = count; prevCursorRef.current = count;
ms.setCursorOffset(count); ms.setCursorOffset(count);
} }
}
}); });
// ─── Imperative handle ────────────────────────────────────────────────── // ─── Imperative handle ──────────────────────────────────────────────────