import React, { useState, useRef, useEffect } from 'react' import { Upload, Trash2, Image as ImageIcon, Palette, X, Sliders, RotateCcw, Check, } from 'lucide-react' import { useFormStore } from '../store/useFormStore' import BackgroundImageControls from './interaction/BackgroundImageControls' const COLOR_PRESETS = [ '#ffffff', '#f8fafc', '#f1f5f9', '#f0fdf4', '#eff6ff', '#faf5ff', '#fff1f2', '#0f172a', ] export default function InlineFormBackgroundEditor({ isOpen, onClose }) { const settings = useFormStore((state) => state.settings) const setSettings = useFormStore((state) => state.setSettings) const [activeTab, setActiveTab] = useState('background') // 'background' | 'border' | 'dimensions' const fileInputRef = useRef(null) // Escape closes the editor (not while the WordPress media modal is open: Escape closes that one). useEffect(() => { if (!isOpen) return undefined const onKey = (e) => { if (e.key !== 'Escape') return const mediaOpen = Array.from(document.querySelectorAll('.media-modal')).some((el) => el.getClientRects().length > 0) if (mediaOpen) return e.stopPropagation() onClose() } document.addEventListener('keydown', onKey, true) return () => document.removeEventListener('keydown', onKey, true) }, [isOpen, onClose]) if (!isOpen) return null const handleColorChange = (color) => { setSettings({ formBgColor: color }) } const handleUploadImage = (e) => { const file = e.target.files?.[0] if (!file) return // If WordPress media library is available in window if (window.wp && window.wp.media) { const mediaFrame = window.wp.media({ title: 'Select Form Background Image', button: { text: 'Use Image' }, multiple: false, }) mediaFrame.on('select', () => { const attachment = mediaFrame.state().get('selection').first().toJSON() setSettings({ backgroundImageUrl: attachment.url }) }) mediaFrame.open() return } // Fallback: Read local file as data URL const reader = new FileReader() reader.onload = () => { setSettings({ backgroundImageUrl: reader.result }) } reader.readAsDataURL(file) } const handleWpMediaSelect = () => { if (window.wp && window.wp.media) { const mediaFrame = window.wp.media({ title: 'Select Form Background Image', button: { text: 'Use Image' }, multiple: false, }) mediaFrame.on('select', () => { const attachment = mediaFrame.state().get('selection').first().toJSON() setSettings({ backgroundImageUrl: attachment.url }) }) mediaFrame.open() } else if (fileInputRef.current) { fileInputRef.current.click() } } const hasBackgroundImage = !!settings.backgroundImageUrl return (