frontblocks-gallery-option.js
10 months ago
frontblocks-gallery-option.jsx
3 months ago
frontblocks-gallery.css
11 months ago
frontblocks-gallery.js
10 months ago
masonry.min.js
10 months ago
frontblocks-gallery-option.jsx
108 lines
| 1 | "use strict"; |
| 2 | |
| 3 | // Add custom controls to the Gallery block |
| 4 | const { addFilter } = wp.hooks; |
| 5 | const { Fragment, useEffect } = wp.element; |
| 6 | const { InspectorControls } = wp.blockEditor; |
| 7 | const { |
| 8 | SelectControl, |
| 9 | PanelBody, |
| 10 | ToggleControl, |
| 11 | RangeControl |
| 12 | } = wp.components; |
| 13 | const { __ } = wp.i18n; |
| 14 | const { createHigherOrderComponent } = wp.compose; |
| 15 | |
| 16 | function addCustomGalleryPanel(BlockEdit) { |
| 17 | return function (props) { |
| 18 | if (props.name !== 'core/gallery') { |
| 19 | return <BlockEdit {...props} />; |
| 20 | } |
| 21 | |
| 22 | const { |
| 23 | frblGalleryLayout = 'grid', |
| 24 | frblGutterSize = 20, |
| 25 | frblEnableLightbox = false |
| 26 | } = props.attributes; |
| 27 | |
| 28 | // Apply dynamic styles in the editor. |
| 29 | useEffect(() => { |
| 30 | const blockElement = document.querySelector(`[data-block="${props.clientId}"] .wp-block-gallery`); |
| 31 | |
| 32 | if (blockElement) { |
| 33 | // Remove previous layout classes. |
| 34 | blockElement.classList.remove('frontblocks-gallery-grid', 'frontblocks-gallery-masonry'); |
| 35 | |
| 36 | // Add current layout class. |
| 37 | if (frblGalleryLayout === 'masonry') { |
| 38 | blockElement.classList.add('frontblocks-gallery-masonry'); |
| 39 | } else { |
| 40 | blockElement.classList.add('frontblocks-gallery-grid'); |
| 41 | } |
| 42 | |
| 43 | // Set gutter size as CSS variable. |
| 44 | blockElement.style.setProperty('--frontblocks-gutter', frblGutterSize + 'px'); |
| 45 | } |
| 46 | }, [frblGalleryLayout, frblGutterSize, props.clientId]); |
| 47 | |
| 48 | return ( |
| 49 | <Fragment> |
| 50 | <BlockEdit {...props} /> |
| 51 | <InspectorControls> |
| 52 | <PanelBody |
| 53 | title={__('FrontBlocks Gallery Settings', 'frontblocks')} |
| 54 | initialOpen={true} |
| 55 | > |
| 56 | <SelectControl |
| 57 | label={__('Gallery Layout', 'frontblocks')} |
| 58 | value={frblGalleryLayout} |
| 59 | options={[ |
| 60 | { |
| 61 | label: __('Grid', 'frontblocks'), |
| 62 | value: 'grid' |
| 63 | }, |
| 64 | { |
| 65 | label: __('Masonry', 'frontblocks'), |
| 66 | value: 'masonry' |
| 67 | } |
| 68 | ]} |
| 69 | onChange={(value) => { |
| 70 | props.setAttributes({ |
| 71 | frblGalleryLayout: value |
| 72 | }); |
| 73 | }} |
| 74 | help={__('Choose between grid or masonry layout for your gallery.', 'frontblocks')} |
| 75 | /> |
| 76 | |
| 77 | <RangeControl |
| 78 | label={__('Gutter Size (px)', 'frontblocks')} |
| 79 | value={frblGutterSize} |
| 80 | onChange={(value) => { |
| 81 | props.setAttributes({ |
| 82 | frblGutterSize: value |
| 83 | }); |
| 84 | }} |
| 85 | min={0} |
| 86 | max={50} |
| 87 | step={5} |
| 88 | help={__('Set the spacing between gallery items.', 'frontblocks')} |
| 89 | /> |
| 90 | |
| 91 | <ToggleControl |
| 92 | label={__('Enable Lightbox', 'frontblocks')} |
| 93 | checked={frblEnableLightbox} |
| 94 | onChange={(value) => { |
| 95 | props.setAttributes({ |
| 96 | frblEnableLightbox: value |
| 97 | }); |
| 98 | }} |
| 99 | help={__('Enable lightbox functionality for gallery images.', 'frontblocks')} |
| 100 | /> |
| 101 | </PanelBody> |
| 102 | </InspectorControls> |
| 103 | </Fragment> |
| 104 | ); |
| 105 | }; |
| 106 | } |
| 107 | |
| 108 | addFilter('editor.BlockEdit', 'frontblocks/gallery-panel', addCustomGalleryPanel); |