diff --git a/src/components/Shakespeare/ChatInput.tsx b/src/components/Shakespeare/ChatInput.tsx index fe917d4b..f5f869a3 100644 --- a/src/components/Shakespeare/ChatInput.tsx +++ b/src/components/Shakespeare/ChatInput.tsx @@ -83,35 +83,6 @@ export const ChatInput = memo(function ChatInput({ } }, [handleSend]); - const handlePaste = useCallback(async (e: React.ClipboardEvent) => { - const items = e.clipboardData?.items; - if (!items) return; - - for (let i = 0; i < items.length; i++) { - const item = items[i]; - - // Check if the item is an image - if (item.type.startsWith('image/')) { - e.preventDefault(); // Prevent default paste behavior for images - - const file = item.getAsFile(); - if (file) { - // Generate a filename with timestamp - const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); - const extension = file.type.split('/')[1] || 'png'; - const filename = `pasted-image-${timestamp}.${extension}`; - - // Create a new File object with the generated name - const namedFile = new File([file], filename, { type: file.type }); - - // Add to attached files - setAttachedFiles(prev => [...prev, namedFile]); - } - break; // Only handle the first image found - } - } - }, []); - const handleInputChange = useCallback((e: React.ChangeEvent) => { setInput(e.target.value); }, []); @@ -132,7 +103,7 @@ export const ChatInput = memo(function ChatInput({ value={input} onChange={handleInputChange} onKeyDown={handleKeyDown} - onPaste={handlePaste} + onPasteImage={(file) => setAttachedFiles(prev => [...prev, file])} onFocus={onFocus} placeholder={ !isConfigured diff --git a/src/components/ui/textarea.tsx b/src/components/ui/textarea.tsx index 12c91369..7eae92ca 100644 --- a/src/components/ui/textarea.tsx +++ b/src/components/ui/textarea.tsx @@ -2,10 +2,57 @@ import * as React from "react" import { cn } from "@/lib/utils" -export type TextareaProps = React.TextareaHTMLAttributes +export interface TextareaProps extends React.TextareaHTMLAttributes { + /** + * Callback fired when an image is pasted into the textarea. + * If both onPaste and onPasteImage are provided, onPaste takes precedence. + */ + onPasteImage?: (file: File) => void; +} const Textarea = React.forwardRef( - ({ className, ...props }, ref) => { + ({ className, onPaste, onPasteImage, ...props }, ref) => { + const handlePaste = React.useCallback( + async (e: React.ClipboardEvent) => { + // If onPaste is provided, use it and skip image handling + if (onPaste) { + onPaste(e); + return; + } + + // If onPasteImage is provided, handle image pasting + if (onPasteImage) { + const items = e.clipboardData?.items; + if (!items) return; + + for (let i = 0; i < items.length; i++) { + const item = items[i]; + + // Check if the item is an image + if (item.type.startsWith('image/')) { + e.preventDefault(); // Prevent default paste behavior for images + + const file = item.getAsFile(); + if (file) { + // Generate a filename with timestamp + const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); + const extension = file.type.split('/')[1] || 'png'; + const filename = `pasted-image-${timestamp}.${extension}`; + + // Create a new File object with the generated name + const namedFile = new File([file], filename, { type: file.type }); + + // Call the onPasteImage callback + onPasteImage(namedFile); + } + break; // Only handle the first image found + } + } + } + }, + [onPaste, onPasteImage] + ); + return (