| 1 |
import { useBlockProps } from "@wordpress/block-editor"; |
| 2 |
import { useEffect, useMemo, useRef, useState } from "@wordpress/element"; |
| 3 |
import InspectorControl from "../../components/inspectorControls/inspectorControls"; |
| 4 |
import { panelBodyTitleIcon, panelBodyRightIcon } from "../../icons/icons"; |
| 5 |
import Inspector from "./inspect"; |
| 6 |
import dynamicCssFn from "./dynamicCss"; |
| 7 |
import { inArray, useDeviceType, paginationDotType } from "../../controls/controls"; |
| 8 |
import { EditorWrapper } from "../shared/wrapper"; |
| 9 |
import Render from "./render"; |
| 10 |
import { TogglePanelBodyProvider } from "../../context"; |
| 11 |
import { compose } from "@wordpress/compose"; |
| 12 |
import addInitialAttr from "../shared/addInitialAttr"; |
| 13 |
|
| 14 |
const CarouselTwoEdit = ({ attributes, setAttributes, clientId, isSelected }) => { |
| 15 |
const [posts, setPosts] = useState([]); |
| 16 |
attributes.clientId = clientId; |
| 17 |
|
| 18 |
const { |
| 19 |
blockName, |
| 20 |
carouselPaginationStyle, |
| 21 |
carouselStyle, |
| 22 |
carouselColumn, |
| 23 |
carouselAutoPlay, |
| 24 |
carouselAutoPlayDelay, |
| 25 |
slideToScroll, |
| 26 |
carouselPauseOnHover, |
| 27 |
carouselAnimationEffect, |
| 28 |
carouselTabKeyNav, |
| 29 |
carouselMouseWheelControl, |
| 30 |
carouselNavArrow, |
| 31 |
carouselPaginationDot, |
| 32 |
carouselAdaptiveHeight, |
| 33 |
contentAreaBg, |
| 34 |
imageOverlayType, |
| 35 |
equalHeightEnable, |
| 36 |
infiniteLoop, |
| 37 |
fontListsEditPage, |
| 38 |
contentAreaPadding, |
| 39 |
offset, |
| 40 |
carouselGap, |
| 41 |
imageHeight, |
| 42 |
customCss, |
| 43 |
carouselSpeed, |
| 44 |
carouselFreeScrollMode, |
| 45 |
carouselTickerSpeed, |
| 46 |
carouselDirection, |
| 47 |
uniqueId, |
| 48 |
carouselHeight, |
| 49 |
catTabCategoryBorder, |
| 50 |
imageOverlayCustomColor, |
| 51 |
partialViewSlide, |
| 52 |
} = attributes; |
| 53 |
|
| 54 |
const deviceType = useDeviceType(); |
| 55 |
const blockProps = useBlockProps(); |
| 56 |
|
| 57 |
useEffect(() => { |
| 58 |
if (!blockName) { |
| 59 |
setAttributes({ |
| 60 |
contentHorizontalPosition: "left", |
| 61 |
contentAreaBg: { |
| 62 |
...contentAreaBg, |
| 63 |
color: { |
| 64 |
...contentAreaBg.color, |
| 65 |
solidColor: "transparent", |
| 66 |
}, |
| 67 |
}, |
| 68 |
showReadMoreButton: false, |
| 69 |
imagePosition: "background", |
| 70 |
imageOverlayColor: "default", |
| 71 |
contentVerticalPosition: "bottom", |
| 72 |
imageHeight: { |
| 73 |
...imageHeight, |
| 74 |
device: { |
| 75 |
...imageHeight?.device, |
| 76 |
Desktop: 244, |
| 77 |
}, |
| 78 |
}, |
| 79 |
|
| 80 |
carouselHeight: { |
| 81 |
...carouselHeight, |
| 82 |
device: { |
| 83 |
...carouselHeight.device, |
| 84 |
Desktop: 244, |
| 85 |
Tablet: 210, |
| 86 |
Mobile: 210, |
| 87 |
}, |
| 88 |
}, |
| 89 |
contentAreaPadding: { |
| 90 |
...contentAreaPadding, |
| 91 |
device: { |
| 92 |
...contentAreaPadding.device, |
| 93 |
Desktop: { |
| 94 |
top: 0, |
| 95 |
right: 20, |
| 96 |
bottom: 20, |
| 97 |
left: 20, |
| 98 |
}, |
| 99 |
}, |
| 100 |
}, |
| 101 |
|
| 102 |
catTabCategoryBorder: { |
| 103 |
...catTabCategoryBorder, |
| 104 |
style: "solid", |
| 105 |
}, |
| 106 |
|
| 107 |
imageOverlayCustomColor: { |
| 108 |
...imageOverlayCustomColor, |
| 109 |
color: { |
| 110 |
...imageOverlayCustomColor.color, |
| 111 |
solidColor: "#00000080", |
| 112 |
}, |
| 113 |
}, |
| 114 |
}); |
| 115 |
} |
| 116 |
}, []); |
| 117 |
|
| 118 |
const [carouselAutoPlaySpeed, setCarouselAutoPlaySpeed] = useState({ |
| 119 |
value: 2000, |
| 120 |
unit: "ms", |
| 121 |
}); |
| 122 |
|
| 123 |
const [swiperRand, setSwiperRand] = useState(1); |
| 124 |
const firstLoad = useRef(true); |
| 125 |
|
| 126 |
useEffect(() => { |
| 127 |
setAttributes({ currentScreen: deviceType }); |
| 128 |
}, [deviceType]); |
| 129 |
|
| 130 |
useEffect(() => { |
| 131 |
if (carouselAutoPlayDelay.unit == "s") { |
| 132 |
setCarouselAutoPlaySpeed(carouselAutoPlayDelay.value * 1000); |
| 133 |
} else { |
| 134 |
setCarouselAutoPlaySpeed(carouselAutoPlayDelay); |
| 135 |
} |
| 136 |
}, [carouselAutoPlayDelay]); |
| 137 |
|
| 138 |
const blockStyling = useMemo(() => dynamicCssFn(attributes, "frontend", deviceType), [attributes, deviceType]); |
| 139 |
|
| 140 |
useEffect(() => { |
| 141 |
if (firstLoad.current) { |
| 142 |
firstLoad.current = false; |
| 143 |
} else if (inArray(["ticker", "multi_row"], carouselStyle)) { |
| 144 |
setAttributes({ |
| 145 |
carouselAdaptiveHeight: false, |
| 146 |
}); |
| 147 |
} |
| 148 |
}, [blockName, carouselPaginationStyle, carouselStyle]); |
| 149 |
|
| 150 |
useEffect(() => { |
| 151 |
setSwiperRand(Math.floor(Math.random() * 10)); |
| 152 |
}, [ |
| 153 |
carouselPaginationStyle, |
| 154 |
carouselAnimationEffect, |
| 155 |
carouselTabKeyNav, |
| 156 |
carouselNavArrow, |
| 157 |
carouselPaginationDot, |
| 158 |
carouselAutoPlay, |
| 159 |
carouselPauseOnHover, |
| 160 |
carouselMouseWheelControl, |
| 161 |
carouselAdaptiveHeight, |
| 162 |
carouselColumn, |
| 163 |
slideToScroll, |
| 164 |
imageOverlayType, |
| 165 |
equalHeightEnable, |
| 166 |
infiniteLoop, |
| 167 |
posts, |
| 168 |
carouselStyle, |
| 169 |
]); |
| 170 |
const desktopGap = carouselGap?.device?.Desktop ? carouselGap?.device?.Desktop.toString() : "0"; |
| 171 |
const tabletGap = carouselGap?.device?.Tablet ? carouselGap?.device?.Tablet.toString() : desktopGap; |
| 172 |
const mobileGap = carouselGap?.device?.Mobile ? carouselGap?.device?.Mobile.toString() : desktopGap; |
| 173 |
const { Desktop: desktopScroll, Tablet: tabletScroll, Mobile: mobileScroll } = slideToScroll.device; |
| 174 |
|
| 175 |
const isPartialView = carouselStyle === "standard" && partialViewSlide === true; |
| 176 |
let swiperOptions = {}; |
| 177 |
if (carouselStyle !== "ticker") { |
| 178 |
swiperOptions = { |
| 179 |
slidesPerView: Number(carouselColumn.device?.[deviceType]) + (isPartialView ? 0.3 : 0), |
| 180 |
|
| 181 |
simulateTouch: true, |
| 182 |
grid: carouselStyle === "multi_row" ? { rows: 2, fill: "row" } : undefined, |
| 183 |
navigation: { |
| 184 |
nextEl: `#${uniqueId} .sp-smart-post-swiper-nav-arrow .btn-next`, |
| 185 |
prevEl: `#${uniqueId} .sp-smart-post-swiper-nav-arrow .btn-prev`, |
| 186 |
enabled: true, |
| 187 |
}, |
| 188 |
grabCursor: true, |
| 189 |
pagination: paginationDotType(null, carouselPaginationStyle, uniqueId), |
| 190 |
scrollbar: |
| 191 |
carouselPaginationStyle === "scrollbar" |
| 192 |
? { |
| 193 |
draggable: true, |
| 194 |
el: ".swiper-scrollbar", |
| 195 |
} |
| 196 |
: undefined, |
| 197 |
loop: |
| 198 |
!infiniteLoop || |
| 199 |
carouselStyle === "multi_row" || |
| 200 |
desktopScroll > 1 || |
| 201 |
tabletScroll > 1 || |
| 202 |
mobileScroll > 1 |
| 203 |
? false |
| 204 |
: true, |
| 205 |
|
| 206 |
slidesPerGroup: slideToScroll.device?.[deviceType] || 1, |
| 207 |
centeredSlides: "center" === carouselStyle || isPartialView ? true : false, |
| 208 |
watchSlidesProgress: true, |
| 209 |
autoplay: carouselAutoPlay |
| 210 |
? { |
| 211 |
delay: carouselAutoPlayDelay?.value, |
| 212 |
disableOnInteraction: false, |
| 213 |
pauseOnMouseEnter: carouselPauseOnHover, |
| 214 |
reverseDirection: carouselDirection === "left_to_right", |
| 215 |
} |
| 216 |
: undefined, |
| 217 |
|
| 218 |
speed: carouselSpeed?.value, |
| 219 |
|
| 220 |
spaceBetween: carouselAnimationEffect === "cube" ? 0 : carouselGap.device?.[deviceType] || 0, |
| 221 |
|
| 222 |
breakpoints: !["fade", "cube", "flip"].includes(carouselAnimationEffect) |
| 223 |
? { |
| 224 |
0: { |
| 225 |
slidesPerView: Number(carouselColumn?.device?.Mobile) + (isPartialView ? 0.3 : 0), |
| 226 |
slidesPerGroup: mobileScroll > 1 ? parseInt(mobileScroll) : parseInt(desktopScroll), |
| 227 |
spaceBetween: mobileGap ? mobileGap.toString() : desktopGap, |
| 228 |
}, |
| 229 |
600: { |
| 230 |
slidesPerView: Number(carouselColumn?.device?.Tablet) + (isPartialView ? 0.3 : 0), |
| 231 |
slidesPerGroup: tabletScroll > 1 ? parseInt(tabletScroll) : parseInt(desktopScroll), |
| 232 |
spaceBetween: tabletGap ? tabletGap.toString() : desktopGap, |
| 233 |
}, |
| 234 |
1024: { |
| 235 |
slidesPerView: Number(carouselColumn?.device?.Desktop) + (isPartialView ? 0.3 : 0), |
| 236 |
slidesPerGroup: parseInt(desktopScroll) || 1, |
| 237 |
spaceBetween: desktopGap.toString() || "0", |
| 238 |
}, |
| 239 |
} |
| 240 |
: undefined, |
| 241 |
effect: carouselAnimationEffect, |
| 242 |
cubeEffect: |
| 243 |
carouselAnimationEffect === "cube" |
| 244 |
? { |
| 245 |
shadow: true, |
| 246 |
slideShadows: true, |
| 247 |
shadowOffset: 20, |
| 248 |
shadowScale: 0.94, |
| 249 |
} |
| 250 |
: undefined, |
| 251 |
coverflowEffect: |
| 252 |
carouselAnimationEffect === "coverflow" |
| 253 |
? { |
| 254 |
rotate: 50, |
| 255 |
stretch: 0, |
| 256 |
depth: 100, |
| 257 |
modifier: 1, |
| 258 |
slideShadows: true, |
| 259 |
} |
| 260 |
: undefined, |
| 261 |
keyboard: carouselTabKeyNav |
| 262 |
? { |
| 263 |
enabled: true, |
| 264 |
onlyInViewport: true, |
| 265 |
} |
| 266 |
: undefined, |
| 267 |
mousewheel: carouselMouseWheelControl || false, |
| 268 |
autoHeight: carouselAdaptiveHeight ? true : false, |
| 269 |
freeMode: carouselFreeScrollMode || false, |
| 270 |
direction: "horizontal", |
| 271 |
}; |
| 272 |
} else { |
| 273 |
swiperOptions = { |
| 274 |
slidesPerView: carouselColumn.device?.[deviceType], |
| 275 |
slidesPerViewMobile: carouselColumn?.device?.Mobile, |
| 276 |
slidesPerViewTable: carouselColumn?.device?.Tablet, |
| 277 |
speed: carouselTickerSpeed?.value, |
| 278 |
spaceBetween: carouselGap.device?.[deviceType] || 0, |
| 279 |
spaceBetweenMobile: mobileGap ? mobileGap.toString() : desktopGap, |
| 280 |
spaceBetweenTablet: tabletGap ? tabletGap.toString() : desktopGap, |
| 281 |
direction: carouselDirection, |
| 282 |
pauseOnHover: carouselPauseOnHover, |
| 283 |
}; |
| 284 |
} |
| 285 |
useEffect(() => { |
| 286 |
setAttributes({ |
| 287 |
carouselData: JSON.stringify(swiperOptions), |
| 288 |
}); |
| 289 |
}, [JSON.stringify(swiperOptions)]); |
| 290 |
|
| 291 |
return ( |
| 292 |
<div {...blockProps}> |
| 293 |
<style> |
| 294 |
{fontListsEditPage} |
| 295 |
{blockStyling} |
| 296 |
{customCss} |
| 297 |
</style> |
| 298 |
<TogglePanelBodyProvider blockName={blockName}> |
| 299 |
<InspectorControl |
| 300 |
TitleIcon={panelBodyTitleIcon} |
| 301 |
RightIcon={panelBodyRightIcon} |
| 302 |
attributes={attributes} |
| 303 |
setAttributes={setAttributes} |
| 304 |
Inspector={Inspector} |
| 305 |
isSelected={isSelected} |
| 306 |
/> |
| 307 |
|
| 308 |
<EditorWrapper setPosts={setPosts} setAttributes={setAttributes}> |
| 309 |
<Render pageType="editor" attributes={attributes} posts={posts} swiperKey={swiperRand} /> |
| 310 |
</EditorWrapper> |
| 311 |
</TogglePanelBodyProvider> |
| 312 |
</div> |
| 313 |
); |
| 314 |
}; |
| 315 |
export default compose(addInitialAttr)(CarouselTwoEdit); |
| 316 |
|