Skip to content

Commit

Permalink
Fix pr #4832: Fix issue #4830: [Bug]: Copy-paste into the "What do yo…
Browse files Browse the repository at this point in the history
…u want to build?" bar doesn't work
  • Loading branch information
openhands-agent committed Nov 7, 2024
1 parent 1a616ea commit 66dfedf
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions frontend/src/components/chat-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,33 @@ export function ChatInput({
const [isDraggingOver, setIsDraggingOver] = React.useState(false);

const handlePaste = (event: React.ClipboardEvent<HTMLTextAreaElement>) => {
// Only handle paste if we have an image paste handler and there are files
// Handle image files if we have an image paste handler
if (onImagePaste && event.clipboardData.files.length > 0) {
const files = Array.from(event.clipboardData.files).filter((file) =>
file.type.startsWith("image/"),
);
// Only prevent default if we found image files to handle
if (files.length > 0) {
event.preventDefault();
onImagePaste(files);
return;
}
}
// For text paste, let the default behavior handle it

// Handle text paste
const text = event.clipboardData.getData('text');
if (text && textareaRef.current) {
event.preventDefault();
const start = textareaRef.current.selectionStart;
const end = textareaRef.current.selectionEnd;
const currentValue = textareaRef.current.value;
const newValue = currentValue.substring(0, start) + text + currentValue.substring(end);
textareaRef.current.value = newValue;
// Update cursor position
const newPosition = start + text.length;
textareaRef.current.setSelectionRange(newPosition, newPosition);
// Trigger onChange if provided
onChange?.(newValue);
}
};

const handleDragOver = (event: React.DragEvent<HTMLTextAreaElement>) => {
Expand Down

0 comments on commit 66dfedf

Please sign in to comment.