@@ -54,16 +54,14 @@ export default class Gallery extends Component {
54
54
constructor ( props ) {
55
55
super ( props ) ;
56
56
this . galleryRef = React . createRef ( ) ;
57
+ this . state = { active : null , images : props . images , dropzone : null } ;
57
58
}
58
59
59
- componentDidUpdate ( prevProps ) {
60
- const { images } = this . props ;
61
- if ( prevProps . images !== images && this . selectedElement && this . lastDropzone ) {
62
- this . selectedElement . classList . remove ( 'cc__gallery__image--active' ) ;
63
- this . lastDropzone . classList . remove ( 'cc__gallery__image--show_dropzone' ) ;
64
- this . selectedElement = null ;
60
+ componentWillUpdate ( nextProps , nextState ) {
61
+ if ( nextProps . images !== nextState . images ) {
62
+ // eslint-disable-next-line react/no-will-update-set-state
63
+ this . setState ( { images : nextProps . images } ) ;
65
64
}
66
- return true ;
67
65
}
68
66
69
67
onDown = ( event , index , image ) => {
@@ -83,23 +81,18 @@ export default class Gallery extends Component {
83
81
this . offsetY = this . pageYStart - this . selectedElementStartPosition . top ;
84
82
85
83
document . addEventListener ( 'mousemove' , this . onMove ) ;
86
- document . addEventListener ( 'touchmove' , this . onMove ) ;
84
+ document . addEventListener ( 'touchmove' , this . onMove , { passive : false } ) ;
87
85
document . addEventListener ( 'mouseup' , this . onUp ) ;
88
86
document . addEventListener ( 'touchend' , this . onUp ) ;
89
87
document . addEventListener ( 'touchcancel' , this . onUp ) ;
90
88
} ;
91
89
92
90
onMove = ( event ) => {
93
-
91
+ event . preventDefault ( ) ;
94
92
const { pageX, pageY } = event . changedTouches ? event . changedTouches [ 0 ] : event ;
95
93
const { clientWidth : galleryWidth } = this . galleryRef . current ;
96
94
const { clientHeight : itemHeight , clientWidth : itemWidth } = event . target . parentElement . parentElement . parentElement ;
97
95
98
- // set selected element active
99
- if ( ! this . selectedElement . classList . contains ( 'cc__gallery__image--active' ) ) {
100
- this . selectedElement . classList . add ( 'cc__gallery__image--active' ) ;
101
- }
102
-
103
96
// move item
104
97
this . selectedElement . style . left = `${ pageX - this . galleryOffsetX - this . offsetX } px` ;
105
98
this . selectedElement . style . top = `${ pageY - this . galleryOffsetY - this . offsetY } px` ;
@@ -111,24 +104,24 @@ export default class Gallery extends Component {
111
104
const row = Math . floor ( middleY / itemHeight ) ;
112
105
const column = Math . floor ( middleX / itemWidth ) ;
113
106
this . newPosition = ( row * itemsPerRow ) + column ;
107
+ const { dropzone : oldDropzone } = this . state ;
108
+ const newDropzone = this . newPosition + ( this . newPosition > this . index ? 1 : 0 ) ;
109
+ if ( oldDropzone !== newDropzone ) {
110
+ this . setState ( { dropzone : newDropzone , active : this . index } ) ;
111
+ }
114
112
115
113
// show corresponding dropzone
116
114
let insertPosition = this . newPosition * 2 ; // dropzones and images are alternating
117
115
if ( this . newPosition > this . index ) {
118
116
insertPosition += 2 ;
119
117
}
120
118
const dropzone = this . galleryRef . current . children [ insertPosition ] ;
121
- if ( this . lastDropzone && this . lastDropzone !== dropzone ) {
122
- this . lastDropzone . classList . remove ( 'cc__gallery__image--show_dropzone' ) ;
123
- }
124
- if ( dropzone ) {
125
- dropzone . classList . add ( 'cc__gallery__image--show_dropzone' ) ;
126
- }
127
119
this . lastDropzone = dropzone ;
128
120
} ;
129
121
130
122
onUp = ( ) => {
131
123
if ( chayns . env . isApp || chayns . env . isMyChaynsApp ) chayns . allowRefreshScroll ( ) ;
124
+
132
125
document . removeEventListener ( 'mousemove' , this . onMove ) ;
133
126
document . removeEventListener ( 'touchmove' , this . onMove ) ;
134
127
document . removeEventListener ( 'mouseup' , this . onUp ) ;
@@ -147,20 +140,27 @@ export default class Gallery extends Component {
147
140
this . selectedElement . removeEventListener ( 'transitionend' , onTransitionEnd ) ;
148
141
this . selectedElement . classList . remove ( 'cc__gallery__image--transition' ) ;
149
142
143
+ const image = images [ this . index ] ;
144
+ const newArray = images . slice ( ) ;
145
+ newArray . splice ( this . index , 1 ) ;
146
+ newArray . splice ( this . newPosition , 0 , image ) ;
147
+
150
148
if ( onDragEnd ) {
151
- const image = images [ this . index ] ;
152
- const newArray = images . slice ( ) ;
153
- newArray . splice ( this . index , 1 ) ;
154
- newArray . splice ( this . newPosition , 0 , image ) ;
155
149
onDragEnd ( newArray ) ;
156
150
}
151
+
152
+ this . setState ( { dropzone : null , active : null , images : newArray } ) ;
157
153
}
158
154
} ;
159
155
this . transitionEnded = false ;
160
156
this . selectedElement . addEventListener ( 'transitionend' , onTransitionEnd ) ;
161
157
} else {
162
158
this . selectedElement . classList . remove ( 'cc__gallery__image--active' ) ;
163
159
}
160
+ // Enable scrolling.
161
+ document . ontouchmove = function ( e ) {
162
+ return true ;
163
+ } ;
164
164
} ;
165
165
166
166
render ( ) {
@@ -173,11 +173,11 @@ export default class Gallery extends Component {
173
173
className,
174
174
stopPropagation,
175
175
onClick,
176
- images,
177
176
} = this . props ;
178
177
const { style : propStyle } = this . props ;
179
178
const style = { ...propStyle } ;
180
179
const defaultMode = ! dragMode && ! deleteMode ;
180
+ const { active, dropzone : dropzoneId , images } = this . state ;
181
181
182
182
let styleHeight ;
183
183
if ( defaultMode ) {
@@ -193,11 +193,15 @@ export default class Gallery extends Component {
193
193
}
194
194
const numberOfImages = images . length ;
195
195
196
- const dropzone = key => (
197
- < div key = { key } id = { key } className = "cc__gallery__image cc__gallery__image--dropzone" >
196
+ const dropzone = ( key , show ) => (
197
+ < div
198
+ key = { key }
199
+ id = { key }
200
+ className = { classNames ( 'cc__gallery__image cc__gallery__image--dropzone' , { 'cc__gallery__image--show_dropzone' : show } ) }
201
+ >
198
202
< ImageContainer >
199
203
< div
200
- className = " cc__gallery__image__dropzone chayns__background-color--101 chayns__border-color--300"
204
+ className = { classNames ( ' cc__gallery__image__dropzone chayns__background-color--101 chayns__border-color--300' ) }
201
205
/>
202
206
</ ImageContainer >
203
207
</ div >
@@ -216,7 +220,7 @@ export default class Gallery extends Component {
216
220
>
217
221
{
218
222
dragMode
219
- ? dropzone ( 'dropzone' )
223
+ ? dropzone ( 'dropzone' , dropzoneId === 0 )
220
224
: null
221
225
}
222
226
{
@@ -228,8 +232,10 @@ export default class Gallery extends Component {
228
232
icon : 'ts-bars' ,
229
233
className : 'cc__gallery__image__tool--drag' ,
230
234
onDown : ( event ) => {
235
+ event . preventDefault ( ) ;
231
236
this . onDown ( event , index , image ) ;
232
237
} ,
238
+ noScroll : true ,
233
239
} ) ;
234
240
}
235
241
if ( deleteMode ) {
@@ -242,7 +248,11 @@ export default class Gallery extends Component {
242
248
}
243
249
244
250
return [
245
- < div className = "cc__gallery__image" id = { `image${ index } ` } key = { `imageDiv${ index } ` } >
251
+ < div
252
+ className = { classNames ( 'cc__gallery__image' , { 'cc__gallery__image--active' : index === active } ) }
253
+ id = { `image${ index } ` }
254
+ key = { `imageDiv${ index } ` }
255
+ >
246
256
< ImageContainer
247
257
tools = { tools }
248
258
>
@@ -263,7 +273,7 @@ export default class Gallery extends Component {
263
273
</ ImageContainer >
264
274
</ div > ,
265
275
dragMode
266
- ? dropzone ( `dropzone${ index } ` )
276
+ ? dropzone ( `dropzone${ index } ` , dropzoneId === index + 1 )
267
277
: null ,
268
278
] ;
269
279
}
0 commit comments