| 1 |
import { useEffect, useState, useMemo } from "@wordpress/element"; |
| 2 |
import { useBlockProps } from "@wordpress/block-editor"; |
| 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 { useSelect } from "@wordpress/data"; |
| 13 |
import { compose } from "@wordpress/compose"; |
| 14 |
import addInitialAttr from "../shared/addInitialAttr"; |
| 15 |
|
| 16 |
const TimelineThreeEdit = ({ attributes, setAttributes, isSelected }) => { |
| 17 |
const { |
| 18 |
postCardBg, |
| 19 |
contentAreaBg, |
| 20 |
fontListsEditPage, |
| 21 |
timelineIndicatorColor, |
| 22 |
timelineConnectorBorder, |
| 23 |
imageSpace, |
| 24 |
contentAreaPadding, |
| 25 |
gapBetweenPosts, |
| 26 |
postCardBoxShadow, |
| 27 |
postCardHoverBoxShadow, |
| 28 |
imageHeight, |
| 29 |
carouselArrowHorizontal, |
| 30 |
carouselGap, |
| 31 |
customCss, |
| 32 |
carouselStyle, |
| 33 |
carouselColumn, |
| 34 |
uniqueId, |
| 35 |
carouselPaginationStyle, |
| 36 |
postTimelineAutoPlayDelay, |
| 37 |
carouselPauseOnHover, |
| 38 |
carouselDirection, |
| 39 |
postTimelineAutoPlay, |
| 40 |
carouselSpeed, |
| 41 |
postTimelineTickerSpeed, |
| 42 |
timelineLayout, |
| 43 |
postLimit, |
| 44 |
} = attributes; |
| 45 |
const blockProps = useBlockProps(); |
| 46 |
const deviceType = useDeviceType(); |
| 47 |
const { blockName } = attributes; |
| 48 |
const [posts, setPosts] = useState([]); |
| 49 |
|
| 50 |
// Get global breakpoint data from the store and set to attributes. |
| 51 |
const getGlobalBreakpoint = useSelect( |
| 52 |
(select) => select("smartpost/global-settings").getCategory("breakpoint"), |
| 53 |
[] |
| 54 |
); |
| 55 |
useEffect(() => { |
| 56 |
if (typeof getGlobalBreakpoint === "object") { |
| 57 |
setAttributes({ globalBreakPointData: getGlobalBreakpoint }); |
| 58 |
} |
| 59 |
}, [getGlobalBreakpoint]); |
| 60 |
|
| 61 |
useEffect(() => { |
| 62 |
if (!blockName) { |
| 63 |
setAttributes({ |
| 64 |
timelineLayout: "timeline-three-layout-one", |
| 65 |
contentAreaBg: { |
| 66 |
...contentAreaBg, |
| 67 |
color: { |
| 68 |
...contentAreaBg.color, |
| 69 |
style: "", |
| 70 |
}, |
| 71 |
hover: { |
| 72 |
...contentAreaBg.hover, |
| 73 |
style: "", |
| 74 |
}, |
| 75 |
}, |
| 76 |
postCardBg: { |
| 77 |
...postCardBg, |
| 78 |
color: { |
| 79 |
...postCardBg.color, |
| 80 |
style: "", |
| 81 |
}, |
| 82 |
hover: { |
| 83 |
...postCardBg.hover, |
| 84 |
style: "", |
| 85 |
}, |
| 86 |
}, |
| 87 |
timelineIndicatorColor: { |
| 88 |
...timelineIndicatorColor, |
| 89 |
color: "var(--smart-post-secondary)", |
| 90 |
}, |
| 91 |
timelineConnectorBorder: { |
| 92 |
...timelineConnectorBorder, |
| 93 |
color: "var(--smart-post-secondary)", |
| 94 |
}, |
| 95 |
imageSpace: { |
| 96 |
...imageSpace, |
| 97 |
device: { |
| 98 |
...imageSpace.device, |
| 99 |
Desktop: 12, |
| 100 |
}, |
| 101 |
}, |
| 102 |
contentAreaPadding: { |
| 103 |
...contentAreaPadding, |
| 104 |
device: { |
| 105 |
...contentAreaPadding.device, |
| 106 |
Desktop: { |
| 107 |
...contentAreaPadding.device.Desktop, |
| 108 |
top: 0, |
| 109 |
right: 20, |
| 110 |
bottom: 15, |
| 111 |
left: 20, |
| 112 |
}, |
| 113 |
Tablet: { |
| 114 |
...contentAreaPadding.device.Tablet, |
| 115 |
top: 0, |
| 116 |
right: 20, |
| 117 |
bottom: 15, |
| 118 |
left: 20, |
| 119 |
}, |
| 120 |
}, |
| 121 |
}, |
| 122 |
carouselGap: { |
| 123 |
...carouselGap, |
| 124 |
device: { |
| 125 |
...carouselGap.device, |
| 126 |
Desktop: 32, |
| 127 |
}, |
| 128 |
}, |
| 129 |
postCardBoxShadowEnable: true, |
| 130 |
postCardHoverBoxShadowEnable: true, |
| 131 |
postCardBoxShadow: { |
| 132 |
...postCardBoxShadow, |
| 133 |
device: { |
| 134 |
...postCardBoxShadow.device, |
| 135 |
Desktop: { |
| 136 |
top: 0, |
| 137 |
right: 8, |
| 138 |
bottom: 10, |
| 139 |
left: 0, |
| 140 |
}, |
| 141 |
}, |
| 142 |
selectDefault: "var(--smart-post-shadow-medium-4dp)", |
| 143 |
color: "#0000001A", |
| 144 |
}, |
| 145 |
postCardHoverBoxShadow: { |
| 146 |
...postCardHoverBoxShadow, |
| 147 |
device: { |
| 148 |
...postCardHoverBoxShadow.device, |
| 149 |
Desktop: { |
| 150 |
top: 0, |
| 151 |
right: 8, |
| 152 |
bottom: 10, |
| 153 |
left: 0, |
| 154 |
}, |
| 155 |
}, |
| 156 |
selectDefault: "var(--smart-post-shadow-medium-4dp)", |
| 157 |
color: "#0000001A", |
| 158 |
}, |
| 159 |
imageSize: "smart-post-landscape", |
| 160 |
imageHeight: { |
| 161 |
...imageHeight, |
| 162 |
device: { |
| 163 |
...imageHeight?.device, |
| 164 |
Desktop: 310, |
| 165 |
}, |
| 166 |
}, |
| 167 |
carouselArrowHorizontal: { |
| 168 |
...carouselArrowHorizontal, |
| 169 |
device: { |
| 170 |
...carouselArrowHorizontal.device, |
| 171 |
Desktop: -10, |
| 172 |
Tablet: "", |
| 173 |
Mobile: "", |
| 174 |
}, |
| 175 |
}, |
| 176 |
socialPopupShareColor: { |
| 177 |
color: "", |
| 178 |
hoverColor: "", |
| 179 |
}, |
| 180 |
}); |
| 181 |
} |
| 182 |
}, []); |
| 183 |
const desktopGap = carouselGap?.device?.Desktop ? carouselGap?.device?.Desktop.toString() : "0"; |
| 184 |
const tabletGap = carouselGap?.device?.Tablet ? carouselGap?.device?.Tablet.toString() : desktopGap; |
| 185 |
const mobileGap = carouselGap?.device?.Mobile ? carouselGap?.device?.Mobile.toString() : desktopGap; |
| 186 |
// const { |
| 187 |
// Desktop: desktopScroll, |
| 188 |
// Tablet: tabletScroll, |
| 189 |
// Mobile: mobileScroll, |
| 190 |
// } = slideToScroll.device; |
| 191 |
let swiperOptions = {}; |
| 192 |
if (carouselStyle !== "ticker") { |
| 193 |
swiperOptions = { |
| 194 |
slidesPerView: |
| 195 |
carouselColumn.device?.[deviceType] > postLimit ? postLimit : carouselColumn.device?.[deviceType], |
| 196 |
// simulateTouch: true, |
| 197 |
navigation: { |
| 198 |
nextEl: `#${uniqueId} .sp-smart-post-swiper-nav-arrow .btn-next`, |
| 199 |
prevEl: `#${uniqueId} .sp-smart-post-swiper-nav-arrow .btn-prev`, |
| 200 |
enabled: true, |
| 201 |
}, |
| 202 |
grabCursor: true, |
| 203 |
pagination: paginationDotType(null, carouselPaginationStyle, uniqueId), |
| 204 |
scrollbar: |
| 205 |
carouselPaginationStyle === "scrollbar" |
| 206 |
? { |
| 207 |
draggable: true, |
| 208 |
el: ".swiper-scrollbar", |
| 209 |
} |
| 210 |
: false, |
| 211 |
loop: "timeline-three-layout-two" === timelineLayout ? false : true, |
| 212 |
slidesPerGroup: 1, |
| 213 |
autoplay: postTimelineAutoPlay |
| 214 |
? { |
| 215 |
delay: postTimelineAutoPlayDelay?.value, |
| 216 |
disableOnInteraction: false, |
| 217 |
pauseOnMouseEnter: carouselPauseOnHover, |
| 218 |
reverseDirection: carouselDirection === "left_to_right", |
| 219 |
} |
| 220 |
: false, |
| 221 |
|
| 222 |
speed: carouselSpeed?.value, |
| 223 |
spaceBetween: carouselGap.device?.[deviceType] || carouselGap.device?.Desktop || 0, |
| 224 |
breakpoints: { |
| 225 |
0: { |
| 226 |
slidesPerView: |
| 227 |
carouselColumn?.device?.Mobile > postLimit ? postLimit : carouselColumn?.device?.Mobile, |
| 228 |
spaceBetween: mobileGap ? mobileGap.toString() : desktopGap, |
| 229 |
}, |
| 230 |
600: { |
| 231 |
slidesPerView: |
| 232 |
carouselColumn?.device?.Tablet > postLimit ? postLimit : carouselColumn?.device?.Tablet, |
| 233 |
spaceBetween: tabletGap ? tabletGap.toString() : desktopGap, |
| 234 |
}, |
| 235 |
1024: { |
| 236 |
slidesPerView: |
| 237 |
carouselColumn?.device?.Desktop > postLimit ? postLimit : carouselColumn?.device?.Desktop, |
| 238 |
spaceBetween: desktopGap.toString() || "0", |
| 239 |
}, |
| 240 |
}, |
| 241 |
keyboard: { |
| 242 |
enabled: true, |
| 243 |
onlyInViewport: true, |
| 244 |
}, |
| 245 |
mousewheel: true, |
| 246 |
// autoHeight: carouselAdaptiveHeight ? true : false, |
| 247 |
// freeMode: carouselFreeScrollMode || false, |
| 248 |
direction: "horizontal", |
| 249 |
}; |
| 250 |
} else { |
| 251 |
swiperOptions = { |
| 252 |
slidesPerView: |
| 253 |
carouselColumn?.device?.[deviceType] > postLimit ? postLimit : carouselColumn?.device?.[deviceType], |
| 254 |
slidesPerViewMobile: |
| 255 |
carouselColumn?.device?.Mobile > postLimit ? postLimit : carouselColumn?.device?.Mobile, |
| 256 |
slidesPerViewTable: carouselColumn?.device?.Tablet > postLimit ? postLimit : carouselColumn?.device?.Tablet, |
| 257 |
speed: postTimelineTickerSpeed?.value, |
| 258 |
spaceBetween: carouselGap.device?.[deviceType] || 0, |
| 259 |
spaceBetweenMobile: mobileGap ? mobileGap.toString() : desktopGap, |
| 260 |
spaceBetweenTablet: tabletGap ? tabletGap.toString() : desktopGap, |
| 261 |
direction: carouselDirection, |
| 262 |
pauseOnHover: carouselPauseOnHover, |
| 263 |
}; |
| 264 |
} |
| 265 |
useEffect(() => { |
| 266 |
setAttributes({ |
| 267 |
carouselData: JSON.stringify(swiperOptions), |
| 268 |
}); |
| 269 |
}, [JSON.stringify(swiperOptions)]); |
| 270 |
// useEffect( () => { |
| 271 |
// setAttributes( { |
| 272 |
// dynamicCss: dynamicCssFn( attributes, 'frontend', setAttributes ), |
| 273 |
// } ); |
| 274 |
// }, cssDependency(attributes)); |
| 275 |
|
| 276 |
const blockStyling = useMemo( |
| 277 |
() => dynamicCssFn(attributes, "frontend", deviceType), |
| 278 |
[...cssDependency(attributes), deviceType] |
| 279 |
); |
| 280 |
|
| 281 |
useEffect(() => { |
| 282 |
setAttributes({ currentScreen: deviceType }); |
| 283 |
}, [deviceType]); |
| 284 |
|
| 285 |
return ( |
| 286 |
<div {...blockProps}> |
| 287 |
<style> |
| 288 |
{fontListsEditPage} |
| 289 |
{blockStyling} |
| 290 |
{customCss} |
| 291 |
</style> |
| 292 |
<TogglePanelBodyProvider> |
| 293 |
<InspectorControl |
| 294 |
TitleIcon={panelBodyTitleIcon} |
| 295 |
RightIcon={panelBodyRightIcon} |
| 296 |
Inspector={Inspector} |
| 297 |
attributes={attributes} |
| 298 |
setAttributes={setAttributes} |
| 299 |
isSelected={isSelected} |
| 300 |
/> |
| 301 |
{/* Render Html here */} |
| 302 |
<EditorWrapper setPosts={setPosts} setAttributes={setAttributes}> |
| 303 |
<Render posts={posts} attributes={attributes} page={"editor"} /> |
| 304 |
</EditorWrapper> |
| 305 |
</TogglePanelBodyProvider> |
| 306 |
</div> |
| 307 |
); |
| 308 |
}; |
| 309 |
|
| 310 |
export default compose(addInitialAttr)(TimelineThreeEdit); |
| 311 |
|