Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions src/components/chat-item/prompt-input/prompt-text-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down