@@ -64,8 +64,9 @@ export interface ImageGalleryProps
64
64
showToolbar ?: boolean ;
65
65
/** 是否内嵌 */
66
66
embed ?: boolean ;
67
- items : Array < ImageGalleryItem > ;
67
+ items ? : Array < ImageGalleryItem > ;
68
68
position ?: ImageGalleryPosition ;
69
+ enlargeWithGallary ?: boolean ;
69
70
}
70
71
71
72
export interface ImageGalleryState {
@@ -74,6 +75,8 @@ export interface ImageGalleryState {
74
75
items : Array < ImageGalleryItem > ;
75
76
/** 图片缩放比例尺 */
76
77
scale : number ;
78
+ /** 默认图片缩放比例尺 */
79
+ defaultScale : number ;
77
80
/** 图片旋转角度 */
78
81
rotate : number ;
79
82
/**
@@ -95,7 +98,7 @@ export interface ImageGalleryState {
95
98
/** 图片是否加载中 */
96
99
imageLoading : boolean ;
97
100
/** 图片信息 */
98
- imageLoadInfo ? : {
101
+ imageLoadInfo : {
99
102
naturalWidth : number ;
100
103
naturalHeight : number ;
101
104
url : string ;
@@ -149,33 +152,37 @@ export class ImageGallery extends React.Component<
149
152
tx : 0 ,
150
153
ty : 0 ,
151
154
scale : 1 ,
155
+ defaultScale : 1 ,
152
156
rotate : 0 ,
153
157
showToolbar : false ,
154
158
imageGallaryClassName : '' ,
155
159
actions : ImageGallery . defaultProps . actions ,
156
- imageLoading : false ,
157
- isNaturalSize : false
160
+ imageLoading : true ,
161
+ isNaturalSize : false ,
162
+ imageLoadInfo : {
163
+ naturalWidth : 0 ,
164
+ naturalHeight : 0 ,
165
+ url : ''
166
+ }
158
167
} ;
159
168
160
169
galleryMain ?: HTMLDivElement ;
161
170
162
171
constructor ( props : ImageGalleryProps ) {
163
172
super ( props ) ;
164
173
165
- const hasItems = ! ! props ?. items ;
166
-
167
174
this . state = {
168
175
...this . state ,
169
- items : hasItems ? props . items : [ ] ,
170
- index : hasItems ? 0 : - 1 ,
176
+ items : props . items ?? [ ] ,
177
+ index : ! ! props ?. items ? 0 : - 1 ,
171
178
showToolbar : ! ! props ?. showToolbar
172
179
} ;
173
180
}
174
181
175
182
componentDidUpdate ( prevProps : Readonly < ImageGalleryProps > ) : void {
176
183
if ( this . props . items !== prevProps . items ) {
177
184
this . setState ( {
178
- items : this . props . items
185
+ items : this . props . items ?? [ ]
179
186
} ) ;
180
187
}
181
188
if ( this . props ?. showToolbar !== prevProps ?. showToolbar ) {
@@ -189,6 +196,12 @@ export class ImageGallery extends React.Component<
189
196
position : this . props . position
190
197
} ) ;
191
198
}
199
+
200
+ if ( this . props . enlargeWithGallary !== prevProps . enlargeWithGallary ) {
201
+ this . setState ( {
202
+ enlargeWithGallary : this . props . enlargeWithGallary
203
+ } ) ;
204
+ }
192
205
}
193
206
194
207
@autobind
@@ -291,6 +304,7 @@ export class ImageGallery extends React.Component<
291
304
enlargeWithGallary : info . enlargeWithGallary ,
292
305
imageGallaryClassName : info . imageGallaryClassName ,
293
306
position : info ?. position ,
307
+ imageLoading : true ,
294
308
/** 外部传入合法key值的actions才会生效 */
295
309
actions : Array . isArray ( info . toolbarActions )
296
310
? info . toolbarActions . filter ( action =>
@@ -301,8 +315,9 @@ export class ImageGallery extends React.Component<
301
315
}
302
316
303
317
resetImageAction ( ) {
318
+ const { defaultScale} = this . state ;
304
319
this . setState ( {
305
- scale : 1 ,
320
+ scale : defaultScale ,
306
321
rotate : 0 ,
307
322
isNaturalSize : false
308
323
} ) ;
@@ -319,15 +334,13 @@ export class ImageGallery extends React.Component<
319
334
@autobind
320
335
prev ( ) {
321
336
const index = this . state . index ;
322
- this . setState ( { index : index - 1 , imageLoading : true } ) ;
323
- this . resetImageAction ( ) ;
337
+ this . setIndex ( index - 1 ) ;
324
338
}
325
339
326
340
@autobind
327
341
next ( ) {
328
342
const index = this . state . index ;
329
- this . setState ( { index : index + 1 , imageLoading : true } ) ;
330
- this . resetImageAction ( ) ;
343
+ this . setIndex ( index + 1 ) ;
331
344
}
332
345
333
346
@autobind
@@ -340,8 +353,16 @@ export class ImageGallery extends React.Component<
340
353
* 设置当前选中
341
354
*/
342
355
@autobind
343
- setIndex ( index : number ) {
344
- this . setState ( { index, imageLoading : true } ) ;
356
+ setIndex ( cIndex : number ) {
357
+ const { items, index} = this . state ;
358
+ const bool = items [ index ] . originalSrc === items [ cIndex ] . originalSrc ;
359
+ console . log ( bool ) ;
360
+ this . setState ( {
361
+ index : cIndex ,
362
+ imageLoading : ! bool ,
363
+ tx : 0 ,
364
+ ty : 0
365
+ } ) ;
345
366
this . resetImageAction ( ) ;
346
367
}
347
368
@@ -379,10 +400,11 @@ export class ImageGallery extends React.Component<
379
400
return ;
380
401
}
381
402
403
+ const { defaultScale} = this . state ;
382
404
switch ( action . key ) {
383
405
case ImageActionKey . DEFAULT_VIEW :
384
406
this . setState ( {
385
- scale : 1 ,
407
+ scale : defaultScale ,
386
408
isNaturalSize : false ,
387
409
tx : 0 ,
388
410
ty : 0
@@ -421,13 +443,24 @@ export class ImageGallery extends React.Component<
421
443
@autobind
422
444
handleImageLoad ( e : React . SyntheticEvent < HTMLImageElement > ) {
423
445
const { index, items} = this . state ;
446
+ const { clientWidth, clientHeight} = this . galleryMain ! ;
447
+ // @ts -ignore
448
+ const naturalHeight = e . target ?. naturalHeight ;
449
+ // @ts -ignore
450
+ const naturalWidth = e . target ?. naturalWidth ;
451
+ let scale = 1 ;
452
+ if ( naturalHeight >= clientHeight || naturalWidth >= clientWidth ) {
453
+ const p1 = clientHeight / naturalHeight ;
454
+ const p2 = clientWidth / naturalWidth ;
455
+ scale = p1 > p2 ? p2 : p1 ;
456
+ }
424
457
this . setState ( {
458
+ scale,
459
+ defaultScale : scale ,
425
460
imageLoadInfo : {
426
461
url : items [ index ] ?. src ,
427
- // @ts -ignore
428
- naturalHeight : e . target ?. naturalHeight ,
429
- // @ts -ignore
430
- naturalWidth : e . target ?. naturalWidth
462
+ naturalHeight,
463
+ naturalWidth
431
464
} ,
432
465
imageLoading : false
433
466
} ) ;
@@ -465,9 +498,10 @@ export class ImageGallery extends React.Component<
465
498
if ( action . key === ImageActionKey . DRAG ) {
466
499
return (
467
500
< DragProgress
468
- value = { 1 }
501
+ value = { scale }
469
502
onChange = { this . handleDragProgress }
470
503
skin = { embed ? 'light' : 'dark' }
504
+ max = { 2 }
471
505
/>
472
506
) ;
473
507
}
@@ -559,7 +593,7 @@ export class ImageGallery extends React.Component<
559
593
return [
560
594
~ index && items [ index ] ? (
561
595
< >
562
- < div className = { cx ( 'ImageGallery-main' ) } ref = { this . galleryMainRef } >
596
+ < div className = { cx ( 'ImageGallery-main' ) } >
563
597
< div
564
598
className = { cx (
565
599
'ImageGallery-preview' ,
@@ -576,7 +610,10 @@ export class ImageGallery extends React.Component<
576
610
: ''
577
611
) }
578
612
>
579
- < div className = { cx ( 'ImageGallery-image-wrap' ) } >
613
+ < div
614
+ className = { cx ( 'ImageGallery-image-wrap' ) }
615
+ ref = { this . galleryMainRef }
616
+ >
580
617
< img
581
618
draggable = { false }
582
619
src = { items [ index ] . originalSrc }
0 commit comments