react-editable-photo-grid 2.3.7 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,188 +1,188 @@
1
- import React, { useEffect, useState, TouchEvent } from 'react'
2
- import { imgSrcProperty, GalleryButtonArrows, PhotoItem } from '../types';
3
- import { getImageSrcProperty, photoHasDetails } from "../utils";
4
-
5
- type GalleryProps = {
6
- photos: PhotoItem[],
7
- activeKey: number,
8
- setActiveKey: (key: number) => void,
9
- imageSrcPrefix: string;
10
- imageSrcProperty: imgSrcProperty;
11
- buttonArrows?: GalleryButtonArrows;
12
- onGallerySwipe?: (photo: PhotoItem) => void;
13
- }
14
-
15
- const NonScrollGallery = (props: GalleryProps) => {
16
- const [touchPosition, setTouchPosition] = useState<number | null>(null),
17
- [visible, setVisible] = useState<boolean>(false);
18
-
19
- const getSrcProperty = (photo: PhotoItem): string => {
20
- const property = props.imageSrcProperty as string;
21
- return getImageSrcProperty(photo, property);
22
- }
23
-
24
- const showGallery = () => {
25
- setVisible(true);
26
- }
27
-
28
- const handleSwipe = () => {
29
- if (props.onGallerySwipe) {
30
- const photoKey = props.activeKey,
31
- photo: PhotoItem = props.photos[photoKey];
32
-
33
- if (photo) {
34
- props.onGallerySwipe(photo);
35
- }
36
- }
37
- }
38
-
39
- const shouldRenderImage = (index: number) => {
40
- const key = props.activeKey;
41
- let validIndexes = [key];
42
-
43
- if (key > 0) {
44
- validIndexes.push(key - 1);
45
- }
46
-
47
- if (key < props.photos.length - 1) {
48
- validIndexes.push(key + 1);
49
- }
50
-
51
- return validIndexes.includes(index);
52
- }
53
-
54
- const prevItem = (e: React.MouseEvent<HTMLButtonElement>) => {
55
- e.preventDefault()
56
- if (props.activeKey > 0) {
57
- props.setActiveKey(props.activeKey - 1);
58
- handleSwipe();
59
- }
60
- }
61
-
62
- const nextItem = (e: React.MouseEvent<HTMLButtonElement>) => {
63
- e.preventDefault()
64
- if (props.activeKey < props.photos.length - 1) {
65
- props.setActiveKey(props.activeKey + 1);
66
- handleSwipe();
67
- }
68
- }
69
-
70
- const keydownHandler = (e: KeyboardEvent) => {
71
- if (e.keyCode === 39) {
72
- if (props.activeKey < props.photos.length - 1) {
73
- props.setActiveKey(props.activeKey + 1);
74
- }
75
- } else if (e.keyCode === 37) {
76
- if (props.activeKey > 0) {
77
- props.setActiveKey(props.activeKey - 1);
78
- }
79
- }
80
- handleSwipe();
81
- }
82
-
83
- const hideGallery = () => {
84
- setVisible(false);
85
- props.setActiveKey(-1);
86
- }
87
-
88
- const disableRightClick = () => {
89
- return false;
90
- }
91
-
92
- const handleTouchStart = (e: TouchEvent<HTMLDivElement>) => {
93
- const touchDown = e.touches[0].clientX;
94
- setTouchPosition(touchDown);
95
- }
96
-
97
- const handleTouchMove = (e: TouchEvent<HTMLDivElement>) => {
98
- if (touchPosition === null) {
99
- return;
100
- }
101
-
102
- const currentTouch = e.touches[0].clientX,
103
- diff = touchPosition - currentTouch;
104
-
105
- if (diff > 5) {
106
- if (props.activeKey < props.photos.length - 1) {
107
- props.setActiveKey(props.activeKey + 1);
108
- }
109
- }
110
-
111
- if (diff < -5) {
112
- if (props.activeKey > 0) {
113
- props.setActiveKey(props.activeKey - 1);
114
- }
115
- }
116
-
117
- handleSwipe();
118
- setTouchPosition(null);
119
- }
120
-
121
- useEffect(() => {
122
- if (props.activeKey > -1) {
123
- if (visible === false) {
124
- showGallery()
125
- }
126
- }
127
-
128
- document.addEventListener('keydown', keydownHandler);
129
-
130
- return function cleanup() {
131
- document.removeEventListener('keydown', keydownHandler);
132
- };
133
-
134
- }, [props.activeKey])
135
-
136
- return (
137
- <div
138
- className="custom__gallery"
139
- style={{ display: visible === true ? 'block' : 'none' }}
140
- onTouchStart={handleTouchStart}
141
- onTouchMove={handleTouchMove}
142
- >
143
- <div className="clearfix">
144
- <button className="close__gallery" onClick={hideGallery} type="button">&times;</button>
145
- </div>
146
- <div className="custom__gallery__container">
147
- {props.activeKey > 0 &&
148
- <button
149
- onClick={prevItem}
150
- className="previous__item"
151
- dangerouslySetInnerHTML={{ __html: props.buttonArrows ? props.buttonArrows.prev : '&#8592' }}
152
- />
153
- }
154
- <div className="custom__gallery__images">
155
- {props.photos.length &&
156
- props.photos.map((photo, i) =>
157
- <div key={`${i}`} className={props.activeKey == i ? "custom__gallery__item active" : "custom__gallery__item"}>
158
- <img
159
- src={shouldRenderImage(i) ? `${props.imageSrcPrefix}${getSrcProperty(photo)}` : ''}
160
- alt="gallery item"
161
- onContextMenu={disableRightClick}
162
- />
163
- {photoHasDetails(photo) === true && (
164
- <div className="custom__gallery__photo__overlay">
165
- {photo.name != undefined && photo.name !== '' && (
166
- <h4>{photo.name}</h4>
167
- )}
168
- {photo.description != undefined && photo.description !== '' && (
169
- <p>{photo.description}</p>
170
- )}
171
- </div>
172
- )}
173
- </div>
174
- )}
175
- </div>
176
- {props.activeKey < props.photos.length - 1 &&
177
- <button
178
- onClick={nextItem}
179
- className="next__item"
180
- dangerouslySetInnerHTML={{ __html: props.buttonArrows ? props.buttonArrows.next : '&#8594' }}
181
- />
182
- }
183
- </div>
184
- </div>
185
- )
186
- }
187
-
1
+ import React, { useEffect, useState, TouchEvent } from 'react'
2
+ import { imgSrcProperty, GalleryButtonArrows, PhotoItem } from '../types';
3
+ import { getImageSrcProperty, photoHasDetails } from "../utils";
4
+
5
+ type GalleryProps = {
6
+ photos: PhotoItem[],
7
+ activeKey: number,
8
+ setActiveKey: (key: number) => void,
9
+ imageSrcPrefix: string;
10
+ imageSrcProperty: imgSrcProperty;
11
+ buttonArrows?: GalleryButtonArrows;
12
+ onGallerySwipe?: (photo: PhotoItem) => void;
13
+ }
14
+
15
+ const NonScrollGallery = (props: GalleryProps) => {
16
+ const [touchPosition, setTouchPosition] = useState<number | null>(null),
17
+ [visible, setVisible] = useState<boolean>(false);
18
+
19
+ const getSrcProperty = (photo: PhotoItem): string => {
20
+ const property = props.imageSrcProperty as string;
21
+ return getImageSrcProperty(photo, property);
22
+ }
23
+
24
+ const showGallery = () => {
25
+ setVisible(true);
26
+ }
27
+
28
+ const handleSwipe = () => {
29
+ if (props.onGallerySwipe) {
30
+ const photoKey = props.activeKey,
31
+ photo: PhotoItem = props.photos[photoKey];
32
+
33
+ if (photo) {
34
+ props.onGallerySwipe(photo);
35
+ }
36
+ }
37
+ }
38
+
39
+ const shouldRenderImage = (index: number) => {
40
+ const key = props.activeKey;
41
+ let validIndexes = [key];
42
+
43
+ if (key > 0) {
44
+ validIndexes.push(key - 1);
45
+ }
46
+
47
+ if (key < props.photos.length - 1) {
48
+ validIndexes.push(key + 1);
49
+ }
50
+
51
+ return validIndexes.includes(index);
52
+ }
53
+
54
+ const prevItem = (e: React.MouseEvent<HTMLButtonElement>) => {
55
+ e.preventDefault()
56
+ if (props.activeKey > 0) {
57
+ props.setActiveKey(props.activeKey - 1);
58
+ handleSwipe();
59
+ }
60
+ }
61
+
62
+ const nextItem = (e: React.MouseEvent<HTMLButtonElement>) => {
63
+ e.preventDefault()
64
+ if (props.activeKey < props.photos.length - 1) {
65
+ props.setActiveKey(props.activeKey + 1);
66
+ handleSwipe();
67
+ }
68
+ }
69
+
70
+ const keydownHandler = (e: KeyboardEvent) => {
71
+ if (e.keyCode === 39) {
72
+ if (props.activeKey < props.photos.length - 1) {
73
+ props.setActiveKey(props.activeKey + 1);
74
+ }
75
+ } else if (e.keyCode === 37) {
76
+ if (props.activeKey > 0) {
77
+ props.setActiveKey(props.activeKey - 1);
78
+ }
79
+ }
80
+ handleSwipe();
81
+ }
82
+
83
+ const hideGallery = () => {
84
+ setVisible(false);
85
+ props.setActiveKey(-1);
86
+ }
87
+
88
+ const disableRightClick = () => {
89
+ return false;
90
+ }
91
+
92
+ const handleTouchStart = (e: TouchEvent<HTMLDivElement>) => {
93
+ const touchDown = e.touches[0].clientX;
94
+ setTouchPosition(touchDown);
95
+ }
96
+
97
+ const handleTouchMove = (e: TouchEvent<HTMLDivElement>) => {
98
+ if (touchPosition === null) {
99
+ return;
100
+ }
101
+
102
+ const currentTouch = e.touches[0].clientX,
103
+ diff = touchPosition - currentTouch;
104
+
105
+ if (diff > 5) {
106
+ if (props.activeKey < props.photos.length - 1) {
107
+ props.setActiveKey(props.activeKey + 1);
108
+ }
109
+ }
110
+
111
+ if (diff < -5) {
112
+ if (props.activeKey > 0) {
113
+ props.setActiveKey(props.activeKey - 1);
114
+ }
115
+ }
116
+
117
+ handleSwipe();
118
+ setTouchPosition(null);
119
+ }
120
+
121
+ useEffect(() => {
122
+ if (props.activeKey > -1) {
123
+ if (visible === false) {
124
+ showGallery()
125
+ }
126
+ }
127
+
128
+ document.addEventListener('keydown', keydownHandler);
129
+
130
+ return function cleanup() {
131
+ document.removeEventListener('keydown', keydownHandler);
132
+ };
133
+
134
+ }, [props.activeKey])
135
+
136
+ return (
137
+ <div
138
+ className="custom__gallery"
139
+ style={{ display: visible === true ? 'block' : 'none' }}
140
+ onTouchStart={handleTouchStart}
141
+ onTouchMove={handleTouchMove}
142
+ >
143
+ <div className="clearfix">
144
+ <button className="close__gallery" onClick={hideGallery} type="button">&times;</button>
145
+ </div>
146
+ <div className="custom__gallery__container">
147
+ {props.activeKey > 0 &&
148
+ <button
149
+ onClick={prevItem}
150
+ className="previous__item"
151
+ dangerouslySetInnerHTML={{ __html: props.buttonArrows ? props.buttonArrows.prev : '&#8592' }}
152
+ />
153
+ }
154
+ <div className="custom__gallery__images">
155
+ {props.photos.length &&
156
+ props.photos.map((photo, i) =>
157
+ <div key={`${i}`} className={props.activeKey == i ? "custom__gallery__item active" : "custom__gallery__item"}>
158
+ <img
159
+ src={shouldRenderImage(i) ? `${props.imageSrcPrefix}${getSrcProperty(photo)}` : ''}
160
+ alt="gallery item"
161
+ onContextMenu={disableRightClick}
162
+ />
163
+ {photoHasDetails(photo) === true && (
164
+ <div className="custom__gallery__photo__overlay">
165
+ {photo.name != undefined && photo.name !== '' && (
166
+ <h4>{photo.name}</h4>
167
+ )}
168
+ {photo.description != undefined && photo.description !== '' && (
169
+ <p>{photo.description}</p>
170
+ )}
171
+ </div>
172
+ )}
173
+ </div>
174
+ )}
175
+ </div>
176
+ {props.activeKey < props.photos.length - 1 &&
177
+ <button
178
+ onClick={nextItem}
179
+ className="next__item"
180
+ dangerouslySetInnerHTML={{ __html: props.buttonArrows ? props.buttonArrows.next : '&#8594' }}
181
+ />
182
+ }
183
+ </div>
184
+ </div>
185
+ )
186
+ }
187
+
188
188
  export default NonScrollGallery;
@@ -1,61 +1,61 @@
1
- import React from 'react';
2
- import { PhotoControlsProps } from '../types';
3
- import { castRowKey } from '../utils';
4
- import '../styles.css';
5
-
6
- const PhotoControls = (props: PhotoControlsProps) => {
7
- return (
8
- <ul className="photogrid--photo__controls">
9
- {props.photo.column > 1 &&
10
- <li>
11
- <button
12
- type="button"
13
- className="photo__control move__photo__left"
14
- onClick={props.movePhotoLeft}
15
- data-id={props.photo.id}
16
- data-row={props.rowKey}
17
- dangerouslySetInnerHTML={{ __html: props.buttonArrows ? props.buttonArrows.left : '&#8592' }}
18
- />
19
- </li>
20
- }
21
- {castRowKey(props.rowKey) > 1 &&
22
- <li>
23
- <button
24
- type="button"
25
- className="photo__control move__photo__up"
26
- onClick={props.movePhotoUp}
27
- data-id={props.photo.id}
28
- data-row={props.rowKey}
29
- dangerouslySetInnerHTML={{ __html: props.buttonArrows ? props.buttonArrows.up : '&#8593' }}
30
- />
31
- </li>
32
- }
33
- {castRowKey(props.rowKey) < props.rowCount || castRowKey(props.rowKey) === props.rowCount && props.photoCount > 1 ?
34
- <li>
35
- <button
36
- type="button"
37
- className="photo__control move__photo__down"
38
- onClick={props.movePhotoDown}
39
- data-id={props.photo.id}
40
- data-row={props.rowKey}
41
- dangerouslySetInnerHTML={{ __html: props.buttonArrows ? props.buttonArrows.down : '&#8595' }}
42
- />
43
- </li> : null
44
- }
45
- {props.photo.column < props.photoCount &&
46
- <li>
47
- <button
48
- type="button"
49
- className="photo__control move__photo__right"
50
- onClick={props.movePhotoRight}
51
- data-id={props.photo.id}
52
- data-row={props.rowKey}
53
- dangerouslySetInnerHTML={{ __html: props.buttonArrows ? props.buttonArrows.right : '&#8594' }}
54
- />
55
- </li>
56
- }
57
- </ul>
58
- );
59
- };
60
-
1
+ import React from 'react';
2
+ import { PhotoControlsProps } from '../types';
3
+ import { castRowKey } from '../utils';
4
+ import '../styles.css';
5
+
6
+ const PhotoControls = (props: PhotoControlsProps) => {
7
+ return (
8
+ <ul className="photogrid--photo__controls">
9
+ {props.photo.column > 1 &&
10
+ <li>
11
+ <button
12
+ type="button"
13
+ className="photo__control move__photo__left"
14
+ onClick={props.movePhotoLeft}
15
+ data-id={props.photo.id}
16
+ data-row={props.rowKey}
17
+ dangerouslySetInnerHTML={{ __html: props.buttonArrows ? props.buttonArrows.left : '&#8592' }}
18
+ />
19
+ </li>
20
+ }
21
+ {castRowKey(props.rowKey) > 1 &&
22
+ <li>
23
+ <button
24
+ type="button"
25
+ className="photo__control move__photo__up"
26
+ onClick={props.movePhotoUp}
27
+ data-id={props.photo.id}
28
+ data-row={props.rowKey}
29
+ dangerouslySetInnerHTML={{ __html: props.buttonArrows ? props.buttonArrows.up : '&#8593' }}
30
+ />
31
+ </li>
32
+ }
33
+ {castRowKey(props.rowKey) < props.rowCount || castRowKey(props.rowKey) === props.rowCount && props.photoCount > 1 ?
34
+ <li>
35
+ <button
36
+ type="button"
37
+ className="photo__control move__photo__down"
38
+ onClick={props.movePhotoDown}
39
+ data-id={props.photo.id}
40
+ data-row={props.rowKey}
41
+ dangerouslySetInnerHTML={{ __html: props.buttonArrows ? props.buttonArrows.down : '&#8595' }}
42
+ />
43
+ </li> : null
44
+ }
45
+ {props.photo.column < props.photoCount &&
46
+ <li>
47
+ <button
48
+ type="button"
49
+ className="photo__control move__photo__right"
50
+ onClick={props.movePhotoRight}
51
+ data-id={props.photo.id}
52
+ data-row={props.rowKey}
53
+ dangerouslySetInnerHTML={{ __html: props.buttonArrows ? props.buttonArrows.right : '&#8594' }}
54
+ />
55
+ </li>
56
+ }
57
+ </ul>
58
+ );
59
+ };
60
+
61
61
  export default PhotoControls;
@@ -1,33 +1,33 @@
1
- import React from 'react';
2
- import { RowControlsProps } from "../types";
3
- import { castRowKey } from '../utils';
4
- import'../styles.css';
5
-
6
- const RowControls = (props: RowControlsProps) => {
7
- return (
8
- <ul className='photogrid--row__controls'>
9
- {castRowKey(props.rowKey) > 1 &&
10
- <li>
11
- <button
12
- className="row__control move__row__up"
13
- onClick={props.moveRowUp}
14
- data-row={props.rowKey}
15
- dangerouslySetInnerHTML={{ __html: props.buttonArrows ? props.buttonArrows.up : '&#8593' }}
16
- />
17
- </li>
18
- }
19
- {castRowKey(props.rowKey) < props.rowCount &&
20
- <li>
21
- <button
22
- className="row__control move__row__down"
23
- onClick={props.moveRowDown}
24
- data-row={props.rowKey}
25
- dangerouslySetInnerHTML={{ __html: props.buttonArrows ? props.buttonArrows.down : '&#8595' }}
26
- />
27
- </li>
28
- }
29
- </ul>
30
- );
31
- };
32
-
1
+ import React from 'react';
2
+ import { RowControlsProps } from "../types";
3
+ import { castRowKey } from '../utils';
4
+ import'../styles.css';
5
+
6
+ const RowControls = (props: RowControlsProps) => {
7
+ return (
8
+ <ul className='photogrid--row__controls'>
9
+ {castRowKey(props.rowKey) > 1 &&
10
+ <li>
11
+ <button
12
+ className="row__control move__row__up"
13
+ onClick={props.moveRowUp}
14
+ data-row={props.rowKey}
15
+ dangerouslySetInnerHTML={{ __html: props.buttonArrows ? props.buttonArrows.up : '&#8593' }}
16
+ />
17
+ </li>
18
+ }
19
+ {castRowKey(props.rowKey) < props.rowCount &&
20
+ <li>
21
+ <button
22
+ className="row__control move__row__down"
23
+ onClick={props.moveRowDown}
24
+ data-row={props.rowKey}
25
+ dangerouslySetInnerHTML={{ __html: props.buttonArrows ? props.buttonArrows.down : '&#8595' }}
26
+ />
27
+ </li>
28
+ }
29
+ </ul>
30
+ );
31
+ };
32
+
33
33
  export default RowControls;