// image-editor.js - Nano Banana / Pollinations UI overlay for Image Editing export function initImageEditor() { if (document.getElementById('image-editor-modal')) return; const modalHtml = ` `; document.body.insertAdjacentHTML('beforeend', modalHtml); const modal = document.getElementById('image-editor-modal'); const closeBtn = document.getElementById('close-image-editor'); const canvas = document.getElementById('editor-canvas'); const promptInput = document.getElementById('editor-prompt'); const submitBtn = document.getElementById('editor-submit'); const baseImg = document.getElementById('editor-base-image'); let ctx = canvas.getContext('2d'); let isDrawing = false; let currentPromptContext = ''; function resizeCanvas() { canvas.width = canvas.offsetWidth; canvas.height = canvas.offsetHeight; ctx.lineCap = 'round'; ctx.lineJoin = 'round'; ctx.lineWidth = 30; ctx.strokeStyle = 'rgba(255, 50, 50, 0.5)'; } window.addEventListener('resize', () => { if (modal.style.display !== 'none') resizeCanvas(); }); closeBtn.onclick = () => { modal.style.opacity = '0'; setTimeout(() => modal.style.display = 'none', 300); }; // Drawing Logic const startDrawing = (e) => { isDrawing = true; draw(e); }; const stopDrawing = () => { isDrawing = false; ctx.beginPath(); }; const draw = (e) => { if (!isDrawing) return; e.preventDefault(); const rect = canvas.getBoundingClientRect(); const clientX = e.touches ? e.touches[0].clientX : e.clientX; const clientY = e.touches ? e.touches[0].clientY : e.clientY; const x = clientX - rect.left; const y = clientY - rect.top; ctx.lineTo(x, y); ctx.stroke(); ctx.beginPath(); ctx.moveTo(x, y); }; canvas.addEventListener('mousedown', startDrawing); canvas.addEventListener('mousemove', draw); canvas.addEventListener('mouseup', stopDrawing); canvas.addEventListener('mouseout', stopDrawing); canvas.addEventListener('touchstart', startDrawing, { passive: false }); canvas.addEventListener('touchmove', draw, { passive: false }); canvas.addEventListener('touchend', stopDrawing); submitBtn.onclick = () => { const editInstruction = promptInput.value.trim(); if (!editInstruction) return; // For Pollinations (Option A), we just append the edit to the prompt or overwrite it. // In a real API, we'd send baseImg.src and canvas.toDataURL() as a mask. const newPrompt = `${currentPromptContext}, modified: ${editInstruction}`; // Send a message into the chat conceptually const input = document.querySelector('#chat-input'); const sendBtn = document.querySelector('#send-button'); if (input && sendBtn) { input.value = `Bitte generiere das Bild mit der Änderung: "${editInstruction}". Neuer Prompt: ${newPrompt}`; sendBtn.click(); } closeBtn.click(); }; window.openImageEditor = (url, originalPromptEncoded) => { baseImg.src = url; currentPromptContext = decodeURIComponent(originalPromptEncoded); promptInput.value = ''; modal.style.display = 'flex'; // Force reflow void modal.offsetWidth; modal.style.opacity = '1'; setTimeout(() => { resizeCanvas(); ctx.clearRect(0, 0, canvas.width, canvas.height); }, 100); }; } // Auto-init on load if not a module or manually called if (typeof document !== 'undefined') { document.addEventListener('DOMContentLoaded', initImageEditor); // Just in case it's loaded dynamically after DOMContentLoaded if (document.readyState === 'complete' || document.readyState === 'interactive') { initImageEditor(); } }