import { __ } from '@wordpress/i18n';
import { useBlockProps, InspectorControls, MediaUpload, MediaUploadCheck } from '@wordpress/block-editor';
import { useState, useEffect } from 'react';
import {
PanelBody,
SelectControl,
Button,
ToggleControl,
TextControl,
__experimentalNumberControl as NumberControl,
__experimentalUnitControl as UnitControl,
TabPanel,
__experimentalBorderControl as BorderControl
} from '@wordpress/components';
import metadata from './block.json';
import './editor.scss';
import LightGallery from 'lightgallery/react';
import 'lightgallery/css/lightgallery.css';
import 'lightgallery/css/lg-zoom.css';
import 'lightgallery/css/lg-thumbnail.css';
import lgThumbnail from 'lightgallery/plugins/thumbnail';
import lgZoom from 'lightgallery/plugins/zoom';
// Check if JetEngine or ACF/SCF are activated and get their gallery field data
const { apiFetch } = wp;
apiFetch({ path: '/custom/v1/get-gallery-supported-plugins-data' })
.then((data) => {
console.log(data); // Log the entire data object
if (data.jetengine_active) {
console.log('Jet Engine is activated');
// Other logic here
}
})
.catch((error) => {
console.error('Error fetching Jet Engine status:', error);
});
const MultiMediaUpload = ({ onSelect }) => {
const [mediaIds, setMediaIds] = useState([]);
const handleSelectMedia = (media) => {
const selectedMedia = media.map((item) => ({
id: item.id,
url: item.url,
alt: item.alt || ''
}));
setMediaIds(selectedMedia);
onSelect(selectedMedia);
};
return (
(
)}
/>
);
};
export default function Edit(props) {
const { attributes, setAttributes } = props;
const {
galleryItems = [],
source,
sourceMetaField,
sourceTable,
sourceColumn,
includePostThumbnail,
initialLayout,
itemsAspectRatio,
gridColumnsLarge,
gridColumnsMedium,
gridColumnsSmall,
gridGapLarge,
gridGapMedium,
gridGapSmall,
download,
downloadSize,
maxInitialItems,
thumbnail,
inlineHeightLarge,
inlineHeightMedium,
inlineHeightSmall,
justifiedRowHeightSmall,
justifiedRowHeightMedium,
justifiedRowHeightLarge,
justifiedLastRow,
thumbnailsBorders,
thumbnailsBordersSelected,
thumbnailsStyle,
thumbnailActiveStyle,
showCaptions,
zoomFromOrigin,
lgSettings
} = attributes;
// Handle Updating LightGallery settings
const [settings, setSettings] = useState(lgSettings);
useEffect(() => {
// Only update state if lgSettings actually change
if (JSON.stringify(settings) !== JSON.stringify(lgSettings)) {
setSettings(lgSettings);
}
}, [lgSettings]); // Runs only when lgSettings change
const [borders, setBorders] = useState(thumbnailsBorders || {
top: { color: '#000000', style: 'solid', width: '1px' },
right: { color: '#000000', style: 'solid', width: '1px' },
bottom: { color: '#000000', style: 'solid', width: '1px' },
left: { color: '#000000', style: 'solid', width: '1px' }
});
const [selectedBorders, setSelectedBorders] = useState(thumbnailsBordersSelected || {
top: { color: '#000000', style: 'solid', width: '1px' },
right: { color: '#000000', style: 'solid', width: '1px' },
bottom: { color: '#000000', style: 'solid', width: '1px' },
left: { color: '#000000', style: 'solid', width: '1px' }
});
const handleSelectMedia = (selectedMedia) => {
setAttributes({ galleryItems: selectedMedia });
};
const handleBorderChange = (newBorders) => {
setBorders(newBorders);
setAttributes({ thumbnailsBorders: newBorders });
};
const handleBorderSelectedChange = (newBorders) => {
setSelectedBorders(newBorders);
setAttributes({ thumbnailsBordersSelected: newBorders });
};
// lightGallery Transistion Styles
const lgTransitionStyles = [
{ label: 'lg-slide', value: 'lg-slide' },
{ label: 'lg-fade', value: 'lg-fade' },
{ label: 'lg-zoom-in', value: 'lg-zoom-in' },
{ label: 'lg-zoom-in-big', value: 'lg-zoom-in-big' },
{ label: 'lg-zoom-out', value: 'lg-zoom-out' },
{ label: 'lg-zoom-out-big', value: 'lg-zoom-out-big' },
{ label: 'lg-zoom-out-in', value: 'lg-zoom-out-in' },
{ label: 'lg-zoom-in-out', value: 'lg-zoom-in-out' },
{ label: 'lg-soft-zoom', value: 'lg-soft-zoom' },
{ label: 'lg-scale-up', value: 'lg-scale-up' },
{ label: 'lg-slide-circular', value: 'lg-slide-circular' },
{ label: 'lg-slide-circular-vertical', value: 'lg-slide-circular-vertical' },
{ label: 'lg-slide-vertical', value: 'lg-slide-vertical' },
{ label: 'lg-slide-vertical-growth', value: 'lg-slide-vertical-growth' },
{ label: 'lg-slide-skew-only', value: 'lg-slide-skew-only' },
{ label: 'lg-slide-skew-only-rev', value: 'lg-slide-skew-only-rev' },
{ label: 'lg-slide-skew-only-y', value: 'lg-slide-skew-only-y' },
{ label: 'lg-slide-skew-only-y-rev', value: 'lg-slide-skew-only-y-rev' },
{ label: 'lg-slide-skew', value: 'lg-slide-skew' },
{ label: 'lg-slide-skew-rev', value: 'lg-slide-skew-rev' },
{ label: 'lg-slide-skew-cross', value: 'lg-slide-skew-cross' },
{ label: 'lg-slide-skew-cross-rev', value: 'lg-slide-skew-cross-rev' },
{ label: 'lg-slide-skew-ver', value: 'lg-slide-skew-ver' },
{ label: 'lg-slide-skew-ver-rev', value: 'lg-slide-skew-ver-rev' },
{ label: 'lg-slide-skew-ver-cross', value: 'lg-slide-skew-ver-cross' },
{ label: 'lg-slide-skew-ver-cross-rev', value: 'lg-slide-skew-ver-cross-rev' },
{ label: 'lg-lollipop', value: 'lg-lollipop' },
{ label: 'lg-lollipop-rev', value: 'lg-lollipop-rev' },
{ label: 'lg-rotate', value: 'lg-rotate' },
{ label: 'lg-rotate-rev', value: 'lg-rotate-rev' },
{ label: 'lg-tube', value: 'lg-tube' }
];
const galleryClasses = `lcp-gallery ${initialLayout}`;
const lgPluginRequirements = [
{ setting: 'zoom', plugin: 'lgZoom' },
{ setting: 'thumbnail', plugin: 'lgThumbnail' },
{ setting: 'rotate', plugin: 'lgRotate' },
{ setting: 'autoplay', plugin: 'lgAutoplay' },
{ setting: 'fullScreen', plugin: 'lgFullScreen' },
{ setting: 'download', plugin: 'lgDownload' },
// Add more settings and corresponding plugins as needed
];
useEffect(() => {
// Initialize plugins array
const updatedPlugins = [];
// Loop through the plugin requirements
lgPluginRequirements.forEach(({ setting, plugin }) => {
// If the setting is enabled, add the corresponding plugin to the plugins array
if (lgSettings[setting]) {
updatedPlugins.push(plugin);
}
});
// Use function-based setAttributes to get the latest lgSettings and update plugins array
setAttributes((prevAttributes) => {
const updatedLgSettings = {
...prevAttributes.lgSettings,
plugins: updatedPlugins, // Updated plugins array
};
// Log the lgSettings after update
console.log("Updated lgSettings:", updatedLgSettings);
return {
...prevAttributes,
lgSettings: updatedLgSettings,
};
});
}, [lgSettings.zoom, lgSettings.thumbnail, lgSettings.rotate, lgSettings.autoplay, lgSettings.fullScreen, lgSettings.download, setAttributes]);
return (
<>
{/* Settings and Style Tabs */}
{({ name }) => {
if (name === 'settings') {
return (
<>
{/* Source Settings */}
setAttributes({ source: newValue })}
options={[
{ value: 'manual', label: __('Manually Select', metadata.textdomain) },
{ value: 'lcp_post_gallery', label: __('Post Gallery', metadata.textdomain) },
{ value: 'meta_field', label: __('Meta field of current post', metadata.textdomain) },
{ value: 'linked_table', label: __('Linked Table', metadata.textdomain) }
]}
/>
{source === 'manual' && }
{source === 'meta_field' && (
setAttributes({ sourceMetaField: newValue })}
/>
)}
{source === 'linked_table' && (
<>
setAttributes({ sourceTable: newValue })}
/>
setAttributes({ sourceColumn: newValue })}
/>
>
)}
{/* Parse an element for gallery items */}
{
setAttributes({
parseElementForItems: value, // Set as a boolean directly
});
}}
/>
{attributes.parseElementForItems && (
setAttributes({ parsedTargetElement: newValue })}
/>
)}
setAttributes({ includePostThumbnail: isChecked })}
checked={includePostThumbnail}
label={__('Include Post Thumbnail (Featured Image)', metadata.textdomain)}
/>
{/* Display Settings */}
setAttributes({ initialLayout: newValue })}
options={[
{ value: 'inline', label: __('Inline', metadata.textdomain) },
{ value: 'grid', label: __('Grid', metadata.textdomain) },
{ value: 'justified', label: __('Justified', metadata.textdomain) },
{ value: 'button', label: __('Button', metadata.textdomain) }
]}
/>
{initialLayout === 'button' && (
setAttributes({ sourceColumn: newValue })}
/>
)
}
setAttributes({ thumbnail: isChecked })}
checked={thumbnail}
label={__('Show Thumbnails', metadata.textdomain)}
/>
{thumbnail && (
<>
setAttributes({ itemsAspectRatio: newValue })}
options={[
{ value: 'bottom', label: __('Bottom', metadata.textdomain) },
{ value: 'right', label: __('Right', metadata.textdomain) },
{ value: 'left', label: __('Left', metadata.textdomain) },
{ value: 'hidden', label: __('Hidden', metadata.textdomain) }
]}
/>
setAttributes({ itemsAspectRatio: newValue })}
options={[
{ value: 'bottom', label: __('Bottom', metadata.textdomain) },
{ value: 'right', label: __('Right', metadata.textdomain) },
{ value: 'left', label: __('Left', metadata.textdomain) },
{ value: 'hidden', label: __('Hidden', metadata.textdomain) }
]}
/>
setAttributes({ itemsAspectRatio: newValue })}
options={[
{ value: 'bottom', label: __('Bottom', metadata.textdomain) },
{ value: 'right', label: __('Right', metadata.textdomain) },
{ value: 'left', label: __('Left', metadata.textdomain) },
{ value: 'hidden', label: __('Hidden', metadata.textdomain) }
]}
/>
>
)}
{initialLayout !== 'inline' && initialLayout !== 'justified' && (
setAttributes({ itemsAspectRatio: newValue })}
options={[
{ value: '1-1', label: __('1:1', metadata.textdomain) },
{ value: '16-9', label: __('16:9', metadata.textdomain) },
{ value: '3-2', label: __('3:2', metadata.textdomain) },
{ value: '3-4', label: __('3:4', metadata.textdomain) },
{ value: '4-5', label: __('4:5', metadata.textdomain) },
{ value: '2-3', label: __('2:3', metadata.textdomain) }
]}
/>
)}
{initialLayout !== 'inline' && (
setAttributes({ maxInitialItems: newValue })}
value={maxInitialItems}
isShiftStepEnabled
shiftStep={1}
min={0}
/>
)}
{/* Gallery Settings */}
{/* Dynamic Settings */}
{
// Update autoplay
setAttributes({
lgSettings: {
...lgSettings,
['dynamic']: value,
}
});
}}
/>
{/* Zoom Settings - Zoom requires lgZoom plugin */}
{
setAttributes({
lgSettings: {
...lgSettings,
zoom: value, // Update the zoom setting
plugins: value
? [...lgSettings.plugins, 'lgZoom'] // Add lgZoom plugin if enabled
: lgSettings.plugins.filter(plugin => plugin !== 'lgZoom'), // Remove lgZoom plugin if disabled
},
});
}}
/>
{/* Fullscreen Settings - fullScreen requires lgFullscreen plugin */}
{
// Update autoplay
setAttributes({
lgSettings: {
...lgSettings,
fullScreen: value, // Update the zoom setting
plugins: value
? [...lgSettings.plugins, 'lgFullScreen'] // Add lgFullScreen plugin if enabled
: lgSettings.plugins.filter(plugin => plugin !== 'lgFullScreen'), // Remove lgFullScreen plugin if disabled
},
});
}}
/>
{/* Sharing Settings - Sharing requires lgShare plugin */}
{
// Update autoplay
setAttributes({
lgSettings: {
...lgSettings,
share: value, // Update the zoom setting
plugins: value
? [...lgSettings.plugins, 'lgShare'] // Add lgShare plugin if enabled
: lgSettings.plugins.filter(plugin => plugin !== 'lgShare'), // Remove lgShare plugin if disabled
},
});
}}
/>
{/* Autoplay Settings - Autoplay requires lgAutoplay plugin */}
{
// Update autoplay
setAttributes({
lgSettings: {
...lgSettings,
autoplay: value, // Update the zoom setting
plugins: value
? [...lgSettings.plugins, 'lgAutoplay'] // Add lgAutoplay plugin if enabled
: lgSettings.plugins.filter(plugin => plugin !== 'lgAutoplay'), // Remove lgAutoplay plugin if disabled
},
});
}}
/>
{/* HASH SETTINGS - Hashrequires lghash plugin */}
{
// Update autoplay
setAttributes({
lgSettings: {
...lgSettings,
hash: value, // Update the zoom setting
plugins: value
? [...lgSettings.plugins, 'lgHash'] // Add lgAutoplay plugin if enabled
: lgSettings.plugins.filter(plugin => plugin !== 'lgHash'), // Remove lgAutoplay plugin if disabled
},
});
}}
/>
{/* Hash requires a unique id in lgSettings.galleryId */}
{lgSettings.hash && (
{
setAttributes({
lgSettings: {
...lgSettings,
galleryId: newValue, // Update the hashValue based on the text input
}
});
}}
/>
)}
{
// Update autoplay
setAttributes({
lgSettings: {
...lgSettings,
zoomFromOrigin: value, // Update the zoomFromOrigin setting
},
});
}}
/>
{/*Captions Settings */}
setAttributes({ showCaptions: isChecked })}
checked={showCaptions}
label={__('Show Captions', metadata.textdomain)}
/>
{/*Download Controls - If download is true, show options to set download size */}
{
// Update autoplay
setAttributes({
lgSettings: {
...lgSettings,
['download']: value,
},
});
}}
/>
{/* Options to set available download size */}
{download && (
setAttributes({ downloadSize: newValue })}
options={[
{ value: 'full', label: __('Full', metadata.textdomain) },
{ value: 'medium', label: __('Medium', metadata.textdomain) },
{ value: 'large', label: __('Large', metadata.textdomain) }
]}
/>
)}
{/* Add the SelectControl for Transition */}
{
// Update autoplay
setAttributes({
lgSettings: {
...lgSettings,
['mode']: value,
}
});
}}
/>
{/* Add the TextControl for specific container to open the gallery in*/}
{initialLayout != "inline"}{
setAttributes({ lgSettings: {
...lgSettings, // spread the current lgSettings object
container: newValue // update only the container property
} })}
/>
}
>
);
}
if (name === 'style') {
return (
{initialLayout === 'inline' && (
<>
setAttributes({ inlineHeightLarge: value })}
value={inlineHeightLarge}
/>
setAttributes({ inlineHeightMedium: value })}
value={inlineHeightMedium}
/>
setAttributes({ inlineHeightSmall: value })}
value={inlineHeightSmall}
/>
>
)}
{initialLayout === 'justified' && (
<>
setAttributes({ justifiedLastRow: newValue })}
options={[
{ value: 'justify', label: __('Justify', metadata.textdomain) },
{ value: 'nojustify', label: __('Unjustified', metadata.textdomain) },
{ value: 'left', label: __('Align Left', metadata.textdomain) },
{ value: 'center', label: __('Align Center', metadata.textdomain) },
{ value: 'right', label: __('Align Right', metadata.textdomain) },
{ value: 'hide', label: __('Hide', metadata.textdomain) }
]}
/>
setAttributes({ justifiedRowHeightLarge: value })}
value={justifiedRowHeightLarge}
/>
setAttributes({ justifiedRowHeightMedium: value })}
value={justifiedRowHeightMedium}
/>
setAttributes({ justifiedRowHeightSmall: value })}
value={justifiedRowHeightSmall}
/>
>
)}
{initialLayout === 'grid' && (
<>
setAttributes({ gridColumnsLarge: newValue })}
value={gridColumnsLarge}
isShiftStepEnabled
shiftStep={1}
min={1}
max={9}
/>
setAttributes({ gridColumnsMedium: newValue })}
value={gridColumnsMedium}
isShiftStepEnabled
shiftStep={1}
min={1}
max={9}
/>
setAttributes({ gridColumnsSmall: newValue })}
value={gridColumnsSmall}
isShiftStepEnabled
shiftStep={1}
min={1}
max={9}
/>
setAttributes({ gridGapLarge: newValue })}
value={gridGapLarge}
isShiftStepEnabled
shiftStep={1}
min={0}
/>
>
)}
{thumbnail && (
<>
setAttributes({ thumbnailsStyle: newValue })}
options={[
{ value: '', label: __('None', metadata.textdomain) },
{ value: 'lg-thumbnail-blur', label: __('Blur', metadata.textdomain) },
{ value: 'lg-thumbnail-grayscale', label: __('Monochrome', metadata.textdomain) }
]}
/>
setAttributes({ thumbnailActiveStyle: newValue })}
options={[
{ value: '', label: __('None', metadata.textdomain) },
{ value: 'lg-thumbnail-active-grayscale', label: __('Monochrome', metadata.textdomain) },
{ value: 'lg-thumbnail-active-blur', label: __('Blur', metadata.textdomain) }
]}
/>
>
)}
);
}
}}
>
);
}