diff --git a/README.md b/README.md index 1b4a21d4..5cb1e23b 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Shakespeare is an AI chat application that allows users to build custom Nostr we ## Features - **AI-Powered Development**: Chat with an AI assistant to build your Nostr website -- **File Attachments**: Attach files directly to chat messages with structured text parts, visual badges, and deduplication +- **File Attachments**: Attach files directly to chat messages with structured text parts, visual badges, and deduplication. Supports both file selection and clipboard image pasting - **Project Management**: Create and manage multiple projects with sidebar navigation - **Real-time Editing**: Edit files directly in the browser with syntax highlighting - **File Explorer**: Browse and manage your project files @@ -16,7 +16,7 @@ Shakespeare is an AI chat application that allows users to build custom Nostr we 1. **Create a Project**: Enter a description of what you want to build in the prompt textarea 2. **Chat with AI**: Use the AI assistant to add features, edit files, and build your website -3. **Attach Files**: Click the paperclip icon to attach files to your messages - they'll show as "Added file: /path/filename" +3. **Attach Files**: Click the paperclip icon to attach files or paste images directly from your clipboard - they'll show as "Added file: /path/filename" 4. **Edit Code**: Switch to the code view to manually edit files if needed 5. **Preview**: View your website in the preview pane diff --git a/src/components/Shakespeare/ChatPane.test.tsx b/src/components/Shakespeare/ChatPane.test.tsx new file mode 100644 index 00000000..9ad6c8b4 --- /dev/null +++ b/src/components/Shakespeare/ChatPane.test.tsx @@ -0,0 +1,121 @@ +import { describe, it, expect, vi } from 'vitest'; +import { render, screen, fireEvent } from '@testing-library/react'; +import { TestApp } from '@/test/TestApp'; +import { ChatPane } from './ChatPane'; + +// Mock the AI settings hook to return configured state +vi.mock('@/hooks/useAISettings', () => ({ + useAISettings: () => ({ + isConfigured: true, + settings: { + recentlyUsedModels: ['gpt-4'], + }, + addRecentlyUsedModel: vi.fn(), + }), +})); + +// Mock other hooks +vi.mock('@/hooks/useFS', () => ({ + useFS: () => ({ + fs: {}, + }), +})); + +vi.mock('@/hooks/useCurrentUser', () => ({ + useCurrentUser: () => ({ + user: null, + metadata: null, + }), +})); + +vi.mock('@/hooks/useKeepAlive', () => ({ + useKeepAlive: () => ({ + updateMetadata: vi.fn(), + }), +})); + +vi.mock('@/hooks/useAIChat', () => ({ + useAIChat: () => ({ + messages: [], + streamingMessage: null, + isLoading: false, + totalCost: 0, + lastInputTokens: 0, + sendMessage: vi.fn(), + startGeneration: vi.fn(), + stopGeneration: vi.fn(), + startNewSession: vi.fn(), + }), +})); + +vi.mock('@/hooks/useProviderModels', () => ({ + useProviderModels: () => ({ + models: [ + { + fullId: 'gpt-4', + contextLength: 8192, + }, + ], + }), +})); + +describe('ChatPane', () => { + it('handles image paste from clipboard', async () => { + render( + + + + ); + + const textarea = screen.getByPlaceholderText(/Ask me to add features/); + expect(textarea).toBeInTheDocument(); + + // Create a mock image file + const imageFile = new File(['fake-image-data'], 'test.png', { + type: 'image/png', + }); + + // Create mock clipboard data + const mockClipboardItem = { + type: 'image/png', + getAsFile: () => imageFile, + } as DataTransferItem; + + const mockClipboardData = { + items: [mockClipboardItem], + length: 1, + } as unknown as DataTransfer; + + // Fire the paste event on the textarea using fireEvent + fireEvent.paste(textarea, { + clipboardData: mockClipboardData, + }); + + // The file should be added to the attached files + // We can't easily test the internal state, but we can verify the event was handled + // without throwing an error + expect(textarea).toBeInTheDocument(); + }); + + it('renders welcome message when no messages exist', () => { + render( + + + + ); + + expect(screen.getByText('Welcome to Shakespeare')).toBeInTheDocument(); + expect(screen.getByText(/Your AI-powered development assistant/)).toBeInTheDocument(); + }); + + it('shows placeholder text mentioning paste functionality', () => { + render( + + + + ); + + const textarea = screen.getByPlaceholderText(/Paste images directly or use 📎 to attach files/); + expect(textarea).toBeInTheDocument(); + }); +}); \ No newline at end of file diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index 4c6a956a..84de7222 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -349,6 +349,35 @@ export const ChatPane = forwardRef(({ } }; + const handlePaste = 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 + } + } + }; + // Handle first user interaction to enable audio context const handleFirstInteraction = () => { // This will be handled automatically by the useKeepAlive hook @@ -496,6 +525,7 @@ export const ChatPane = forwardRef(({ value={input} onChange={(e) => setInput(e.target.value)} onKeyDown={handleKeyDown} + onPaste={handlePaste} onFocus={handleFirstInteraction} placeholder={providerModel.trim() ? "Ask me to add features, edit files, or build your project..." : "Please select a model to start chatting..."} className="min-h-[52px] max-h-32 resize-none border-0 bg-transparent px-4 py-3 pb-12 text-sm focus-visible:ring-0 focus-visible:ring-offset-0 placeholder:text-muted-foreground"