1
- import React , { useCallback , useState } from 'react' ;
1
+ import React , { useCallback , useState , useEffect , useRef } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import { PlusOutlined } from '@ant-design/icons' ;
4
4
5
5
import ImagesZoom from './ImagesZoom' ;
6
6
7
+ function isImageValid ( src ) {
8
+ return new Promise ( ( resolve ) => {
9
+ const img = document . createElement ( 'img' ) ;
10
+ img . onerror = ( ) => resolve ( false ) ;
11
+ img . onload = ( ) => resolve ( true ) ;
12
+ img . src = src ;
13
+ } ) ;
14
+ }
7
15
const PostImages = ( { images } ) => {
8
16
const [ showImagesZoom , setShowImagesZoom ] = useState ( false ) ;
17
+ const imgEl = useRef ( ) ;
18
+
19
+ useEffect (
20
+ ( ) => {
21
+ isImageValid ( images [ 0 ] . src ) . then ( ( isValid ) => {
22
+ if ( ! isValid ) {
23
+ imgEl . current . remove ( ) ;
24
+ }
25
+ } ) ;
26
+ } ,
27
+ [ images [ 0 ] . src ] ,
28
+ ) ;
9
29
10
30
const onZoom = useCallback ( ( ) => {
11
31
setShowImagesZoom ( true ) ;
12
32
} , [ ] ) ;
13
33
const onClose = useCallback ( ( ) => {
14
34
setShowImagesZoom ( false ) ;
15
35
} , [ ] ) ;
36
+
16
37
const onError = useCallback ( ( e ) => {
17
- e . target . src = e . target . src . replace ( '/thumb/' , '/original/' ) ;
38
+ console . dir ( e . target ) ;
39
+ e . target . previousSibling . remove ( ) ;
40
+ // e.target.src = e.target.currentSrc.replace('/thumb/', '/original/');
18
41
} , [ ] ) ;
19
42
20
43
if ( images . length === 1 ) {
21
44
return (
22
45
< >
23
- < img role = "presentation" src = { `${ images [ 0 ] . src } ` } alt = { images [ 0 ] . src } onClick = { onZoom } onError = { onError } />
46
+ < picture >
47
+ < source ref = { imgEl } srcSet = { `${ images [ 0 ] . src } ` } />
48
+ < img role = "presentation" onClick = { onZoom } src = { `${ images [ 0 ] . src . replace ( '/thumb/' , '/original/' ) } ` } alt = { images [ 0 ] . src } style = { { maxWidth : '100%' , display : 'inline-block' } } />
49
+ </ picture >
24
50
{ showImagesZoom && < ImagesZoom images = { images } onClose = { onClose } /> }
25
51
</ >
26
52
) ;
27
53
}
28
54
if ( images . length === 2 ) {
29
55
return (
30
56
< >
31
- < img role = "presentation" style = { { width : '50%' , display : 'inline-block' } } src = { `${ images [ 0 ] . src } ` } alt = { images [ 0 ] . src } onClick = { onZoom } onError = { onError } />
32
- < img role = "presentation" style = { { width : '50%' , display : 'inline-block' } } src = { `${ images [ 1 ] . src } ` } alt = { images [ 1 ] . src } onClick = { onZoom } onError = { onError } />
57
+ < picture style = { { width : '50%' , display : 'inline-block' } } >
58
+ < source ref = { imgEl } srcSet = { `${ images [ 0 ] . src } ` } />
59
+ < img role = "presentation" onClick = { onZoom } src = { `${ images [ 0 ] . src . replace ( '/thumb/' , '/original/' ) } ` } alt = { images [ 0 ] . src } style = { { maxWidth : '100%' , display : 'inline-block' } } />
60
+ </ picture >
61
+ < picture style = { { width : '50%' , display : 'inline-block' } } >
62
+ < source ref = { imgEl } srcSet = { `${ images [ 1 ] . src } ` } />
63
+ < img role = "presentation" onClick = { onZoom } src = { `${ images [ 1 ] . src . replace ( '/thumb/' , '/original/' ) } ` } alt = { images [ 0 ] . src } style = { { maxWidth : '100%' , display : 'inline-block' } } />
64
+ </ picture >
33
65
{ showImagesZoom && < ImagesZoom images = { images } onClose = { onClose } /> }
34
66
</ >
35
67
) ;
36
68
}
37
69
return (
38
70
< >
39
71
< div >
40
- < img role = "presentation" style = { { width : '50%' } } src = { `${ images [ 0 ] . src } ` } alt = { images [ 0 ] . src } onClick = { onZoom } onError = { onError } />
72
+ < picture style = { { width : '50%' } } onError = { onError } >
73
+ < source ref = { imgEl } srcSet = { `${ images [ 0 ] . src } ` } />
74
+ < img role = "presentation" onClick = { onZoom } src = { `${ images [ 0 ] . src . replace ( '/thumb/' , '/original/' ) } ` } alt = { images [ 0 ] . src } style = { { maxWidth : '50%' } } />
75
+ </ picture >
41
76
< div
42
77
role = "presentation"
43
78
style = { { display : 'inline-block' , width : '50%' , textAlign : 'center' , verticalAlign : 'middle' } }
@@ -55,7 +90,7 @@ const PostImages = ({ images }) => {
55
90
} ;
56
91
57
92
PostImages . propTypes = {
58
- images : PropTypes . arrayOf ( PropTypes . object ) ,
93
+ images : PropTypes . arrayOf ( PropTypes . object ) . isRequired ,
59
94
} ;
60
95
61
96
export default PostImages ;
0 commit comments