Skip to content

Commit

Permalink
Fix issue #4830: [Bug]: Copy-paste into the "What do you want to buil…
Browse files Browse the repository at this point in the history
…d?" bar doesn't work (#4832)

Co-authored-by: Graham Neubig <[email protected]>
  • Loading branch information
openhands-agent and neubig authored Nov 8, 2024
1 parent a6810fa commit f5003a7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
44 changes: 43 additions & 1 deletion frontend/__tests__/components/chat/chat-input.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import userEvent from "@testing-library/user-event";
import { render, screen } from "@testing-library/react";
import { fireEvent, render, screen } from "@testing-library/react";
import { describe, afterEach, vi, it, expect } from "vitest";
import { ChatInput } from "#/components/chat-input";

Expand Down Expand Up @@ -158,4 +158,46 @@ describe("ChatInput", () => {
await user.tab();
expect(onBlurMock).toHaveBeenCalledOnce();
});

it("should handle text paste correctly", () => {
const onSubmit = vi.fn();
const onChange = vi.fn();

render(<ChatInput onSubmit={onSubmit} onChange={onChange} />);

const input = screen.getByTestId("chat-input").querySelector("textarea");
expect(input).toBeTruthy();

// Fire paste event with text data
fireEvent.paste(input!, {
clipboardData: {
getData: (type: string) => type === 'text/plain' ? 'test paste' : '',
files: []
}
});
});

it("should handle image paste correctly", () => {
const onSubmit = vi.fn();
const onImagePaste = vi.fn();

render(<ChatInput onSubmit={onSubmit} onImagePaste={onImagePaste} />);

const input = screen.getByTestId("chat-input").querySelector("textarea");
expect(input).toBeTruthy();

// Create a paste event with an image file
const file = new File(["dummy content"], "image.png", { type: "image/png" });

// Fire paste event with image data
fireEvent.paste(input!, {
clipboardData: {
getData: () => '',
files: [file]
}
});

// Verify image paste was handled
expect(onImagePaste).toHaveBeenCalledWith([file]);
});
});
9 changes: 7 additions & 2 deletions frontend/src/components/chat-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,18 @@ export function ChatInput({
const [isDraggingOver, setIsDraggingOver] = React.useState(false);

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

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

0 comments on commit f5003a7

Please sign in to comment.