fix: preserve undo history when selecting @ mention autocompletion

- Use document.execCommand("insertText") instead of directly setting input value
- This preserves the browser undo/redo stack, allowing Ctrl+Z to undo autocompletion
- Follows the same pattern already used for enhanced prompt feature
- Fixes #8310
This commit is contained in:
Roo Code 2025-09-25 15:22:29 +00:00
parent bf1aafad9b
commit 287ec589e5

View file

@ -371,7 +371,26 @@ export const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
isSlashCommand,
)
setInputValue(newValue)
// Use execCommand to preserve undo history
try {
if (document.execCommand) {
// Focus the textarea to ensure it's the active element
textAreaRef.current.focus()
// Select all text first
textAreaRef.current.select()
// Insert the new text using execCommand to preserve undo stack
document.execCommand("insertText", false, newValue)
} else {
// Fallback to direct value setting if execCommand is not available
setInputValue(newValue)
}
} catch {
// Fallback to direct value setting if execCommand fails
setInputValue(newValue)
}
const newCursorPosition = newValue.indexOf(" ", mentionIndex + insertValue.length) + 1
setCursorPosition(newCursorPosition)
setIntendedCursorPosition(newCursorPosition)