/* eslint-disable react-hooks/exhaustive-deps */ /* eslint-disable import/no-extraneous-dependencies */ /* eslint-disable camelcase */ /* eslint-disable no-undef */ /** * WordPress dependencies */ import { BlockControls, RichText, useBlockProps } from '@wordpress/block-editor'; import classnames from 'classnames'; import { cloneDeep } from 'lodash'; const { Fragment, RawHTML, useEffect, useRef } = wp.element; const { ToolbarGroup, ToolbarButton } = wp.components; const { dispatch } = wp.data; const { __ } = wp.i18n; // editor style import './editor.scss'; /** * Internal dependencies */ import * as Constants from './constants'; import Inspector from './inspector'; const { DisplayIcon, generateRangeStyles, SidebarOpener, DynamicTag } = window?.gutSliderModules; const { COLUMNS, COLUMNS_GAP } = Constants; const { CE_STYLES } = window.gutSliderModules.GlobalConstants; // import style import Style from './style'; /** * Edit function */ export default function Edit({ attributes, setAttributes, clientId }) { const { uniqueId, preview, //visible elements showImage, showSubTitle, showTitle, showDescription, showButton, sliderType, slideItems, addNewSlide, subtitleTag, titleTag, descriptionTag, titleAnimation, subtitleAnimation, descAnimation, btnAnimation, sbtnAnimation, infiniteLoop, autoplay, autoplayDelay, stopOnLastSlide, reverseDirection, pauseOnHover, speed, centerMode, showNavigation, navContainerPosition, navPosition, customNavIcon, navIconSource, navPrevIcon, navNextIcon, customPrevSVG, customNextSVG, showPagination, paginationType, dynamicBullets, pagColor, activePagColor, keyboardControl, mousewheelControl, slideEffect, carouselEffect, creativeEffectStyle, enableGlobalLink, // custom navigation enableRemoteNav, remotePrevSelector, remoteNextSelector } = attributes; // Unique ID useEffect(() => { setAttributes({ uniqueId: 'gutslider-' + clientId.slice(0, 8) }); // add default slide items if (slideItems.length === 0) { setAttributes({ slideItems: [ { id: 0, panelTitle: 'Gutslider Item', subtitle: 'Gutslider Block', title: '#1 Best Gutenberg Slider Block', description: 'Gutslider is a Gutenberg Slider Block that is easy to use and has many features. It is a great alternative to other slider blocks that are not as easy to use and have less features.', bgType: 'classic', bgColor: '#efefef', bgGradient: 'linear-gradient(135deg,#12c2e9 0%,#c471ed 50%,#f64f59 100%)', bgVideo: {}, enableOverlay: false, overlayType: 'classic', overlayColor: '#000000', overlayGradient: 'linear-gradient(135deg,#12c2e9 0%,#c471ed 50%,#f64f59 100%)', overlayOpacity: 0.5, image: { id: '', url: '', alt: 'Gutslider Logo' }, photo: {}, photoSize: 'thumbnail', focusPoint: { x: 0.5, y: 0.5 }, btnLabel: 'Get Gutslider', btnLinks: { url: 'https://gutenbergkits.com', openInNewTab: true, addNoFollow: false, addSponsored: false }, showSbtnLinks: false, sbtnLabel: '', sbtnLinks: { url: '', openInNewTab: true, addNoFollow: false, addSponsored: false }, // Global link globalLink: { url: '', openInNewTab: false, addNoFollow: false, addSponsored: false }, enableResponsiveBg: false, DesktopBg: { url: '', id: '', alt: '' }, TabletBg: { url: '', id: '', alt: '' }, MobileBg: { url: '', id: '', alt: '' }, DesktopFocalPoint: { x: 0.5, y: 0.5 }, TabletFocalPoint: { x: 0.5, y: 0.5 }, MobileFocalPoint: { x: 0.5, y: 0.5 } }, { id: 1, panelTitle: 'Gutslider Item #2', subtitle: 'Custom Content Slider', title: '#2 Easily Build Custom Slider', description: 'Gutslider is a Gutenberg Slider Block that is easy to use and has many features. It is a great alternative to other slider blocks that are not as easy to use and have less features.', bgType: 'classic', bgColor: '#efefef', bgGradient: 'linear-gradient(135deg,#12c2e9 0%,#c471ed 50%,#f64f59 100%)', bgVideo: {}, enableOverlay: false, overlayType: 'classic', overlayColor: '#000000', overlayGradient: 'linear-gradient(135deg,#12c2e9 0%,#c471ed 50%,#f64f59 100%)', overlayOpacity: 0.5, image: { id: '', url: '', alt: 'Gutslider Logo' }, photo: {}, photoSize: 'thumbnail', focusPoint: { x: 0.5, y: 0.5 }, btnLabel: 'Get Gutslider', btnLinks: { url: 'https://gutenbergkits.com', openInNewTab: true, addNoFollow: false, addSponsored: false }, showSbtnLinks: false, sbtnLabel: '', sbtnLinks: { url: '', openInNewTab: true, addNoFollow: false, addSponsored: false }, // Global link globalLink: { url: '', openInNewTab: false, addNoFollow: false, addSponsored: false }, enableResponsiveBg: false, DesktopBg: { url: '', id: '', alt: '' }, TabletBg: { url: '', id: '', alt: '' }, MobileBg: { url: '', id: '', alt: '' }, DesktopFocalPoint: { x: 0.5, y: 0.5 }, TabletFocalPoint: { x: 0.5, y: 0.5 }, MobileFocalPoint: { x: 0.5, y: 0.5 } }, { id: 2, panelTitle: 'Gutslider Item #3', subtitle: 'Custom Content Slider', title: '#3 Easily Build Custom Slider', description: 'Gutslider is a Gutenberg Slider Block that is easy to use and has many features. It is a great alternative to other slider blocks that are not as easy to use and have less features.', bgType: 'classic', bgColor: '#efefef', bgGradient: 'linear-gradient(135deg,#12c2e9 0%,#c471ed 50%,#f64f59 100%)', bgVideo: {}, enableOverlay: false, overlayType: 'classic', overlayColor: '#000000', overlayGradient: 'linear-gradient(135deg,#12c2e9 0%,#c471ed 50%,#f64f59 100%)', overlayOpacity: 0.5, image: { id: '', url: '', alt: 'Gutslider Logo' }, photo: {}, photoSize: 'thumbnail', focusPoint: { x: 0.5, y: 0.5 }, btnLabel: 'Get Gutslider', btnLinks: { url: 'https://gutenbergkits.com', openInNewTab: true, addNoFollow: false, addSponsored: false }, showSbtnLinks: false, sbtnLabel: '', sbtnLinks: { url: '', openInNewTab: true, addNoFollow: false, addSponsored: false }, // Global link globalLink: { url: '', openInNewTab: false, addNoFollow: false, addSponsored: false }, enableResponsiveBg: false, DesktopBg: { url: '', id: '', alt: '' }, TabletBg: { url: '', id: '', alt: '' }, MobileBg: { url: '', id: '', alt: '' }, DesktopFocalPoint: { x: 0.5, y: 0.5 }, TabletFocalPoint: { x: 0.5, y: 0.5 }, MobileFocalPoint: { x: 0.5, y: 0.5 } } ] }); } }, []); // block preview if (preview) { return (
content slider preview
); } // Block Props const blockProps = useBlockProps({ className: classnames(uniqueId, navContainerPosition, navPosition) }); // Slider Columns const { deskStyle: deskSlideColumns, tabStyle: tabSlideColumns, mobStyle: mobSlideColumns } = generateRangeStyles({ controlName: COLUMNS, attributes, noProperty: true, noUnits: true }); const { deskStyle: deskColumnsGap, tabStyle: tabColumnsGap, mobStyle: mobColumnsGap } = generateRangeStyles({ controlName: COLUMNS_GAP, attributes, noProperty: true, noUnits: true }); // Slider breakpoints const breakPoints = { breakpoints: { 320: { slidesPerView: mobSlideColumns || 1, spaceBetween: parseInt(mobColumnsGap) || 0 }, 768: { slidesPerView: tabSlideColumns || 2, spaceBetween: parseInt(tabColumnsGap) || 15 }, 1025: { slidesPerView: deskSlideColumns || 2, spaceBetween: parseInt(deskColumnsGap) || 15 } } }; const sliderBreakPoints = sliderType === 'carousel' ? breakPoints : {}; // swiper initialize const swiperRef = useRef(null); if (swiperRef.current) { swiperRef.current.addEventListener('click', function () { dispatch('core/block-editor').selectBlock(clientId); dispatch('core/edit-post')?.openGeneralSidebar('edit-post/block'); }); } // swiper init function const gutSliderInit = function (sliderE, options) { if (sliderE?.swiper) { sliderE?.swiper.destroy(); } new Swiper(sliderE, options); if (enableRemoteNav && remotePrevSelector && remoteNextSelector) { const remotePrev = document.querySelector(`.${remotePrevSelector}`); const remoteNext = document.querySelector(`.${remoteNextSelector}`); if (remotePrev) { remotePrev.addEventListener('click', () => { sliderE.swiper.slidePrev(); }); } if (remoteNext) { remoteNext.addEventListener('click', () => { sliderE.swiper.slideNext(); }); } } }; //slider options useEffect(() => { const options = { slidesPerView: 1, spaceBetween: 0, loop: infiniteLoop, autoplay: autoplay ? { delay: autoplayDelay * 100 || 10 * 100, pauseOnMouseEnter: pauseOnHover, stopOnLastSlide, reverseDirection } : false, speed: speed * 100 || 8 * 100, effect: sliderType === 'carousel' ? carouselEffect : slideEffect, creativeEffect: CE_STYLES[creativeEffectStyle], keyboard: { enabled: keyboardControl }, mousewheel: mousewheelControl, ...(centerMode && { centeredSlides: true }), ...sliderBreakPoints }; // set options const setOptions = { ...options, navigation: showNavigation && !enableRemoteNav ? { nextEl: customNavIcon ? `.${uniqueId} .gutslider-next` : `.${uniqueId} .swiper-button-next`, prevEl: customNavIcon ? `.${uniqueId} .gutslider-prev` : `.${uniqueId} .swiper-button-prev` } : false, pagination: showPagination ? { el: `.${uniqueId} .swiper-pagination`, type: paginationType, dynamicBullets, clickable: true } : false }; setAttributes({ sliderOptions: setOptions }); if (!swiperRef.current) { return; } // pagination const pagination = swiperRef.current?.querySelector('.swiper-pagination'); const refPath = navContainerPosition === 'nav_outside' ? swiperRef.current?.parentNode : swiperRef.current; // navigation const prevNav = customNavIcon ? refPath?.querySelector('.gutslider-prev') : refPath?.querySelector('.swiper-button-prev'); const nextNav = customNavIcon ? refPath?.querySelector('.gutslider-next') : refPath?.querySelector('.swiper-button-next'); gutSliderInit(swiperRef.current, { ...options, navigation: showNavigation && !enableRemoteNav ? { nextEl: nextNav, prevEl: prevNav } : false, pagination: showPagination ? { el: pagination, type: paginationType, dynamicBullets, clickable: true } : false }); }, [ infiniteLoop, autoplay, autoplayDelay, pauseOnHover, stopOnLastSlide, reverseDirection, speed, centerMode, showNavigation, showPagination, paginationType, dynamicBullets, pagColor, activePagColor, slideEffect, carouselEffect, creativeEffectStyle, addNewSlide, sliderType, deskSlideColumns, tabSlideColumns, mobSlideColumns, deskColumnsGap, tabColumnsGap, mobColumnsGap, keyboardControl, mousewheelControl, customNavIcon, navContainerPosition, enableGlobalLink, slideItems, enableRemoteNav, remotePrevSelector, remoteNextSelector, uniqueId ]); // slider items deep copy const sliderItems = cloneDeep(slideItems); return (