'use client' import { useState } from 'react' import { useTheme } from '@/lib/ThemeContext' import { useLanguage } from '@/lib/LanguageContext' import { useWorksheet } from '@/lib/worksheet-editor/WorksheetContext' import type { AIImageStyle } from '@/app/worksheet-editor/types' interface AIImageGeneratorProps { isOpen: boolean onClose: () => void } const AI_STYLES: { id: AIImageStyle; name: string; description: string }[] = [ { id: 'educational', name: 'Bildung', description: 'Klare, lehrreiche Illustrationen' }, { id: 'cartoon', name: 'Cartoon', description: 'Bunte, kindgerechte Zeichnungen' }, { id: 'realistic', name: 'Realistisch', description: 'Fotorealistische Darstellungen' }, { id: 'sketch', name: 'Skizze', description: 'Handgezeichneter Stil' }, { id: 'clipart', name: 'Clipart', description: 'Einfache, flache Grafiken' }, ] const SIZE_OPTIONS = [ { width: 256, height: 256, label: '256 x 256' }, { width: 512, height: 512, label: '512 x 512' }, { width: 512, height: 256, label: '512 x 256 (Breit)' }, { width: 256, height: 512, label: '256 x 512 (Hoch)' }, ] export function AIImageGenerator({ isOpen, onClose }: AIImageGeneratorProps) { const { isDark } = useTheme() const { t } = useLanguage() const { canvas, setActiveTool, saveToHistory } = useWorksheet() const [prompt, setPrompt] = useState('') const [style, setStyle] = useState('educational') const [sizeIndex, setSizeIndex] = useState(1) // Default: 512x512 const [isGenerating, setIsGenerating] = useState(false) const [previewUrl, setPreviewUrl] = useState(null) const [error, setError] = useState(null) const selectedSize = SIZE_OPTIONS[sizeIndex] const handleGenerate = async () => { if (!prompt.trim()) { setError('Bitte geben Sie eine Beschreibung ein.') return } setIsGenerating(true) setError(null) setPreviewUrl(null) const { hostname, protocol } = window.location const apiBase = hostname === 'localhost' ? 'http://localhost:8086' : `${protocol}//${hostname}:8086` try { const response = await fetch(`${apiBase}/api/v1/worksheet/ai-image`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ prompt, style, width: selectedSize.width, height: selectedSize.height }) }) if (!response.ok) { const data = await response.json() throw new Error(data.detail || 'Bildgenerierung fehlgeschlagen') } const data = await response.json() if (data.error) { throw new Error(data.error) } setPreviewUrl(data.image_base64) } catch (err: any) { console.error('AI Image generation failed:', err) setError(err.message || 'Verbindung zum KI-Server fehlgeschlagen. Bitte überprüfen Sie, ob Ollama läuft.') } finally { setIsGenerating(false) } } const handleInsert = () => { if (!previewUrl || !canvas) return // Add image to canvas if ((canvas as any).addImage) { (canvas as any).addImage(previewUrl) } // Reset and close setPrompt('') setPreviewUrl(null) setActiveTool('select') onClose() } if (!isOpen) return null // Glassmorphism styles const overlayStyle = 'fixed inset-0 bg-black/50 backdrop-blur-sm z-50' const modalStyle = isDark ? 'backdrop-blur-xl bg-white/10 border border-white/20' : 'backdrop-blur-xl bg-white/90 border border-black/10 shadow-2xl' const inputStyle = isDark ? 'bg-white/10 border-white/20 text-white placeholder-white/40 focus:border-purple-400' : 'bg-white/50 border-black/10 text-slate-900 placeholder-slate-400 focus:border-purple-500' const labelStyle = isDark ? 'text-white/70' : 'text-slate-600' return (
e.stopPropagation()} > {/* Header */}

KI-Bild generieren

Beschreiben Sie das gewünschte Bild

{/* Prompt Input */}