/**
* 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 (
<>
{__('Tour Width', 'wpvr')} {__('Tour Height', 'wpvr')} {__('Tour Mobile Height', 'wpvr')} {__('Tour Border Radius', 'wpvr')} {__('Tour Border Color', '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}`}