-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
index.tsx
277 lines (234 loc) · 6.74 KB
/
index.tsx
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import React, { forwardRef, memo } from 'react'
import {
View,
Image,
NativeModules,
requireNativeComponent,
StyleSheet,
FlexStyle,
LayoutChangeEvent,
ShadowStyleIOS,
StyleProp,
TransformsStyle,
ImageRequireSource,
Platform,
AccessibilityProps,
ViewProps,
ColorValue,
} from 'react-native'
export type ResizeMode = 'contain' | 'cover' | 'stretch' | 'center'
const resizeMode = {
contain: 'contain',
cover: 'cover',
stretch: 'stretch',
center: 'center',
} as const
export type Priority = 'low' | 'normal' | 'high'
const priority = {
low: 'low',
normal: 'normal',
high: 'high',
} as const
type Cache = 'immutable' | 'web' | 'cacheOnly'
const cacheControl = {
// Ignore headers, use uri as cache key, fetch only if not in cache.
immutable: 'immutable',
// Respect http headers, no aggressive caching.
web: 'web',
// Only load from cache.
cacheOnly: 'cacheOnly',
} as const
export type Source = {
uri?: string
headers?: { [key: string]: string }
priority?: Priority
cache?: Cache
}
export interface OnLoadEvent {
nativeEvent: {
width: number
height: number
}
}
export interface OnProgressEvent {
nativeEvent: {
loaded: number
total: number
}
}
export interface ImageStyle extends FlexStyle, TransformsStyle, ShadowStyleIOS {
backfaceVisibility?: 'visible' | 'hidden'
borderBottomLeftRadius?: number
borderBottomRightRadius?: number
backgroundColor?: string
borderColor?: string
borderWidth?: number
borderRadius?: number
borderTopLeftRadius?: number
borderTopRightRadius?: number
overlayColor?: string
opacity?: number
}
export interface FastImageProps extends AccessibilityProps, ViewProps {
source?: Source | ImageRequireSource
defaultSource?: ImageRequireSource
resizeMode?: ResizeMode
fallback?: boolean
onLoadStart?(): void
onProgress?(event: OnProgressEvent): void
onLoad?(event: OnLoadEvent): void
onError?(): void
onLoadEnd?(): void
/**
* onLayout function
*
* Invoked on mount and layout changes with
*
* {nativeEvent: { layout: {x, y, width, height}}}.
*/
onLayout?: (event: LayoutChangeEvent) => void
/**
*
* Style
*/
style?: StyleProp<ImageStyle>
/**
* TintColor
*
* If supplied, changes the color of all the non-transparent pixels to the given color.
*/
tintColor?: ColorValue
/**
* A unique identifier for this element to be used in UI Automation testing scripts.
*/
testID?: string
/**
* Render children within the image.
*/
children?: React.ReactNode
}
const resolveDefaultSource = (
defaultSource?: ImageRequireSource,
): string | number | null => {
if (!defaultSource) {
return null
}
if (Platform.OS === 'android') {
// Android receives a URI string, and resolves into a Drawable using RN's methods.
const resolved = Image.resolveAssetSource(
defaultSource as ImageRequireSource,
)
if (resolved) {
return resolved.uri
}
return null
}
// iOS or other number mapped assets
// In iOS the number is passed, and bridged automatically into a UIImage
return defaultSource
}
function FastImageBase({
source,
defaultSource,
tintColor,
onLoadStart,
onProgress,
onLoad,
onError,
onLoadEnd,
style,
fallback,
children,
// eslint-disable-next-line no-shadow
resizeMode = 'cover',
forwardedRef,
...props
}: FastImageProps & { forwardedRef: React.Ref<any> }) {
if (fallback) {
const cleanedSource = { ...(source as any) }
delete cleanedSource.cache
const resolvedSource = Image.resolveAssetSource(cleanedSource)
return (
<View style={[styles.imageContainer, style]} ref={forwardedRef}>
<Image
{...props}
style={[StyleSheet.absoluteFill, { tintColor }]}
source={resolvedSource}
defaultSource={defaultSource}
onLoadStart={onLoadStart}
onProgress={onProgress}
onLoad={onLoad as any}
onError={onError}
onLoadEnd={onLoadEnd}
resizeMode={resizeMode}
/>
{children}
</View>
)
}
const resolvedSource = Image.resolveAssetSource(source as any)
const resolvedDefaultSource = resolveDefaultSource(defaultSource)
return (
<View style={[styles.imageContainer, style]} ref={forwardedRef}>
<FastImageView
{...props}
tintColor={tintColor}
style={StyleSheet.absoluteFill}
source={resolvedSource}
defaultSource={resolvedDefaultSource}
onFastImageLoadStart={onLoadStart}
onFastImageProgress={onProgress}
onFastImageLoad={onLoad}
onFastImageError={onError}
onFastImageLoadEnd={onLoadEnd}
resizeMode={resizeMode}
/>
{children}
</View>
)
}
const FastImageMemo = memo(FastImageBase)
const FastImageComponent: React.ComponentType<FastImageProps> = forwardRef(
(props: FastImageProps, ref: React.Ref<any>) => (
<FastImageMemo forwardedRef={ref} {...props} />
),
)
FastImageComponent.displayName = 'FastImage'
export interface FastImageStaticProperties {
resizeMode: typeof resizeMode
priority: typeof priority
cacheControl: typeof cacheControl
preload: (sources: Source[]) => void
clearMemoryCache: () => Promise<void>
clearDiskCache: () => Promise<void>
}
const FastImage: React.ComponentType<FastImageProps> &
FastImageStaticProperties = FastImageComponent as any
FastImage.resizeMode = resizeMode
FastImage.cacheControl = cacheControl
FastImage.priority = priority
FastImage.preload = (sources: Source[]) =>
NativeModules.FastImageView.preload(sources)
FastImage.clearMemoryCache = () =>
NativeModules.FastImageView.clearMemoryCache()
FastImage.clearDiskCache = () => NativeModules.FastImageView.clearDiskCache()
const styles = StyleSheet.create({
imageContainer: {
overflow: 'hidden',
},
})
// Types of requireNativeComponent are not correct.
const FastImageView = (requireNativeComponent as any)(
'FastImageView',
FastImage,
{
nativeOnly: {
onFastImageLoadStart: true,
onFastImageProgress: true,
onFastImageLoad: true,
onFastImageError: true,
onFastImageLoadEnd: true,
},
},
)
export default FastImage