| 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 { useDeviceType, paginationDotType } from "../../controls/controls"; |
| 6 |
import { EditorWrapper } from "../shared/wrapper"; |
| 7 |
import dynamicCssFn from "./dynamicCss"; |
| 8 |
import Inspector from "./inspect"; |
| 9 |
import Render from "./render"; |
| 10 |
import { TogglePanelBodyProvider } from "../../context"; |
| 11 |
import { cssDependency } from "../shared/cssDependency"; |
| 12 |
import { compose } from "@wordpress/compose"; |
| 13 |
import addInitialAttr from "../shared/addInitialAttr"; |
| 14 |
import { priceLink } from "../shared/helpFn"; |
| 15 |
import { blockPreviewPanelLink } from "../../controls/constants"; |
| 16 |
|
| 17 |
const PostSliderTwoEdit = ({ attributes, setAttributes, isSelected }) => { |
| 18 |
const [posts, setPosts] = useState([]); |
| 19 |
const { |
| 20 |
blockName, |
| 21 |
fontListsEditPage, |
| 22 |
customCss, |
| 23 |
carouselArrowVertical, |
| 24 |
carouselArrowHorizontal, |
| 25 |
carouselArrowSpaceBetween, |
| 26 |
infiniteLoop, |
| 27 |
carouselPauseOnHover, |
| 28 |
contentAreaWidth, |
| 29 |
contentAreaHeight, |
| 30 |
contentAreaBg, |
| 31 |
contentAreaPadding, |
| 32 |
carouselArrowBorder, |
| 33 |
contentHorizontalPosition, |
| 34 |
imagePosition, |
| 35 |
blockLayoutName, |
| 36 |
contentAreaBorderWidth, |
| 37 |
socialPopupShareColor, |
| 38 |
paginationDotStyle, |
| 39 |
postSliderSlidePerGroups, |
| 40 |
carouselAutoPlay, |
| 41 |
carouselDirection, |
| 42 |
carouselPaginationStyle, |
| 43 |
postSliderSlideScroll, |
| 44 |
carouselAutoPlayDelay, |
| 45 |
postSliderHoverPause, |
| 46 |
carouselSpeed, |
| 47 |
postSliderLayout, |
| 48 |
uniqueId, |
| 49 |
postSliderTwoAnimationEffect, |
| 50 |
postSliderTwoMouseWheelControl, |
| 51 |
postSliderTwoFreeScrollMode, |
| 52 |
postSliderTwoTabKeyNavigation, |
| 53 |
// postSliderTwoNavArrow, |
| 54 |
titleFontSize, |
| 55 |
carouselNavArrow, |
| 56 |
carouselPaginationDot, |
| 57 |
} = attributes; |
| 58 |
|
| 59 |
const blockProps = useBlockProps(); |
| 60 |
|
| 61 |
const deviceType = useDeviceType(); |
| 62 |
const [postSliderKey, setPostSliderKey] = useState(1); |
| 63 |
|
| 64 |
useEffect(() => { |
| 65 |
if (!blockName) { |
| 66 |
setAttributes({ |
| 67 |
// carouselPauseOnHover: false, |
| 68 |
contentAlignment: "center", |
| 69 |
imagePosition: "background", |
| 70 |
|
| 71 |
titleFontSize: { |
| 72 |
...titleFontSize, |
| 73 |
device: { |
| 74 |
...titleFontSize.device, |
| 75 |
Desktop: 42, |
| 76 |
Tablet: 32, |
| 77 |
Mobile: 18, |
| 78 |
}, |
| 79 |
}, |
| 80 |
carouselArrowSpaceBetween: { |
| 81 |
...carouselArrowSpaceBetween, |
| 82 |
device: { |
| 83 |
...carouselArrowSpaceBetween.device, |
| 84 |
Desktop: 100, |
| 85 |
}, |
| 86 |
}, |
| 87 |
carouselArrowHorizontal: { |
| 88 |
...carouselArrowHorizontal, |
| 89 |
device: { |
| 90 |
...carouselArrowHorizontal.device, |
| 91 |
Desktop: "", |
| 92 |
}, |
| 93 |
}, |
| 94 |
carouselArrowVertical: { |
| 95 |
...carouselArrowVertical, |
| 96 |
device: { |
| 97 |
...carouselArrowVertical.device, |
| 98 |
Desktop: "", |
| 99 |
Tablet: "", |
| 100 |
Mobile: "", |
| 101 |
}, |
| 102 |
}, |
| 103 |
// carouselArrowBgColor: { |
| 104 |
// ...carouselArrowBgColor, |
| 105 |
// color: '#2F2F2F', |
| 106 |
// }, |
| 107 |
// carouselArrowBorderRadius: { |
| 108 |
// ...carouselArrowBorderRadius, |
| 109 |
// device: { |
| 110 |
// ...carouselArrowBorderRadius.device, |
| 111 |
// Desktop: { |
| 112 |
// top: 0, |
| 113 |
// right: 0, |
| 114 |
// bottom: 0, |
| 115 |
// left: 0, |
| 116 |
// }, |
| 117 |
// }, |
| 118 |
// }, |
| 119 |
imageOverlayType: "box", |
| 120 |
contentAreaWidth: { |
| 121 |
...contentAreaWidth, |
| 122 |
device: { |
| 123 |
...contentAreaWidth.device, |
| 124 |
Desktop: 580, |
| 125 |
}, |
| 126 |
unit: { |
| 127 |
...contentAreaWidth.unit, |
| 128 |
Desktop: "px", |
| 129 |
}, |
| 130 |
}, |
| 131 |
contentAreaHeight: { |
| 132 |
...contentAreaHeight, |
| 133 |
device: { |
| 134 |
...contentAreaHeight.device, |
| 135 |
Desktop: 280, |
| 136 |
}, |
| 137 |
unit: { |
| 138 |
...contentAreaHeight.unit, |
| 139 |
Desktop: "px", |
| 140 |
}, |
| 141 |
}, |
| 142 |
contentHorizontalPosition: "center", |
| 143 |
contentAreaBg: { |
| 144 |
...contentAreaBg, |
| 145 |
color: { |
| 146 |
...contentAreaBg.color, |
| 147 |
solidColor: "#ffffffe6", |
| 148 |
style: "bgColor", |
| 149 |
}, |
| 150 |
}, |
| 151 |
contentAreaPadding: { |
| 152 |
...contentAreaPadding, |
| 153 |
device: { |
| 154 |
...contentAreaPadding.device, |
| 155 |
Desktop: { |
| 156 |
top: 8, |
| 157 |
right: 32, |
| 158 |
bottom: 8, |
| 159 |
left: 32, |
| 160 |
}, |
| 161 |
}, |
| 162 |
}, |
| 163 |
carouselArrowBorder: { |
| 164 |
...carouselArrowBorder, |
| 165 |
style: "none", |
| 166 |
}, |
| 167 |
contentAreaBorderWidth: { |
| 168 |
...contentAreaBorderWidth, |
| 169 |
device: { |
| 170 |
...contentAreaBorderWidth.device, |
| 171 |
Desktop: { |
| 172 |
top: 1, |
| 173 |
right: 1, |
| 174 |
bottom: 1, |
| 175 |
left: 1, |
| 176 |
}, |
| 177 |
}, |
| 178 |
}, |
| 179 |
socialPopupShareColor: { |
| 180 |
...socialPopupShareColor, |
| 181 |
color: "#333", |
| 182 |
}, |
| 183 |
imageHoverEffect: "normal", |
| 184 |
}); |
| 185 |
} |
| 186 |
}, []); |
| 187 |
const swiperOptions = { |
| 188 |
slidesPerView: 1, |
| 189 |
simulateTouch: true, |
| 190 |
navigation: carouselNavArrow |
| 191 |
? { |
| 192 |
nextEl: `#${uniqueId} .sp-smart-post-swiper-nav-arrow .btn-next`, |
| 193 |
prevEl: `#${uniqueId} .sp-smart-post-swiper-nav-arrow .btn-prev`, |
| 194 |
enabled: true, |
| 195 |
} |
| 196 |
: false, |
| 197 |
spaceBetween: 0, |
| 198 |
grabCursor: true, |
| 199 |
pagination: paginationDotType(null, carouselPaginationStyle, uniqueId), |
| 200 |
scrollbar: |
| 201 |
carouselPaginationStyle === "scrollbar" |
| 202 |
? { |
| 203 |
draggable: true, |
| 204 |
el: `#${uniqueId} .swiper-scrollbar`, |
| 205 |
} |
| 206 |
: false, |
| 207 |
loop: !infiniteLoop || "scrollbar" === carouselPaginationStyle ? false : true, |
| 208 |
slidesPerGroup: postSliderSlideScroll?.device?.[deviceType], |
| 209 |
autoplay: carouselAutoPlay |
| 210 |
? { |
| 211 |
delay: carouselAutoPlayDelay?.value, |
| 212 |
disableOnInteraction: false, |
| 213 |
pauseOnMouseEnter: postSliderHoverPause, |
| 214 |
reverseDirection: carouselDirection === "left_to_right", |
| 215 |
} |
| 216 |
: false, |
| 217 |
speed: carouselSpeed?.value, |
| 218 |
effect: postSliderTwoAnimationEffect, |
| 219 |
cubeEffect: |
| 220 |
postSliderTwoAnimationEffect === "cube" |
| 221 |
? { |
| 222 |
shadow: true, |
| 223 |
slideShadows: true, |
| 224 |
shadowOffset: 20, |
| 225 |
shadowScale: 0.94, |
| 226 |
} |
| 227 |
: false, |
| 228 |
coverflowEffect: |
| 229 |
postSliderTwoAnimationEffect === "coverflow" |
| 230 |
? { |
| 231 |
rotate: 50, |
| 232 |
stretch: 0, |
| 233 |
depth: 100, |
| 234 |
modifier: 1, |
| 235 |
slideShadows: true, |
| 236 |
} |
| 237 |
: false, |
| 238 |
keyboard: postSliderTwoTabKeyNavigation |
| 239 |
? { |
| 240 |
enabled: true, |
| 241 |
onlyInViewport: true, |
| 242 |
} |
| 243 |
: false, |
| 244 |
mousewheel: postSliderTwoMouseWheelControl || false, |
| 245 |
freeMode: postSliderTwoFreeScrollMode || false, |
| 246 |
touchStartPreventDefault: false, |
| 247 |
direction: postSliderLayout === "post-slider-layout-two" ? "vertical" : "horizontal", |
| 248 |
}; |
| 249 |
useEffect(() => { |
| 250 |
setAttributes({ |
| 251 |
carouselData: JSON.stringify(swiperOptions), |
| 252 |
}); |
| 253 |
}, [JSON.stringify(swiperOptions)]); |
| 254 |
|
| 255 |
const blockStyling = useMemo( |
| 256 |
() => dynamicCssFn(attributes, "frontend", deviceType), |
| 257 |
[...cssDependency(attributes), deviceType, attributes] |
| 258 |
); |
| 259 |
|
| 260 |
useEffect(() => { |
| 261 |
setAttributes({ currentScreen: deviceType }); |
| 262 |
}, [deviceType]); |
| 263 |
|
| 264 |
useEffect(() => { |
| 265 |
if (blockLayoutName === "post-slider-two-layout-three") { |
| 266 |
setAttributes({ |
| 267 |
imagePosition: contentHorizontalPosition === "right" ? "left" : "right", |
| 268 |
}); |
| 269 |
} else { |
| 270 |
setAttributes({ imagePosition: "background" }); |
| 271 |
} |
| 272 |
}, [imagePosition, contentHorizontalPosition]); |
| 273 |
|
| 274 |
useEffect(() => { |
| 275 |
setPostSliderKey(Math.floor(Math.random() * 1000000)); |
| 276 |
}, [ |
| 277 |
carouselPauseOnHover, |
| 278 |
infiniteLoop, |
| 279 |
postSliderTwoTabKeyNavigation, |
| 280 |
postSliderTwoMouseWheelControl, |
| 281 |
postSliderTwoFreeScrollMode, |
| 282 |
postSliderTwoAnimationEffect, |
| 283 |
paginationDotStyle, |
| 284 |
postSliderSlidePerGroups, |
| 285 |
carouselAutoPlay, |
| 286 |
carouselDirection, |
| 287 |
posts, |
| 288 |
carouselNavArrow, |
| 289 |
carouselPaginationDot, |
| 290 |
carouselPaginationStyle, |
| 291 |
]); |
| 292 |
return ( |
| 293 |
<div {...blockProps}> |
| 294 |
<style> |
| 295 |
{fontListsEditPage} |
| 296 |
{blockStyling} |
| 297 |
{customCss} |
| 298 |
</style> |
| 299 |
<TogglePanelBodyProvider> |
| 300 |
<InspectorControl |
| 301 |
TitleIcon={panelBodyTitleIcon} |
| 302 |
RightIcon={panelBodyRightIcon} |
| 303 |
Inspector={Inspector} |
| 304 |
attributes={attributes} |
| 305 |
setAttributes={setAttributes} |
| 306 |
isSelected={isSelected} |
| 307 |
// templateLibrary={ true } |
| 308 |
/> |
| 309 |
{/* Render Html here */} |
| 310 |
<EditorWrapper |
| 311 |
setPosts={setPosts} |
| 312 |
setAttributes={setAttributes} |
| 313 |
customClass={carouselPaginationDot ? " sp-pagination" : ""} |
| 314 |
> |
| 315 |
<Render attributes={attributes} posts={posts} swiperKey={postSliderKey} pageType="editor" /> |
| 316 |
</EditorWrapper> |
| 317 |
</TogglePanelBodyProvider> |
| 318 |
</div> |
| 319 |
); |
| 320 |
}; |
| 321 |
|
| 322 |
export default compose(addInitialAttr)(PostSliderTwoEdit); |
| 323 |
|