/
CGallery.tsx
340 lines (308 loc) · 11.3 KB
/
CGallery.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import { getRandStr, TCromwellBlockData, TCromwellBlockProps, TGallerySettings } from '@cromwell/core';
import clsx from 'clsx';
import {
ButtonBack,
ButtonNext,
CarouselContext,
CarouselInjectedProps,
CarouselProvider,
DotGroup,
Image as CarouselImage,
ImageProps,
ImageWithZoom,
Slide,
Slider,
WithStore,
} from 'pure-react-carousel';
import React, { useContext, useEffect } from 'react';
import ReactResizeDetector from 'react-resize-detector';
import { CBlock } from '../CBlock/CBlock';
import { Link } from '../Link/Link';
import styles from './CGallery.module.scss';
import { Lightbox } from './Lightbox';
import Thumbs from './Thumbs';
export type TCGalleryProps = {
className?: string;
shouldComponentUpdate?: boolean;
} & TCromwellBlockProps;
export type TImageComponent = React.ComponentClass<Partial<ImageProps>>;
/** @internal */
class CarouselStoreSetterRaw extends React.Component<
CarouselInjectedProps & {
setStore: (store: CarouselInjectedProps['carouselStore']) => any;
}
> {
constructor(props) {
super(props);
this.props.setStore(this.props.carouselStore);
}
render() {
this.props.setStore(this.props.carouselStore);
return <></>;
}
}
/** @internal */
const CarouselStoreSetter = WithStore(CarouselStoreSetterRaw) as any as React.ComponentType<{
setStore: (store: CarouselInjectedProps['carouselStore']) => any;
}>;
export class CGallery extends React.Component<TCGalleryProps> {
private gallerySettings?: TGallerySettings;
private prevGallerySettings?: TGallerySettings;
private galleryId?: string;
private thumbsId?: string;
private randId = getRandStr(5);
private galleryStore?: CarouselInjectedProps['carouselStore'];
private activeSlide: number = 0;
private setLightbox?: (open: boolean, index: number) => void;
private forceUpdateThumbs?: () => void;
private onActiveSlideChange = (index: number) => {
this.activeSlide = index;
if (this.gallerySettings?.thumbs && this.gallerySettings?.images?.length) this.forceUpdateThumbs?.();
};
private setActiveSlide = (index: number) => {
this.galleryStore?.setStoreState({ currentSlide: index });
};
private openFullScreen = (index: number) => {
this.setLightbox?.(true, index);
};
private onSlideClick = (index: number) => {
if (this.gallerySettings?.fullscreen) this.openFullScreen(index);
};
private getContent = (data?: TCromwellBlockData, width?: number, height?: number) => {
const { gallery: propsSettings } = this.props;
const totalGallerySettings = data?.gallery ?? propsSettings;
this.gallerySettings = totalGallerySettings;
if (totalGallerySettings?.responsive && width && Object.keys(totalGallerySettings?.responsive).length) {
// find closest breakpoint to width but less
let closest = 0;
Object.keys(totalGallerySettings.responsive).forEach((b, index) => {
if (index === 0) {
// use first if no appropriate would found
this.gallerySettings = totalGallerySettings?.responsive?.[b];
}
const breakpoint = parseInt(b);
if (isNaN(breakpoint)) return;
if (breakpoint > closest && breakpoint < width) {
closest = breakpoint;
this.gallerySettings = totalGallerySettings?.responsive?.[b];
}
});
// Make responsive config override higher-level one
this.gallerySettings = Object.assign({}, totalGallerySettings, this.gallerySettings);
}
if (
this.prevGallerySettings?.images?.length &&
JSON.stringify(this.prevGallerySettings?.images?.map((i) => i.src)) !==
JSON.stringify(this.gallerySettings?.images?.map((i) => i.src))
) {
// reset active slide if images changed
setTimeout(() => {
this.setActiveSlide(0);
});
}
this.prevGallerySettings = this.gallerySettings;
const gallerySettings = this.gallerySettings;
this.galleryId = `CGallery_${data?.id}_${this.randId}`;
this.thumbsId = `${this.galleryId}_thumbs`;
if (!gallerySettings || !(gallerySettings.images || gallerySettings.slides)) return <></>;
const Image = (gallerySettings.zoom ? ImageWithZoom : CarouselImage) as TImageComponent;
const totalSlides = gallerySettings.images?.length ?? gallerySettings.slides?.length ?? 0;
let visibleSlides = gallerySettings.visibleSlides ?? 1;
if (visibleSlides > totalSlides) visibleSlides = totalSlides;
const ButtonBackContent =
gallerySettings.components?.backButton ??
(() => <div className={clsx(styles.navBtnContent, styles.btnBack)}></div>);
const ButtonNextContent =
gallerySettings.components?.nextButton ??
(() => <div className={clsx(styles.navBtnContent, styles.btnNext)}></div>);
let slideWidth;
if (width && gallerySettings.slideMinWidth && !gallerySettings.visibleSlides) {
const maxSlides = Math.floor(width / gallerySettings.slideMinWidth);
slideWidth = width / maxSlides;
if (gallerySettings.slideMaxWidth && slideWidth > gallerySettings.slideMaxWidth)
slideWidth = gallerySettings.slideMaxWidth;
visibleSlides = maxSlides;
}
let containerHeight = gallerySettings.height;
if (gallerySettings.autoHeight) containerHeight = height;
let interval = gallerySettings.interval;
if (typeof interval === 'number') {
if (interval < 400) interval = 400;
}
const galleryJsx = (
<CarouselProvider
orientation={gallerySettings.orientation}
visibleSlides={visibleSlides}
naturalSlideWidth={gallerySettings.ratio ? 100 * gallerySettings.ratio : 125}
naturalSlideHeight={100}
totalSlides={totalSlides}
infinite={gallerySettings.loop}
interval={interval}
isPlaying={gallerySettings.autoPlay}
>
<CarouselOnChangeWatcher onChange={this.onActiveSlideChange} />
<CarouselStoreSetter
setStore={(store) => {
this.galleryStore = store;
}}
/>
<div
className={clsx(styles.container)}
style={{
height: containerHeight ? containerHeight + 'px' : '100%',
width: gallerySettings.width ? gallerySettings.width + 'px' : '100%',
}}
>
<Slider
style={
containerHeight !== undefined
? {
height: containerHeight + 'px',
}
: undefined
}
>
{gallerySettings.images &&
gallerySettings.images.map((img, index) => {
if (!img.src) return <></>;
let imgItem = (
<Image
src={img.src}
style={
containerHeight !== undefined
? {
height: containerHeight + 'px',
}
: undefined
}
alt={img.alt}
className={clsx(
gallerySettings?.backgroundSize === 'contain' ? styles.slideContain : styles.slideCover,
)}
/>
);
if (gallerySettings.components?.imgWrapper) {
const WrapComp = gallerySettings.components.imgWrapper;
imgItem = <WrapComp image={img}>{imgItem}</WrapComp>;
}
if (img.href) {
imgItem = <Link href={img.href}>{imgItem}</Link>;
}
imgItem = (
<Slide
onClick={() => this.onSlideClick(index)}
index={index}
key={img.src + index}
style={{
height: containerHeight && containerHeight + 'px',
}}
>
{imgItem}
</Slide>
);
return imgItem;
})}
{gallerySettings.slides &&
gallerySettings.slides.map((slideJsx, index) => {
let el = slideJsx;
if (gallerySettings.components?.imgWrapper) {
const WrapComp = gallerySettings.components.imgWrapper;
el = <WrapComp>{el}</WrapComp>;
}
return (
<Slide
onClick={() => this.onSlideClick(index)}
index={index}
key={`slide_${index}`}
className={gallerySettings?.classes?.slide}
style={{
height: containerHeight && height + 'px',
// width: slideWidth && slideWidth + 'px',
}}
>
{el}
</Slide>
);
})}
</Slider>
{gallerySettings.pagination && (
<div className={styles.dotContainer}>
<DotGroup className={styles.dotGroup} />
</div>
)}
{gallerySettings.navigation && (
<>
<ButtonBack className={clsx(styles.navBtn, styles.navBtnBack, gallerySettings.classes?.navBtn)}>
<ButtonBackContent />
</ButtonBack>
<ButtonNext className={clsx(styles.navBtn, styles.navBtnNext, gallerySettings.classes?.navBtn)}>
<ButtonNextContent />
</ButtonNext>
</>
)}
</div>
</CarouselProvider>
);
return (
<>
{galleryJsx}
{!!(gallerySettings?.thumbs && gallerySettings?.images?.length) && (
<Thumbs
thumbsId={this.thumbsId}
gallerySettings={gallerySettings}
width={width}
activeSlide={this.activeSlide}
totalSlides={totalSlides}
setActiveSlide={this.setActiveSlide}
getUpdate={(forceUpdate) => {
this.forceUpdateThumbs = forceUpdate;
}}
/>
)}
{gallerySettings?.fullscreen && gallerySettings.images && (
<Lightbox
images={gallerySettings.images?.map((img) => img.src) ?? []}
getState={(setOpen) => {
this.setLightbox = setOpen;
}}
/>
)}
</>
);
};
render() {
return (
<CBlock
{...this.props}
type="gallery"
key={this.props.id + '_crw'}
content={(data, ref, setContentInstance) => {
setContentInstance(this);
return (
<ReactResizeDetector handleWidth handleHeight refreshMode="throttle" refreshRate={50}>
{({ targetRef, width, height }) => {
return (
<div id={this.galleryId} className={styles.max} ref={targetRef as any}>
{this.getContent(data, width, height)}
</div>
);
}}
</ReactResizeDetector>
);
}}
/>
);
}
}
/** @internal */
export function CarouselOnChangeWatcher(props: { onChange: (index: number) => void }) {
const carouselContext = useContext(CarouselContext);
useEffect(() => {
function onChange() {
props.onChange(carouselContext.state.currentSlide);
}
carouselContext.subscribe(onChange);
return () => carouselContext.unsubscribe(onChange);
}, [carouselContext]);
return <></>;
}