From 287ec589e535ae7c1b81d93668733dc0efad5a7d Mon Sep 17 00:00:00 2001 From: Roo Code Date: Thu, 25 Sep 2025 15:22:29 +0000 Subject: [PATCH] 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 --- .../src/components/chat/ChatTextArea.tsx | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/webview-ui/src/components/chat/ChatTextArea.tsx b/webview-ui/src/components/chat/ChatTextArea.tsx index 993912f240..8e66515670 100644 --- a/webview-ui/src/components/chat/ChatTextArea.tsx +++ b/webview-ui/src/components/chat/ChatTextArea.tsx @@ -371,7 +371,26 @@ export const ChatTextArea = forwardRef( 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)