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