From cf484402fa668b031b2bd3414f071ef3b8e3dd8a Mon Sep 17 00:00:00 2001 From: laileni Date: Fri, 26 Dec 2025 11:00:39 -0800 Subject: [PATCH] fix: Fixed the undo functionality for pasted text in the Amazon Q chat input --- .../prompt-input/prompt-text-input.ts | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/components/chat-item/prompt-input/prompt-text-input.ts b/src/components/chat-item/prompt-input/prompt-text-input.ts index b9ebdeeeb..712a30fb9 100644 --- a/src/components/chat-item/prompt-input/prompt-text-input.ts +++ b/src/components/chat-item/prompt-input/prompt-text-input.ts @@ -139,17 +139,27 @@ export class PromptTextInput { // Get plain text from clipboard const text = e.clipboardData?.getData('text/plain'); if (text != null) { - // Insert text at cursor position - const selection = window.getSelection(); - if ((selection?.rangeCount) != null) { - const range = selection.getRangeAt(0); - range.deleteContents(); - range.insertNode(document.createTextNode(text)); - - // Move cursor to end of inserted text - range.collapse(false); - selection.removeAllRanges(); - selection.addRange(range); + // Use execCommand to insert text - this preserves the browser's native undo stack + // so that Ctrl+Z will properly undo the paste operation. + // Note: execCommand is deprecated but is still the most reliable way to integrate + // with the browser's native undo/redo stack for contenteditable elements. + const success = document.execCommand('insertText', false, text); + + // Fallback for browsers that don't support execCommand('insertText') + if (!success) { + const selection = window.getSelection(); + if ((selection?.rangeCount) != null && selection.rangeCount > 0) { + const range = selection.getRangeAt(0); + range.deleteContents(); + const textNode = document.createTextNode(text); + range.insertNode(textNode); + + // Move cursor to end of inserted text + range.setStartAfter(textNode); + range.setEndAfter(textNode); + selection.removeAllRanges(); + selection.addRange(range); + } } // Check if input is empty and trigger input event