| 1 |
import classnames from 'classnames/dedupe'; |
| 2 |
|
| 3 |
import { |
| 4 |
InspectorControls, |
| 5 |
useBlockProps, |
| 6 |
useInnerBlocksProps, |
| 7 |
} from '@wordpress/block-editor'; |
| 8 |
import { createBlock } from '@wordpress/blocks'; |
| 9 |
import { |
| 10 |
__experimentalNumberControl as ExperimentalNumberControl, |
| 11 |
NumberControl as StableNumberControl, |
| 12 |
PanelBody, |
| 13 |
ToggleControl, |
| 14 |
} from '@wordpress/components'; |
| 15 |
import { useDispatch, useSelect } from '@wordpress/data'; |
| 16 |
import { applyFilters } from '@wordpress/hooks'; |
| 17 |
import { __ } from '@wordpress/i18n'; |
| 18 |
|
| 19 |
import EditorStyles from '../../components/editor-styles'; |
| 20 |
import IconPicker from '../../components/icon-picker'; |
| 21 |
import RangeControl from '../../components/range-control'; |
| 22 |
import ToggleGroup from '../../components/toggle-group'; |
| 23 |
|
| 24 |
const NumberControl = StableNumberControl || ExperimentalNumberControl; |
| 25 |
|
| 26 |
const slideBlockName = 'ghostkit/carousel-slide'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Block Edit Class. |
| 30 |
* |
| 31 |
* @param props |
| 32 |
*/ |
| 33 |
export default function BlockEdit(props) { |
| 34 |
const { attributes, setAttributes, clientId } = props; |
| 35 |
|
| 36 |
let { className = '' } = props; |
| 37 |
|
| 38 |
const { |
| 39 |
effect, |
| 40 |
speed, |
| 41 |
autoplay, |
| 42 |
autoplayHoverPause, |
| 43 |
slidesPerView, |
| 44 |
centeredSlides, |
| 45 |
loop, |
| 46 |
freeScroll, |
| 47 |
fadeEdges, |
| 48 |
fadeEdgesSize, |
| 49 |
showArrows, |
| 50 |
arrowPrevIcon, |
| 51 |
arrowNextIcon, |
| 52 |
showBullets, |
| 53 |
dynamicBullets, |
| 54 |
gap, |
| 55 |
} = attributes; |
| 56 |
|
| 57 |
const { getBlocks, slidesCount, block } = useSelect((select) => { |
| 58 |
const blockEditorData = select('core/block-editor'); |
| 59 |
|
| 60 |
return { |
| 61 |
getBlocks: blockEditorData.getBlocks, |
| 62 |
slidesCount: blockEditorData.getBlockCount(clientId), |
| 63 |
block: blockEditorData.getBlock(clientId), |
| 64 |
}; |
| 65 |
}); |
| 66 |
|
| 67 |
const { removeBlock, replaceInnerBlocks } = |
| 68 |
useDispatch('core/block-editor'); |
| 69 |
|
| 70 |
/** |
| 71 |
* Updates the slides count |
| 72 |
* |
| 73 |
* @param {number} newSlidesCount New slides count. |
| 74 |
*/ |
| 75 |
const updateSlidesCount = (newSlidesCount) => { |
| 76 |
// Remove slider block. |
| 77 |
if (newSlidesCount < 1) { |
| 78 |
removeBlock(block.clientId); |
| 79 |
|
| 80 |
// Add new slides. |
| 81 |
} else if (newSlidesCount > slidesCount) { |
| 82 |
const newCount = newSlidesCount - slidesCount; |
| 83 |
const newInnerBlocks = [...getBlocks(block.clientId)]; |
| 84 |
|
| 85 |
for (let i = 1; i <= newCount; i += 1) { |
| 86 |
newInnerBlocks.push(createBlock(slideBlockName, { size: 3 })); |
| 87 |
} |
| 88 |
|
| 89 |
replaceInnerBlocks(block.clientId, newInnerBlocks, false); |
| 90 |
|
| 91 |
// Remove slides. |
| 92 |
} else if (newSlidesCount < slidesCount) { |
| 93 |
const newInnerBlocks = [...getBlocks(block.clientId)]; |
| 94 |
newInnerBlocks.splice(newSlidesCount, slidesCount - newSlidesCount); |
| 95 |
|
| 96 |
replaceInnerBlocks(block.clientId, newInnerBlocks, false); |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
className = classnames( |
| 101 |
className, |
| 102 |
'ghostkit-carousel', |
| 103 |
fadeEdges && 'ghostkit-carousel-fade-edges' |
| 104 |
); |
| 105 |
|
| 106 |
className = applyFilters('ghostkit.editor.className', className, props); |
| 107 |
|
| 108 |
const blockProps = useBlockProps({ className }); |
| 109 |
const innerBlockProps = useInnerBlocksProps( |
| 110 |
{ className: 'ghostkit-carousel-items' }, |
| 111 |
{ |
| 112 |
template: [[slideBlockName], [slideBlockName], [slideBlockName]], |
| 113 |
allowedBlocks: [slideBlockName], |
| 114 |
templateLock: false, |
| 115 |
orientation: 'horizontal', |
| 116 |
} |
| 117 |
); |
| 118 |
|
| 119 |
return ( |
| 120 |
<> |
| 121 |
<InspectorControls> |
| 122 |
<PanelBody title={__('Settings', 'ghostkit')}> |
| 123 |
<RangeControl |
| 124 |
label={__('Slides', 'ghostkit')} |
| 125 |
value={slidesCount} |
| 126 |
onChange={updateSlidesCount} |
| 127 |
min={2} |
| 128 |
max={20} |
| 129 |
allowCustomMax |
| 130 |
/> |
| 131 |
{effect !== 'fade' ? ( |
| 132 |
<> |
| 133 |
<RangeControl |
| 134 |
label={__('Slides per view', 'ghostkit')} |
| 135 |
value={slidesPerView} |
| 136 |
onChange={(value) => |
| 137 |
setAttributes({ slidesPerView: value }) |
| 138 |
} |
| 139 |
min={1} |
| 140 |
max={8} |
| 141 |
allowCustomMax |
| 142 |
/> |
| 143 |
<RangeControl |
| 144 |
label={__('Gap', 'ghostkit')} |
| 145 |
value={gap} |
| 146 |
onChange={(value) => |
| 147 |
setAttributes({ gap: value }) |
| 148 |
} |
| 149 |
min={0} |
| 150 |
max={60} |
| 151 |
allowCustomMax |
| 152 |
/> |
| 153 |
</> |
| 154 |
) : null} |
| 155 |
<ToggleGroup |
| 156 |
label={__('Effect', 'ghostkit')} |
| 157 |
value={effect} |
| 158 |
options={[ |
| 159 |
{ |
| 160 |
value: 'slide', |
| 161 |
label: __('Slide', 'ghostkit'), |
| 162 |
}, |
| 163 |
{ |
| 164 |
value: 'coverflow', |
| 165 |
label: __('Coverflow', 'ghostkit'), |
| 166 |
}, |
| 167 |
{ |
| 168 |
value: 'fade', |
| 169 |
label: __('Fade', 'ghostkit'), |
| 170 |
}, |
| 171 |
]} |
| 172 |
onChange={(value) => { |
| 173 |
setAttributes({ effect: value }); |
| 174 |
}} |
| 175 |
isBlock |
| 176 |
/> |
| 177 |
|
| 178 |
<div |
| 179 |
style={{ |
| 180 |
borderTop: '1px solid #E0E0E0', |
| 181 |
marginBottom: '16px', |
| 182 |
}} |
| 183 |
/> |
| 184 |
|
| 185 |
<RangeControl |
| 186 |
label={__('Speed (seconds)', 'ghostkit')} |
| 187 |
suffix="s " |
| 188 |
value={speed} |
| 189 |
onChange={(value) => setAttributes({ speed: value })} |
| 190 |
min={0} |
| 191 |
max={10} |
| 192 |
step={0.1} |
| 193 |
allowCustomMax |
| 194 |
/> |
| 195 |
<RangeControl |
| 196 |
label={__('Autoplay (seconds)', 'ghostkit')} |
| 197 |
value={autoplay} |
| 198 |
onChange={(value) => setAttributes({ autoplay: value })} |
| 199 |
min={0} |
| 200 |
max={20} |
| 201 |
step={0.3} |
| 202 |
allowCustomMax |
| 203 |
/> |
| 204 |
{autoplay ? ( |
| 205 |
<ToggleControl |
| 206 |
label={__( |
| 207 |
'Pause autoplay on mouse over', |
| 208 |
'ghostkit' |
| 209 |
)} |
| 210 |
checked={!!autoplayHoverPause} |
| 211 |
onChange={(val) => |
| 212 |
setAttributes({ autoplayHoverPause: val }) |
| 213 |
} |
| 214 |
/> |
| 215 |
) : null} |
| 216 |
|
| 217 |
<div |
| 218 |
style={{ |
| 219 |
borderTop: '1px solid #E0E0E0', |
| 220 |
marginBottom: '16px', |
| 221 |
}} |
| 222 |
/> |
| 223 |
|
| 224 |
<ToggleControl |
| 225 |
label={__('Centered Slides', 'ghostkit')} |
| 226 |
checked={!!centeredSlides} |
| 227 |
onChange={(val) => |
| 228 |
setAttributes({ centeredSlides: val }) |
| 229 |
} |
| 230 |
/> |
| 231 |
<ToggleControl |
| 232 |
label={__('Loop', 'ghostkit')} |
| 233 |
checked={!!loop} |
| 234 |
onChange={(val) => setAttributes({ loop: val })} |
| 235 |
/> |
| 236 |
<ToggleControl |
| 237 |
label={__('Free Scroll', 'ghostkit')} |
| 238 |
checked={!!freeScroll} |
| 239 |
onChange={(val) => setAttributes({ freeScroll: val })} |
| 240 |
/> |
| 241 |
<ToggleControl |
| 242 |
label={__('Fade Edges', 'ghostkit')} |
| 243 |
checked={!!fadeEdges} |
| 244 |
onChange={(val) => setAttributes({ fadeEdges: val })} |
| 245 |
/> |
| 246 |
{fadeEdges && ( |
| 247 |
<NumberControl |
| 248 |
label={__('Fade Edges Size', 'ghostkit')} |
| 249 |
suffix="% " |
| 250 |
value={fadeEdgesSize} |
| 251 |
onChange={(val) => |
| 252 |
setAttributes({ |
| 253 |
fadeEdgesSize: parseFloat(val), |
| 254 |
}) |
| 255 |
} |
| 256 |
labelPosition="edge" |
| 257 |
__unstableInputWidth="100px" |
| 258 |
disableUnits |
| 259 |
min={0} |
| 260 |
max={50} |
| 261 |
/> |
| 262 |
)} |
| 263 |
</PanelBody> |
| 264 |
<PanelBody title={__('Arrow', 'ghostkit')}> |
| 265 |
<ToggleControl |
| 266 |
label={__('Show', 'ghostkit')} |
| 267 |
checked={!!showArrows} |
| 268 |
onChange={(val) => setAttributes({ showArrows: val })} |
| 269 |
/> |
| 270 |
{showArrows ? ( |
| 271 |
<> |
| 272 |
<IconPicker |
| 273 |
label={__('Prev icon', 'ghostkit')} |
| 274 |
value={arrowPrevIcon} |
| 275 |
onChange={(value) => |
| 276 |
setAttributes({ arrowPrevIcon: value }) |
| 277 |
} |
| 278 |
insideInspector |
| 279 |
/> |
| 280 |
<IconPicker |
| 281 |
label={__('Next icon', 'ghostkit')} |
| 282 |
value={arrowNextIcon} |
| 283 |
onChange={(value) => |
| 284 |
setAttributes({ arrowNextIcon: value }) |
| 285 |
} |
| 286 |
insideInspector |
| 287 |
/> |
| 288 |
</> |
| 289 |
) : null} |
| 290 |
</PanelBody> |
| 291 |
<PanelBody title={__('Bullets', 'ghostkit')}> |
| 292 |
<ToggleControl |
| 293 |
label={__('Show', 'ghostkit')} |
| 294 |
checked={!!showBullets} |
| 295 |
onChange={(val) => setAttributes({ showBullets: val })} |
| 296 |
/> |
| 297 |
{showBullets ? ( |
| 298 |
<ToggleControl |
| 299 |
label={__('Dynamic', 'ghostkit')} |
| 300 |
checked={!!dynamicBullets} |
| 301 |
onChange={(val) => |
| 302 |
setAttributes({ dynamicBullets: val }) |
| 303 |
} |
| 304 |
/> |
| 305 |
) : null} |
| 306 |
</PanelBody> |
| 307 |
</InspectorControls> |
| 308 |
<div {...blockProps}> |
| 309 |
<div className="block-editor-inner-blocks"> |
| 310 |
<div {...innerBlockProps} /> |
| 311 |
</div> |
| 312 |
</div> |
| 313 |
<EditorStyles |
| 314 |
styles={` |
| 315 |
[data-block="${props.clientId}"] > .ghostkit-carousel { |
| 316 |
--gkt-carousel-gap: ${gap}px; |
| 317 |
--gkt-carousel-slides-per-view: ${ |
| 318 |
effect === 'fade' ? 1 : slidesPerView |
| 319 |
}; |
| 320 |
} |
| 321 |
`} |
| 322 |
/> |
| 323 |
</> |
| 324 |
); |
| 325 |
} |
| 326 |
|