import React from 'react' import { Upload, Image as ImageIcon, Trash2 } from 'lucide-react' // Shared background-image controls of the column and form background editors (the reference builder keys: background image URL, // size cover/contain/auto/custom %, 9 positions, repeat). `value` = { url, size, position, repeat }. export const BG_POSITIONS = [ ['center', 'Center'], ['top', 'Top'], ['bottom', 'Bottom'], ['left', 'Left'], ['right', 'Right'], ['top left', 'Top Left'], ['top right', 'Top Right'], ['bottom left', 'Bottom Left'], ['bottom right', 'Bottom Right'], ] export const BG_REPEATS = [['no-repeat', 'None'], ['repeat', 'Both'], ['repeat-x', 'Horizontal'], ['repeat-y', 'Vertical']] /** A custom size is stored as the CSS value itself ("60%"), which the published form prints unchanged. */ export const isCustomSize = (size) => Boolean(size) && !['cover', 'contain', 'auto'].includes(size) export const customSizePercent = (size) => { const n = parseInt(String(size || '').replace(/[^0-9]/g, ''), 10) return Number.isNaN(n) ? 100 : n } /** Opens the WordPress media library (wp.media); falls back to asking for a URL when it is not loaded. */ export function openMediaLibrary({ title, current, onSelect }) { if (typeof window !== 'undefined' && window.wp && window.wp.media) { const frame = window.wp.media({ title, button: { text: 'Use Image' }, library: { type: 'image' }, multiple: false }) frame.on('select', () => { const attachment = frame.state().get('selection').first().toJSON() if (attachment && attachment.url) onSelect(attachment.url, attachment) }) frame.open() return frame } const url = typeof window !== 'undefined' ? window.prompt('Image URL', current || '') : null if (url !== null && url !== undefined) onSelect(url, null) return null } const selectCls = 'w-full px-2 py-1.5 text-xs border border-gray-200 rounded-md bg-white focus:outline-none focus:border-gray-900' export default function BackgroundImageControls({ value, onChange, mediaTitle = 'Select Background Image', hideSource = false }) { const { url = '', size = 'cover', position = 'center', repeat = 'no-repeat' } = value || {} const custom = isCustomSize(size) return (
{!hideSource && (<>
{url && ( )}
onChange({ url: e.target.value })} onKeyDown={(e) => e.stopPropagation()} className="w-full pl-7 pr-3 py-1.5 text-xs border border-gray-200 rounded-md focus:outline-none focus:border-gray-900" style={{ minHeight: 34 }} />
)} {url && ( <>
{custom && ( )} )}
) }