import { __ } from '@wordpress/i18n'; import { useBlockProps, InspectorControls, MediaUpload, MediaUploadCheck, InnerBlocks, useInnerBlocksProps } from '@wordpress/block-editor'; import { PanelBody, Button, ToggleControl, BaseControl, RangeControl, SelectControl } from '@wordpress/components'; import './editor.scss'; const ALLOWED_VIDEO_TYPES = ['video/mp4', 'video/webm', 'video/ogg', 'video/ogv']; const INNER_BLOCKS_TEMPLATE = [ ['core/heading', { level: 1, placeholder: 'Add heading...', textAlign: 'center' }], ['core/paragraph', { placeholder: 'Add description...', align: 'center' }], ['core/buttons', { layout: { type: 'flex', justifyContent: 'center' } }] ]; export default function Edit({ attributes, setAttributes }) { const { posterId, posterUrl, videoSources, mobileSources, fadeEffect, overlayOpacity, contentAlignment, minHeight } = attributes; const blockProps = useBlockProps({ className: 'video-cover-editor', style: { minHeight: minHeight ? `${minHeight}px` : undefined } }); const innerBlocksProps = useInnerBlocksProps( { className: 'video-cover-content' }, { template: INNER_BLOCKS_TEMPLATE, templateLock: false } ); const onSelectPoster = (media) => { setAttributes({ posterId: media.id, posterUrl: media.url }); }; const onSelectVideo = (media, isMobile = false) => { const newSource = { id: media.id, url: media.url, mime: media.mime }; if (isMobile) { // Check if this format already exists in mobile sources const exists = mobileSources.some(s => s.mime === media.mime); if (!exists) { setAttributes({ mobileSources: [...mobileSources, newSource] }); } } else { // Check if this format already exists in desktop sources const exists = videoSources.some(s => s.mime === media.mime); if (!exists) { setAttributes({ videoSources: [...videoSources, newSource] }); } } }; const removeVideoSource = (index, isMobile = false) => { if (isMobile) { const updated = [...mobileSources]; updated.splice(index, 1); setAttributes({ mobileSources: updated }); } else { const updated = [...videoSources]; updated.splice(index, 1); setAttributes({ videoSources: updated }); } }; const renderVideoSourceList = (sources, isMobile = false) => { if (sources.length === 0) return null; return (
{videoSources.length} {__('desktop source(s)', 'jvb')} {mobileSources.length > 0 && `, ${mobileSources.length} ${__('mobile source(s)', 'jvb')}` }
{__('Configure video sources in the sidebar →', 'jvb')}