| 1 |
import { useBlockProps } from "@wordpress/block-editor"; |
| 2 |
import { useEffect, useState, useMemo } from "@wordpress/element"; |
| 3 |
import InspectorControl from "../../components/inspectorControls/inspectorControls"; |
| 4 |
import { panelBodyRightIcon, panelBodyTitleIcon } from "../../icons/icons"; |
| 5 |
import { EditorWrapper } from "../shared/wrapper"; |
| 6 |
import dynamicCssFn from "./dynamicCss"; |
| 7 |
import Inspector from "./inspect"; |
| 8 |
import Render from "./render"; |
| 9 |
import { useDeviceType, paginationDotType } from "../../controls/controls"; |
| 10 |
import { TogglePanelBodyProvider } from "../../context"; |
| 11 |
import { cssDependency } from "../shared/cssDependency"; |
| 12 |
import { compose } from "@wordpress/compose"; |
| 13 |
import addInitialAttr from "../shared/addInitialAttr"; |
| 14 |
|
| 15 |
const PostThumbnailEdit = ({ attributes, setAttributes, clientId, isSelected }) => { |
| 16 |
const [posts, setPosts] = useState([]); |
| 17 |
attributes.clientId = clientId; |
| 18 |
const { |
| 19 |
blockName, |
| 20 |
thumbnailSliderLayout, |
| 21 |
postThumbnailAnimationEffect, |
| 22 |
carouselPaginationStyle, |
| 23 |
carouselAutoPlay, |
| 24 |
postThumbnailPosition, |
| 25 |
contentAreaPadding, |
| 26 |
postCardBg, |
| 27 |
catTabCategoryBorder, |
| 28 |
fontListsEditPage, |
| 29 |
carouselArrowHorizontal, |
| 30 |
carouselPaginationVertical, |
| 31 |
carouselArrowColor, |
| 32 |
titleFontSize, |
| 33 |
carouselSpeed, |
| 34 |
carouselArrowVertical, |
| 35 |
contentAreaInnerWidth, |
| 36 |
carouselArrowWidth, |
| 37 |
carouselArrowHeight, |
| 38 |
carouselPaginationWidth, |
| 39 |
carouselPaginationHeight, |
| 40 |
customCss, |
| 41 |
addToCartColor, |
| 42 |
addToCartBorder, |
| 43 |
priceColor, |
| 44 |
discountColor, |
| 45 |
postThumbnailLoop, |
| 46 |
carouselAutoPlayDelay, |
| 47 |
carouselDirection, |
| 48 |
postThumbnailTabKeyNavigation, |
| 49 |
postThumbnailAdaptiveHeight, |
| 50 |
postThumbnailFreeScrollMode, |
| 51 |
thumbnailsSlidePerGroups, |
| 52 |
carouselPauseOnHover, |
| 53 |
postThumbnailMouseWheelControl, |
| 54 |
uniqueId, |
| 55 |
postThumbnailGap, |
| 56 |
postThumbnailSlideToScroll, |
| 57 |
postThumbnailItemsPerSlide, |
| 58 |
carouselArrowSpaceBetween, |
| 59 |
carouselNavArrow, |
| 60 |
carouselPaginationDot, |
| 61 |
globalBreakPointData, |
| 62 |
carouselPaginationBorder, |
| 63 |
} = attributes; |
| 64 |
|
| 65 |
const blockProps = useBlockProps(); |
| 66 |
const deviceType = useDeviceType(); |
| 67 |
|
| 68 |
const [postSliderKey, setPostSliderKey] = useState(1); |
| 69 |
|
| 70 |
const { Desktop: desktopGap, Tablet: tabletGap, Mobile: mobileGap } = postThumbnailGap.device; |
| 71 |
const { Desktop: desktopScroll, Tablet: tabletScroll, Mobile: mobileScroll } = postThumbnailSlideToScroll.device; |
| 72 |
|
| 73 |
const thumbOption = { |
| 74 |
loop: postThumbnailLoop && thumbnailSliderLayout !== "thumbnail-slider-layout-five", |
| 75 |
spaceBetween: desktopGap, |
| 76 |
grid: thumbnailSliderLayout === "thumbnail-slider-layout-five" ? { rows: 2, fill: "row" } : false, |
| 77 |
freeMode: true, |
| 78 |
watchSlidesProgress: true, |
| 79 |
touchStartPreventDefault: false, |
| 80 |
direction: ["left", "right"].includes(postThumbnailPosition) ? "vertical" : "horizontal", |
| 81 |
slideToClickedSlide: true, |
| 82 |
speed: carouselSpeed?.value, |
| 83 |
breakpoints: { |
| 84 |
0: { |
| 85 |
slidesPerView: postThumbnailItemsPerSlide?.device?.Mobile, |
| 86 |
slidesPerGroup: mobileScroll > 1 ? parseInt(mobileScroll) : parseInt(desktopScroll), |
| 87 |
spaceBetween: mobileGap ? mobileGap.toString() : desktopGap, |
| 88 |
}, |
| 89 |
[parseInt(globalBreakPointData.mobile) + 1]: { |
| 90 |
slidesPerView: postThumbnailItemsPerSlide?.device?.Tablet, |
| 91 |
slidesPerGroup: tabletScroll > 1 ? parseInt(tabletScroll) : parseInt(desktopScroll), |
| 92 |
spaceBetween: tabletGap ? tabletGap.toString() : desktopGap, |
| 93 |
}, |
| 94 |
[parseInt(globalBreakPointData.tablet) + 1]: { |
| 95 |
slidesPerView: postThumbnailItemsPerSlide?.device?.Desktop, |
| 96 |
slidesPerGroup: parseInt(desktopScroll) || 1, |
| 97 |
spaceBetween: desktopGap.toString() || "20", |
| 98 |
}, |
| 99 |
}, |
| 100 |
}; |
| 101 |
|
| 102 |
const swiperOptions = { |
| 103 |
slidesPerView: 1, |
| 104 |
simulateTouch: true, |
| 105 |
navigation: { |
| 106 |
nextEl: `#${uniqueId} .sp-smart-post-swiper-nav-arrow .btn-next`, |
| 107 |
prevEl: `#${uniqueId} .sp-smart-post-swiper-nav-arrow .btn-prev`, |
| 108 |
enabled: true, |
| 109 |
}, |
| 110 |
grabCursor: true, |
| 111 |
speed: carouselSpeed?.value, |
| 112 |
pagination: paginationDotType(null, carouselPaginationStyle, uniqueId), |
| 113 |
scrollbar: |
| 114 |
carouselPaginationStyle === "scrollbar" |
| 115 |
? { |
| 116 |
draggable: true, |
| 117 |
el: ".swiper-scrollbar", |
| 118 |
} |
| 119 |
: undefined, |
| 120 |
loop: postThumbnailLoop && thumbnailSliderLayout !== "thumbnail-slider-layout-five", |
| 121 |
// slidesPerGroup: Math.min(thumbnailsSlidePerGroups || 1, 10), |
| 122 |
slidesPerGroup: Number(desktopScroll), |
| 123 |
autoplay: carouselAutoPlay |
| 124 |
? { |
| 125 |
delay: carouselAutoPlayDelay?.value || 3000, |
| 126 |
disableOnInteraction: false, |
| 127 |
pauseOnMouseEnter: carouselPauseOnHover, |
| 128 |
reverseDirection: carouselDirection === "left_to_right", |
| 129 |
} |
| 130 |
: false, |
| 131 |
effect: postThumbnailAnimationEffect, |
| 132 |
cubeEffect: |
| 133 |
postThumbnailAnimationEffect === "cube" |
| 134 |
? { |
| 135 |
shadow: true, |
| 136 |
slideShadows: true, |
| 137 |
shadowOffset: 20, |
| 138 |
shadowScale: 0.94, |
| 139 |
} |
| 140 |
: undefined, |
| 141 |
coverflowEffect: |
| 142 |
postThumbnailAnimationEffect === "coverflow" |
| 143 |
? { |
| 144 |
rotate: 50, |
| 145 |
stretch: 0, |
| 146 |
depth: 100, |
| 147 |
modifier: 1, |
| 148 |
slideShadows: true, |
| 149 |
} |
| 150 |
: undefined, |
| 151 |
keyboard: postThumbnailTabKeyNavigation |
| 152 |
? { |
| 153 |
enabled: true, |
| 154 |
onlyInViewport: true, |
| 155 |
} |
| 156 |
: false, |
| 157 |
mousewheel: postThumbnailMouseWheelControl || false, |
| 158 |
autoHeight: !!postThumbnailAdaptiveHeight, |
| 159 |
freeMode: !!postThumbnailFreeScrollMode, |
| 160 |
}; |
| 161 |
|
| 162 |
useEffect(() => { |
| 163 |
setAttributes({ |
| 164 |
carouselData: JSON.stringify(swiperOptions), |
| 165 |
}); |
| 166 |
}, [JSON.stringify(swiperOptions)]); |
| 167 |
|
| 168 |
useEffect(() => { |
| 169 |
setAttributes({ |
| 170 |
thumbnailsData: JSON.stringify(thumbOption), |
| 171 |
}); |
| 172 |
}, [JSON.stringify(thumbOption)]); |
| 173 |
useEffect(() => { |
| 174 |
if (!blockName) { |
| 175 |
setAttributes({ |
| 176 |
imagePosition: "background", |
| 177 |
titleFontSize: { |
| 178 |
...titleFontSize, |
| 179 |
device: { |
| 180 |
...titleFontSize.device, |
| 181 |
Desktop: 44, |
| 182 |
Tablet: 32, |
| 183 |
Mobile: 20, |
| 184 |
}, |
| 185 |
}, |
| 186 |
carouselArrowHorizontal: { |
| 187 |
...carouselArrowHorizontal, |
| 188 |
device: { |
| 189 |
...carouselArrowHorizontal.device, |
| 190 |
Desktop: 30, |
| 191 |
Tablet: 10, |
| 192 |
Mobile: 5, |
| 193 |
}, |
| 194 |
unit: { |
| 195 |
...carouselArrowHorizontal.unit, |
| 196 |
Desktop: "px", |
| 197 |
Tablet: "px", |
| 198 |
Mobile: "px", |
| 199 |
}, |
| 200 |
}, |
| 201 |
carouselArrowSpaceBetween: { |
| 202 |
...carouselArrowSpaceBetween, |
| 203 |
device: { |
| 204 |
...carouselArrowSpaceBetween.device, |
| 205 |
Desktop: 100, |
| 206 |
}, |
| 207 |
}, |
| 208 |
carouselPaginationVertical: { |
| 209 |
...carouselPaginationVertical, |
| 210 |
device: { |
| 211 |
...carouselPaginationVertical.device, |
| 212 |
Desktop: "", |
| 213 |
Tablet: "", |
| 214 |
Mobile: "", |
| 215 |
}, |
| 216 |
}, |
| 217 |
carouselArrowColor: { |
| 218 |
...carouselArrowColor, |
| 219 |
color: "#ffffff", |
| 220 |
hoverColor: "#FFFFFF", |
| 221 |
}, |
| 222 |
postSliderGeneralContentAlign: "center", |
| 223 |
imageOverlayColor: "default", |
| 224 |
catTabCategoryBorder: { |
| 225 |
...catTabCategoryBorder, |
| 226 |
style: "none", |
| 227 |
}, |
| 228 |
contentAlignment: "center", |
| 229 |
postCardBg: { |
| 230 |
...postCardBg, |
| 231 |
color: { |
| 232 |
...postCardBg.color, |
| 233 |
style: "", |
| 234 |
}, |
| 235 |
hover: { |
| 236 |
...postCardBg.hover, |
| 237 |
style: "", |
| 238 |
}, |
| 239 |
}, |
| 240 |
contentHorizontalPosition: "center", |
| 241 |
// contentAreaPadding: { |
| 242 |
// ...contentAreaPadding, |
| 243 |
// device: { |
| 244 |
// ...contentAreaPadding.device, |
| 245 |
// Desktop: { |
| 246 |
// ...contentAreaPadding.device.Desktop, |
| 247 |
// top: 0, |
| 248 |
// right: 90, |
| 249 |
// bottom: 0, |
| 250 |
// left: 90, |
| 251 |
// }, |
| 252 |
// Tablet: { |
| 253 |
// ...contentAreaPadding.device.Desktop, |
| 254 |
// top: 8, |
| 255 |
// right: 16, |
| 256 |
// bottom: 8, |
| 257 |
// left: 16, |
| 258 |
// }, |
| 259 |
// }, |
| 260 |
// }, |
| 261 |
carouselSpeed: { |
| 262 |
...carouselSpeed, |
| 263 |
value: 600, |
| 264 |
}, |
| 265 |
carouselArrowVertical: { |
| 266 |
...carouselArrowVertical, |
| 267 |
device: { |
| 268 |
...carouselArrowVertical.device, |
| 269 |
Desktop: 50, |
| 270 |
}, |
| 271 |
unit: { |
| 272 |
...carouselArrowVertical.unit, |
| 273 |
Desktop: "%", |
| 274 |
}, |
| 275 |
}, |
| 276 |
contentAreaInnerWidth: { |
| 277 |
...contentAreaInnerWidth, |
| 278 |
device: { |
| 279 |
...contentAreaInnerWidth.device, |
| 280 |
Desktop: 85, |
| 281 |
Tablet: 90, |
| 282 |
Mobile: 100, |
| 283 |
}, |
| 284 |
}, |
| 285 |
carouselArrowWidth: { |
| 286 |
...carouselArrowWidth, |
| 287 |
device: { |
| 288 |
...carouselArrowWidth.device, |
| 289 |
Mobile: 30, |
| 290 |
}, |
| 291 |
}, |
| 292 |
carouselArrowHeight: { |
| 293 |
...carouselArrowHeight, |
| 294 |
device: { |
| 295 |
...carouselArrowHeight.device, |
| 296 |
Mobile: 30, |
| 297 |
}, |
| 298 |
}, |
| 299 |
carouselPaginationWidth: { |
| 300 |
...carouselPaginationWidth, |
| 301 |
device: { |
| 302 |
...carouselPaginationWidth.device, |
| 303 |
Mobile: 10, |
| 304 |
}, |
| 305 |
}, |
| 306 |
carouselPaginationHeight: { |
| 307 |
...carouselPaginationHeight, |
| 308 |
device: { |
| 309 |
...carouselPaginationHeight.device, |
| 310 |
Mobile: 10, |
| 311 |
}, |
| 312 |
}, |
| 313 |
addToCartColor: { |
| 314 |
...addToCartColor, |
| 315 |
color: "#FFFFFF", |
| 316 |
}, |
| 317 |
addToCartBorder: { |
| 318 |
...addToCartBorder, |
| 319 |
color: "#FFFFFF", |
| 320 |
}, |
| 321 |
priceColor: { |
| 322 |
...priceColor, |
| 323 |
color: "#FFFFFF", |
| 324 |
}, |
| 325 |
discountColor: { |
| 326 |
...discountColor, |
| 327 |
color: "#FFFFFF", |
| 328 |
}, |
| 329 |
imageHoverEffect: "normal", |
| 330 |
carouselPaginationBorder: { |
| 331 |
...carouselPaginationBorder, |
| 332 |
style: "", |
| 333 |
color: "", |
| 334 |
}, |
| 335 |
}); |
| 336 |
} |
| 337 |
}, []); |
| 338 |
|
| 339 |
const blockStyling = useMemo( |
| 340 |
() => dynamicCssFn(attributes, "frontend", deviceType), |
| 341 |
[...cssDependency(attributes), deviceType, attributes] |
| 342 |
); |
| 343 |
|
| 344 |
useEffect(() => { |
| 345 |
setAttributes({ blockLayoutName: thumbnailSliderLayout }); |
| 346 |
}, [blockName, thumbnailSliderLayout]); |
| 347 |
|
| 348 |
useEffect(() => { |
| 349 |
setAttributes({ currentScreen: deviceType }); |
| 350 |
}, [deviceType]); |
| 351 |
|
| 352 |
useEffect(() => { |
| 353 |
setPostSliderKey(Math.floor(Math.random() * 100)); |
| 354 |
}, [ |
| 355 |
postThumbnailAnimationEffect, |
| 356 |
blockName, |
| 357 |
carouselPaginationStyle, |
| 358 |
carouselAutoPlay, |
| 359 |
postThumbnailPosition, |
| 360 |
posts, |
| 361 |
carouselNavArrow, |
| 362 |
carouselPaginationDot, |
| 363 |
]); |
| 364 |
|
| 365 |
return ( |
| 366 |
<div {...blockProps}> |
| 367 |
<style> |
| 368 |
{fontListsEditPage} |
| 369 |
{blockStyling} |
| 370 |
{customCss} |
| 371 |
</style> |
| 372 |
<TogglePanelBodyProvider> |
| 373 |
<InspectorControl |
| 374 |
TitleIcon={panelBodyTitleIcon} |
| 375 |
RightIcon={panelBodyRightIcon} |
| 376 |
Inspector={Inspector} |
| 377 |
attributes={attributes} |
| 378 |
setAttributes={setAttributes} |
| 379 |
isSelected={isSelected} |
| 380 |
/> |
| 381 |
<EditorWrapper setPosts={setPosts} setAttributes={setAttributes}> |
| 382 |
<Render key={postSliderKey} attributes={attributes} posts={posts} pageType="editor" /> |
| 383 |
</EditorWrapper> |
| 384 |
</TogglePanelBodyProvider> |
| 385 |
</div> |
| 386 |
); |
| 387 |
}; |
| 388 |
|
| 389 |
export default compose(addInitialAttr)(PostThumbnailEdit); |
| 390 |
|