-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathGallery.jsx
448 lines (409 loc) · 16 KB
/
Gallery.jsx
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/**
* @component
*/
import classNames from 'clsx';
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { isString } from '../../utils/is';
import { isServer } from '../../utils/isServer';
import { getDataUrlFromFile } from '../utils/getDataUrl';
import './Gallery.scss';
import Image from './Image';
import ImageContainer from './ImageContainer';
/**
* An image gallery that displays up to four images by default. Also supports
* reordering and deletion of images and blurred image previews for images
* loaded from `tsimg.cloud`.
*/
export default class Gallery extends Component {
static getBigImageUrls(images) {
return images.map((image) => {
const img = image.url || image.file || image;
return isString(img) ? img : getDataUrlFromFile(img);
});
}
constructor(props) {
super(props);
this.galleryRef = React.createRef();
this.state = {
active: null,
images: props.images,
dropzone: null,
galleryWidth: null,
};
}
componentDidMount() {
this.setState({ galleryWidth: this.galleryRef.current.offsetWidth });
}
componentDidUpdate(prevProps) {
const { images } = this.props;
if (prevProps.images !== images) {
// eslint-disable-next-line react/no-did-update-set-state
this.setState({ images });
}
}
onDown = (event, index, image) => {
// deactivate refresh scroll in apps
if (chayns.env.isApp || chayns.env.isMyChaynsApp)
chayns.disallowRefreshScroll();
this.index = index;
this.image = image;
this.pageXStart = event.changedTouches
? event.changedTouches[0].pageX
: event.pageX;
this.pageYStart = event.changedTouches
? event.changedTouches[0].pageY
: event.pageY;
this.selectedElement =
event.target.parentElement.parentElement.parentElement;
this.selectedElementStartPosition =
this.selectedElement.getBoundingClientRect();
this.galleryStartPosition =
this.galleryRef.current.getBoundingClientRect();
this.galleryOffsetX = this.galleryStartPosition.left;
this.galleryOffsetY = this.galleryStartPosition.top;
this.offsetX = this.pageXStart - this.selectedElementStartPosition.left;
this.offsetY = this.pageYStart - this.selectedElementStartPosition.top;
document.addEventListener('mousemove', this.onMove);
document.addEventListener('touchmove', this.onMove, { passive: false });
document.addEventListener('mouseup', this.onUp);
document.addEventListener('touchend', this.onUp);
document.addEventListener('touchcancel', this.onUp);
};
onMove = (event) => {
event.preventDefault();
const { pageX, pageY } = event.changedTouches
? event.changedTouches[0]
: event;
const { clientWidth: galleryWidth } = this.galleryRef.current;
const { clientHeight: itemHeight, clientWidth: itemWidth } =
event.target.parentElement.parentElement.parentElement;
// move item
this.selectedElement.style.left = `${
pageX - this.galleryOffsetX - this.offsetX
}px`;
this.selectedElement.style.top = `${
pageY - this.galleryOffsetY - this.offsetY
}px`;
// determine new position
const itemsPerRow = Math.round(galleryWidth / itemWidth);
const middleX =
pageX - this.galleryOffsetX - this.offsetX + itemWidth / 2;
const middleY =
pageY - this.galleryOffsetY - this.offsetY + itemHeight / 2;
const row = Math.floor(middleY / itemHeight);
const column = Math.floor(middleX / itemWidth);
this.newPosition = row * itemsPerRow + column;
const { dropzone: oldDropzone } = this.state;
const newDropzone =
this.newPosition + (this.newPosition > this.index ? 1 : 0);
if (oldDropzone !== newDropzone) {
this.setState({
dropzone: newDropzone,
active: this.index,
});
}
// show corresponding dropzone
let insertPosition = this.newPosition * 2; // dropzones and images are alternating
if (this.newPosition > this.index) {
insertPosition += 2;
}
const dropzone = this.galleryRef.current.children[insertPosition];
this.lastDropzone = dropzone;
};
onUp = () => {
if (chayns.env.isApp || chayns.env.isMyChaynsApp)
chayns.allowRefreshScroll();
document.removeEventListener('mousemove', this.onMove);
document.removeEventListener('touchmove', this.onMove);
document.removeEventListener('mouseup', this.onUp);
document.removeEventListener('touchend', this.onUp);
document.removeEventListener('touchcancel', this.onUp);
if (this.lastDropzone) {
// there's no lastDropzone if user hasn't moved
const { onDragEnd, images } = this.props;
const rect = this.lastDropzone.getBoundingClientRect();
this.selectedElement.classList.add(
'cc__gallery__image--transition'
);
this.selectedElement.style.left = `${
rect.left - this.galleryOffsetX
}px`;
this.selectedElement.style.top = `${
rect.top - this.galleryOffsetY
}px`;
const onTransitionEnd = () => {
if (this.selectedElement && !this.transitionEnded) {
this.transitionEnded = true;
this.selectedElement.removeEventListener(
'transitionend',
onTransitionEnd
);
this.selectedElement.classList.remove(
'cc__gallery__image--transition'
);
const image = images[this.index];
const newArray = images.slice();
newArray.splice(this.index, 1);
newArray.splice(this.newPosition, 0, image);
if (onDragEnd) {
onDragEnd(newArray);
}
this.setState({
dropzone: null,
active: null,
images: newArray,
});
}
};
this.transitionEnded = false;
this.selectedElement.addEventListener(
'transitionend',
onTransitionEnd
);
} else {
this.selectedElement.classList.remove('cc__gallery__image--active');
}
// Enable scrolling.
document.ontouchmove = () => true;
};
render() {
const {
height,
width,
onDelete,
deleteMode,
dragMode,
className,
preventParams,
stopPropagation,
onClick,
smallTiles,
} = this.props;
const { style: propStyle } = this.props;
const style = { ...propStyle };
const defaultMode = !dragMode && !deleteMode && !smallTiles;
const {
active,
dropzone: dropzoneId,
images,
galleryWidth,
} = this.state;
let styleHeight;
if (defaultMode) {
if (height) {
styleHeight = height;
} else if (galleryWidth < 420) {
styleHeight = galleryWidth;
} else {
styleHeight = 420;
}
style.height = `${styleHeight}px`;
}
if (width) {
style.width = width;
}
const numberOfImages = images.length;
const dropzone = (key, show) => (
<div
key={key}
id={key}
className={classNames(
'cc__gallery__image cc__gallery__image--dropzone',
{ 'cc__gallery__image--show_dropzone': show }
)}
>
<ImageContainer>
<div
className={classNames(
'cc__gallery__image__dropzone chayns__background-color--101 chayns__border-color--300'
)}
/>
</ImageContainer>
</div>
);
return (
<div
className={classNames('cc__gallery', className, {
'cc__gallery--default-mode': defaultMode,
'cc__gallery--delete-mode': deleteMode,
'cc__gallery--drag-mode': dragMode,
})}
style={style}
ref={this.galleryRef}
key="gallery"
>
{dragMode ? dropzone('dropzone', dropzoneId === 0) : null}
{images.map((image, index) => {
if (index < 4 || deleteMode || dragMode) {
const tools = [];
if (dragMode && images.length > 1) {
// Show drag icon only if a reorder is possible
tools.push({
icon: 'ts-bars',
className: 'cc__gallery__image__tool--drag',
onDown: (event) => {
event.preventDefault();
this.onDown(event, index, image);
},
noScroll: true,
});
}
if (deleteMode) {
tools.push({
icon: 'ts-wrong',
onClick: () => {
onDelete(image, index);
},
});
}
return [
<div
className={classNames('cc__gallery__image', {
'cc__gallery__image--active':
index === active,
})}
id={`image${index}`}
// eslint-disable-next-line react/no-array-index-key
key={`imageDiv${index}`}
>
<ImageContainer tools={tools}>
<Image
// eslint-disable-next-line react/no-array-index-key
key={`image${index}`}
preventParams={preventParams}
image={image.url || image.file || image}
moreImages={
index === 3 && defaultMode
? numberOfImages - 1 - index
: 0
}
onClick={
onClick || defaultMode
? (event) => {
if (stopPropagation)
event.stopPropagation();
if (onClick) {
onClick(
Gallery.getBigImageUrls(
images
),
index
);
} else if (defaultMode) {
chayns.openImage(
Gallery.getBigImageUrls(
images
),
index
);
}
}
: null
}
className="cc__gallery__image--cover"
/>
</ImageContainer>
</div>,
dragMode
? dropzone(
`dropzone${index}`,
dropzoneId === index + 1
)
: null,
];
}
return null;
})}
</div>
);
}
}
Gallery.propTypes = {
/**
* An array of strings or File objects that will be the image sources.
*/
images: isServer() // eslint-disable-line react/require-default-props
? PropTypes.array.isRequired
: PropTypes.arrayOf(
PropTypes.oneOfType([
PropTypes.shape({
url: PropTypes.string.isRequired,
}),
PropTypes.shape({
file: PropTypes.instanceOf(File).isRequired,
}),
PropTypes.string,
PropTypes.instanceOf(File),
]).isRequired
).isRequired,
/**
* A function that is called when an Image is clicked.
*/
onClick: PropTypes.func,
/**
* A function that is called when an image is deleted in deletion mode.
*/
onDelete: PropTypes.func,
/**
* Wether the deletion mode is active.
*/
deleteMode: PropTypes.bool,
/**
* The height of the gallery as a number of pixels or CSS string.
*/
height: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/**
* The width of the gallery as a number of pixels or CSS string.
*/
width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/**
* A classname string that will be applied to the root container.
*/
className: PropTypes.string,
/**
* A React style object that is applied to the root container.
*/
style: PropTypes.objectOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.number])
),
/**
* Wether to stop propagation of click events to parent elements.
*/
stopPropagation: PropTypes.bool,
/**
* Wether drag mode is active.
*/
dragMode: PropTypes.bool,
/**
* Called after the user finished reordering the array. Receives the new
* array as its first parameter.
*/
onDragEnd: PropTypes.func,
/**
* This will be forwarded to the `Image`-component. It prevents parameters
* of the loaded image. E.g. supply `{ width: true }` to prevent the
* `width`-parameter on the loaded image.
*/
preventParams: PropTypes.bool,
/**
* This option changes the layout to the layout known from delete- and
* drag-mode without activating this modes.
*/
smallTiles: PropTypes.bool,
};
Gallery.defaultProps = {
onClick: null,
onDelete: null,
deleteMode: false,
height: null,
width: null,
className: null,
style: {},
stopPropagation: false,
dragMode: false,
onDragEnd: null,
preventParams: false,
smallTiles: false,
};
Gallery.displayName = 'Gallery';