Skip to content

Commit ae3cf7a

Browse files
committed
feat: add paste handling for image uploads in ChatView component
- Implemented a new handlePaste function to manage image pasting from the clipboard. - Enhanced user experience by allowing image files to be directly uploaded via paste action. - Prevented default paste behavior if no text is present, ensuring only image files are processed.
1 parent bca3716 commit ae3cf7a

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

src/components/ChatView.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Textarea, UnstyledButton } from "@mantine/core";
22
import { useCallback, useEffect, useMemo, useRef } from "react";
3+
import type { ClipboardEvent } from "react";
34
import { toast } from "sonner";
45
import { twJoin } from "tailwind-merge";
56
import { useImmer } from "use-immer";
@@ -176,6 +177,28 @@ const ChatView = ({
176177
setAttachments([]);
177178
};
178179

180+
const handlePaste = useCallback(
181+
(event: ClipboardEvent<HTMLTextAreaElement>) => {
182+
const clipboardData = event.clipboardData;
183+
const imageItems = Array.from(clipboardData?.items ?? []).filter(
184+
(item) => item.kind === "file" && item.type.startsWith("image/"),
185+
);
186+
if (imageItems.length === 0) {
187+
return;
188+
}
189+
if (!clipboardData?.getData("text/plain")) {
190+
event.preventDefault();
191+
}
192+
for (const item of imageItems) {
193+
const file = item.getAsFile();
194+
if (file) {
195+
void handleFileInput(file);
196+
}
197+
}
198+
},
199+
[handleFileInput],
200+
);
201+
179202
return (
180203
<div className="flex flex-col h-full min-h-0">
181204
<div
@@ -256,6 +279,7 @@ const ChatView = ({
256279
onChange={(event) => {
257280
setPrompt(event.target.value);
258281
}}
282+
onPaste={handlePaste}
259283
onCompositionStart={() => {
260284
isComposing.current = true;
261285
}}

0 commit comments

Comments
 (0)