fix: preserve undo history when pasting into the chat input#492
Draft
laileni-aws wants to merge 2 commits into
Draft
fix: preserve undo history when pasting into the chat input#492laileni-aws wants to merge 2 commits into
laileni-aws wants to merge 2 commits into
Conversation
The chat prompt input handled paste by manually inserting a text node
at the cursor, which bypasses the browser's native undo stack. As a
result, undo (Ctrl/Cmd+Z) did not work correctly after pasting.
Insert the pasted text with document.execCommand('insertText'), which
records the change in the undo history, and fall back to the previous
manual insertion when execCommand is unavailable. Adds tests for the
execCommand path, the fallback path, and empty-paste handling.
Keeps the repo lint gate green (strict-boolean-expressions).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Pasting text into the chat prompt input broke undo: pressing Ctrl/Cmd+Z after a paste did not undo the pasted text correctly. This PR inserts pasted text in a way that participates in the browser's native undo history.
Problem
The prompt input's
pastehandler prevented the default paste and inserted the clipboard text by manually creating and inserting a DOM text node at the cursor:Directly mutating the DOM this way does not add an entry to the browser's undo stack, so after a paste the editor's undo history is inconsistent and Ctrl/Cmd+Z does not undo the paste as expected.
Fix
Insert the pasted text with
document.execCommand('insertText', false, text), which performs the insertion through the editing pipeline and records it in the native undo history. IfexecCommandis unavailable (or returnsfalse), fall back to the previous manual text-node insertion so behavior is preserved in environments that do not support it.Testing
src/components/__test__/chat-item/prompt-text-input.spec.ts:execCommand('insertText')(the undo-preserving path) and fires the input event;execCommandis unavailable, the handler falls back to manual insertion and the text still appears;execCommandtest was confirmed to fail against the previous manual-insertion implementation and pass with this change.npx jest): 844 tests.eslintandprettier --checkpass; production build (npm run build) succeeds.Additional change
ui-tests/__test__/flows/quick-action-commands-header.ts: coerced the result of a Playwrightevaluatecall to a boolean withBoolean(...)before using it in a condition, satisfying@typescript-eslint/strict-boolean-expressionsand keeping the repo lint gate green.Notes
document.execCommandis deprecated but remains the only widely supported way to insert text into acontenteditableelement while preserving the native undo stack; the fallback keeps the previous behavior where it is not available.