-
Notifications
You must be signed in to change notification settings - Fork 2
/
HTMLImage.js
165 lines (150 loc) · 3.86 KB
/
HTMLImage.js
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
import React, { PureComponent } from 'react'
import { Image, View } from 'react-native'
import PropTypes from 'prop-types'
import { CustomImage } from './widget'
export default class HTMLImage extends PureComponent {
static propTypes = {
source: PropTypes.object.isRequired,
alt: PropTypes.string,
style: Image.propTypes.style,
errorImgSource: PropTypes.object,
imagesMaxWidth: PropTypes.number,
imagesInitialDimensions: PropTypes.shape({
width: PropTypes.number,
height: PropTypes.number
})
};
static defaultProps = {
imagesInitialDimensions: {
width: 100,
height: 100
}
};
constructor (props) {
super(props)
this.state = {
width: props.imagesInitialDimensions.width,
height: props.imagesInitialDimensions.height,
indeterminate: !props.style || !props.style.width || !props.style.height
}
}
componentDidMount () {
this.getImageSize()
}
UNSAFE_componentWillReceiveProps (nextProps) {
this.getImageSize(nextProps)
}
get errorImage () {
const { imagesMaxWidth, errorImgSource } = this.props
return (
<Image
style={{
width: imagesMaxWidth,
height: imagesMaxWidth * 9 / 16
}}
source={errorImgSource}
/>
)
}
get placeholderImage () {
return (
<View
style={{
width: this.props.imagesInitialDimensions.width,
height: this.props.imagesInitialDimensions.height
}}
/>
)
}
getDimensionsFromStyle = (style, height, width) => {
let styleWidth
let styleHeight
if (height) {
styleHeight = height
}
if (width) {
styleWidth = width
}
if (Array.isArray(style)) {
style.forEach(styles => {
if (!width && styles['width']) {
styleWidth = styles['width']
}
if (!height && styles['height']) {
styleHeight = styles['height']
}
})
} else {
if (!width && style['width']) {
styleWidth = style['width']
}
if (!height && style['height']) {
styleHeight = style['height']
}
}
return { styleWidth, styleHeight }
};
getImageSize = (props = this.props) => {
const { source, imagesMaxWidth } = props
// Fetch image dimensions only if they aren't supplied or if with or height is missing
Image.getSize(
source.uri,
(originalWidth, originalHeight) => {
if (originalWidth === 0 || originalHeight === 0) {
return this.setState({ error: true })
}
if (!imagesMaxWidth) {
return this.setState({
width: originalWidth,
height: originalHeight
})
}
const optimalWidth = originalWidth === 0 ? 0 : imagesMaxWidth <= originalWidth ? imagesMaxWidth : originalWidth
const optimalHeight = originalHeight === 0 ? 0 : (optimalWidth * originalHeight) / originalWidth
this.setState({
width: optimalWidth,
height: optimalHeight,
indeterminate: false,
error: false
})
},
() => {
this.setState({ error: true })
}
)
};
validImage = (
source,
style,
onLoad,
onLoadEnd,
errorImgSource
) => {
return (
<CustomImage
source={source}
resizeMode='cover'
style={[
style,
{
width: this.state.width,
height: this.state.height
}
]}
onLoad={onLoad}
onLoadEnd={onLoadEnd}
errorImgSource={errorImgSource}
/>
)
};
render () {
const { source, style, onLoad, onLoadEnd, errorImgSource } = this.props
if (this.state.error) {
return this.errorImage
}
if (this.state.indeterminate) {
return this.placeholderImage
}
return this.validImage(source, style, onLoad, onLoadEnd, errorImgSource)
}
}