/** * WPVR Gutenberg Block * * A modern implementation of the WPVR tour block using ES6 and WordPress Block API */ const { __ } = wp.i18n; const { registerBlockType } = wp.blocks; const { useEffect, useState } = wp.element; const { InspectorControls, useBlockProps } = wp.blockEditor; const { TextControl, SelectControl, ColorPalette, PanelBody, RangeControl, __experimentalUnitControl: UnitControl, __experimentalNumberControl: NumberControl } = wp.components; const { apiFetch } = wp; // Block icon as SVG const blockIcon = ; // Available colors for the border const BORDER_COLORS = [ { name: 'red', color: '#f00' }, { name: 'white', color: '#fff' }, { name: 'blue', color: '#00f' }, { name: 'black', color: '#000' }, { name: 'gray', color: '#888' }, ]; // Border style options const BORDER_STYLES = [ { value: 'none', label: __('None', 'wpvr') }, { value: 'solid', label: __('Solid', 'wpvr') }, { value: 'dotted', label: __('Dotted', 'wpvr') }, { value: 'dashed', label: __('Dashed', 'wpvr') }, { value: 'double', label: __('Double', 'wpvr') }, ]; // Width unit options const WIDTH_UNITS = [ { value: 'px', label: 'px' }, { value: '%', label: '%' }, { value: 'vw', label: 'vw' }, { value: 'fullwidth', label: __('Fullwidth', 'wpvr') }, ]; // Height unit options const HEIGHT_UNITS = [ { value: 'px', label: 'px' }, { value: 'vh', label: 'vh' }, ]; // Radius unit options const RADIUS_UNITS = [ { value: 'px', label: 'px' }, { value: '%', label: '%' }, ]; /** * WPVR Block Edit component using functional component and hooks */ function WpvrEdit({ attributes, setAttributes }) { const [tourOptions, setTourOptions] = useState([{ value: "0", label: __("None", "wpvr") }]); // Fetch tour data when component mounts useEffect(() => { apiFetch({ path: 'wpvr/v1/panodata' }) .then(data => { if (data && Array.isArray(data)) { setTourOptions(data); } }) .catch(error => { console.error('Error fetching WPVR tour data:', error); }); }, []); // Handle width unit change const handleWidthUnitChange = (value) => { if (value === 'fullwidth' && attributes.width === 'fullwidth') { setAttributes({ width: 'fullwidth', width_unit: '' }); } else { if( value === 'fullwidth' ){ setAttributes({ width: 'fullwidth', width_unit: '' }); }else { setAttributes({ width: attributes.width_unit === '' ? '600' : attributes.width, width_unit: value }); } } console.log(attributes.width, value); }; // Get block props with custom styles const blockProps = useBlockProps({ className: 'wpvr-block-preview', style: { width: attributes.width_unit ? `${attributes.width}${attributes.width_unit}` : (attributes.width === 'fullwidth' ? '100%' : undefined), height: `${attributes.height}${attributes.height_unit}`, borderRadius: attributes.radius ? `${attributes.radius}${attributes.radius_unit}` : undefined, borderStyle: attributes.border_style, borderColor: attributes.border_color, borderWidth: attributes.border_width ? `${attributes.border_width}px` : undefined, } }); return ( <> setAttributes({ id: value })} />

{__('Tour Width', 'wpvr')}

setAttributes({ width: value })} disabled={attributes.width === 'fullwidth'} />

{__('Tour Height', 'wpvr')}

setAttributes({ height: value })} /> setAttributes({ height_unit: value })} />

{__('Tour Mobile Height', 'wpvr')}

setAttributes({ mobile_height: value })} /> setAttributes({ mobile_height_unit: value })} />

{__('Tour Border Radius', 'wpvr')}

setAttributes({ radius: value })} /> setAttributes({ radius_unit: value })} />
setAttributes({ border_width: value.toString() })} min={0} max={20} /> setAttributes({ border_style: value })} />

{__('Tour Border Color', 'wpvr')}

setAttributes({ border_color: value })} disableCustomColors={false} />
{attributes.id !== "0" ? (
{__('WPVR Tour Preview', 'wpvr')}

ID: {attributes.id}
{__('Width', 'wpvr')}: {attributes.width}{attributes.width_unit}
{__('Height', 'wpvr')}: {attributes.height}{attributes.height_unit}
{__('Mobile Height', 'wpvr')}: {attributes.mobile_height}{attributes.mobile_height_unit}
{attributes.radius && `${__('Radius', 'wpvr')}: ${attributes.radius}${attributes.radius_unit}`}

) : (
{__('Please select a tour from the block settings panel.', 'wpvr')}
)}
); } // Register the block registerBlockType('wpvr/wpvr-block', { title: __('WPVR Tour', 'wpvr'), description: __('Add a virtual reality tour to your content.', 'wpvr'), icon: blockIcon, category: 'media', keywords: [__('vr', 'wpvr'), __('tour', 'wpvr'), __('panorama', 'wpvr'), __('virtual reality', 'wpvr')], attributes: { id: { type: 'string', default: '0', }, width: { type: 'string', default: '600', }, width_unit: { type: 'string', default: 'px', }, height: { type: 'string', default: '400', }, height_unit: { type: 'string', default: 'px', }, mobile_height: { type: 'string', default: '300', }, mobile_height_unit: { type: 'string', default: 'px', }, radius: { type: 'string', default: '0', }, radius_unit: { type: 'string', default: 'px', }, border_width: { type: 'string', default: '0', }, border_style: { type: 'string', default: 'none', }, border_color: { type: 'string', default: '', }, }, edit: WpvrEdit, save: () => { // Return null to use PHP render callback return null; }, });