From 6e03767f62d13526ac86de86b2f34fbdc34e0ccd Mon Sep 17 00:00:00 2001 From: lemonknowsall Date: Fri, 12 Dec 2025 17:54:18 -0800 Subject: [PATCH 1/2] bugfix: add paste image functionality to main index page --- src/components/Shakespeare/ChatInput.tsx | 37 +++++------------------- src/lib/utils.ts | 37 ++++++++++++++++++++++++ src/pages/Index.tsx | 7 +++++ 3 files changed, 52 insertions(+), 29 deletions(-) diff --git a/src/components/Shakespeare/ChatInput.tsx b/src/components/Shakespeare/ChatInput.tsx index fe917d4b..40d55437 100644 --- a/src/components/Shakespeare/ChatInput.tsx +++ b/src/components/Shakespeare/ChatInput.tsx @@ -1,4 +1,4 @@ -import { useState, useCallback, memo } from 'react'; +import { useState, useCallback, useMemo, memo } from 'react'; import { useTranslation } from 'react-i18next'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea'; @@ -7,6 +7,7 @@ import { CircularProgress } from '@/components/ui/circular-progress'; import { FileAttachment } from '@/components/ui/file-attachment'; import { Square, ArrowUp } from 'lucide-react'; import { ModelSelector } from '@/components/ModelSelector'; +import { createPasteHandler } from '@/lib/utils'; interface ChatInputProps { isLoading: boolean; @@ -83,34 +84,12 @@ 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 handlePaste = useMemo( + () => createPasteHandler((file) => { + setAttachedFiles(prev => [...prev, file]); + }), + [] // setAttachedFiles is stable from useState, so no dependencies needed + ); const handleInputChange = useCallback((e: React.ChangeEvent) => { setInput(e.target.value); diff --git a/src/lib/utils.ts b/src/lib/utils.ts index bac620ff..38884eb2 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -69,3 +69,40 @@ export function formatRelativeTime(timestamp: number | Date): string { return `${diffYears} years ago`; } } + +/** + * Creates a paste event handler that extracts images from clipboard data + * and adds them via the provided callback. + * @param onFileAdd - Callback function to add the pasted file + * @returns A paste event handler function + */ +export function createPasteHandler(onFileAdd: (file: File) => void) { + return 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 + onFileAdd(namedFile); + } + break; // Only handle the first image found + } + } + }; +} \ No newline at end of file diff --git a/src/pages/Index.tsx b/src/pages/Index.tsx index 8b276f20..2159ceeb 100644 --- a/src/pages/Index.tsx +++ b/src/pages/Index.tsx @@ -25,6 +25,7 @@ import { useSeoMeta } from '@unhead/react'; import { ShakespeareLogo } from '@/components/ShakespeareLogo'; import { AppShowcase } from '@/components/AppShowcase'; import { useToast } from '@/hooks/useToast'; +import { createPasteHandler } from '@/lib/utils'; export default function Index() { const { t } = useTranslation(); @@ -187,6 +188,11 @@ export default function Index() { } }; + // Handle paste - extract images from clipboard + const handlePaste = createPasteHandler((file) => { + setAttachedFiles(prev => [...prev, file]); + }); + // Handle textarea click - show onboarding if no providers configured const handleTextareaClick = () => { if (!hasProvidersConfigured) { @@ -333,6 +339,7 @@ export default function Index() { value={prompt} onChange={handlePromptChange} onKeyDown={handleKeyDown} + onPaste={handlePaste} onClick={handleTextareaClick} className="resize-none border-0 bg-transparent px-4 py-3 text-base focus-visible:ring-0 focus-visible:ring-offset-0 placeholder:text-muted-foreground" disabled={isCreating || isGeneratingInfo || (hasProvidersConfigured && !providerModel.trim())} From 5a456c9957c1c15218970ee4edfc2873e0f9e854 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 20 Dec 2025 14:13:15 -0600 Subject: [PATCH 2/2] refactor: move paste image handling to Textarea component - Add onPasteImage prop to Textarea component - Remove createPasteHandler utility function from utils.ts - Update Index.tsx and ChatInput.tsx to use onPasteImage prop - onPaste takes precedence over onPasteImage when both are provided --- src/components/Shakespeare/ChatInput.tsx | 12 +----- src/components/ui/textarea.tsx | 52 +++++++++++++++++++++++- src/lib/utils.ts | 36 ---------------- src/pages/Index.tsx | 8 +--- 4 files changed, 53 insertions(+), 55 deletions(-) diff --git a/src/components/Shakespeare/ChatInput.tsx b/src/components/Shakespeare/ChatInput.tsx index 40d55437..f5f869a3 100644 --- a/src/components/Shakespeare/ChatInput.tsx +++ b/src/components/Shakespeare/ChatInput.tsx @@ -1,4 +1,4 @@ -import { useState, useCallback, useMemo, memo } from 'react'; +import { useState, useCallback, memo } from 'react'; import { useTranslation } from 'react-i18next'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea'; @@ -7,7 +7,6 @@ import { CircularProgress } from '@/components/ui/circular-progress'; import { FileAttachment } from '@/components/ui/file-attachment'; import { Square, ArrowUp } from 'lucide-react'; import { ModelSelector } from '@/components/ModelSelector'; -import { createPasteHandler } from '@/lib/utils'; interface ChatInputProps { isLoading: boolean; @@ -84,13 +83,6 @@ export const ChatInput = memo(function ChatInput({ } }, [handleSend]); - const handlePaste = useMemo( - () => createPasteHandler((file) => { - setAttachedFiles(prev => [...prev, file]); - }), - [] // setAttachedFiles is stable from useState, so no dependencies needed - ); - const handleInputChange = useCallback((e: React.ChangeEvent) => { setInput(e.target.value); }, []); @@ -111,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 (