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 8, 2024
1 parent d3203d3 commit 2971dd2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
35 changes: 30 additions & 5 deletions frontend/__tests__/components/chat/chat-input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,24 @@ describe("ChatInput", () => {
const input = screen.getByTestId("chat-input").querySelector("textarea");
expect(input).toBeTruthy();

// Paste text data
await user.paste(input!, "test paste");
// Create a paste event with text data
const clipboardData = {
files: [],
getData: (type: string) => type === "text" ? "test paste" : "",
items: [],
types: ["text"]
};

// Create and dispatch paste event
const pasteEvent = new Event("paste", { bubbles: true });
Object.defineProperty(pasteEvent, "clipboardData", {
value: clipboardData
});

input!.dispatchEvent(pasteEvent);

// Verify text paste was handled
expect(onChange).toHaveBeenCalledWith("test paste");
});

it("should handle image paste correctly", async () => {
Expand All @@ -185,12 +201,21 @@ describe("ChatInput", () => {

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

// Paste image data
await user.paste(input!, {
files: [file]
// Create and dispatch paste event
const pasteEvent = new Event("paste", { bubbles: true });
Object.defineProperty(pasteEvent, "clipboardData", {
value: clipboardData
});

input!.dispatchEvent(pasteEvent);

// Verify image paste was handled
expect(onImagePaste).toHaveBeenCalledWith([file]);
});
Expand Down
16 changes: 13 additions & 3 deletions frontend/src/components/chat-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,28 @@ 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 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/"),
);
// 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 && onChange) {
const target = event.target as HTMLTextAreaElement;
const start = target.selectionStart;
const end = target.selectionEnd;
const currentValue = target.value;
const newValue =
currentValue.substring(0, start) + text + currentValue.substring(end);
onChange(newValue);
}
};

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

0 comments on commit 2971dd2

Please sign in to comment.