|
@@ -1,4 +1,4 @@
|
1
|
|
-import React from "react";
|
|
1
|
+import React from 'react';
|
2
|
2
|
import {
|
3
|
3
|
ActivityIndicator,
|
4
|
4
|
Linking,
|
|
@@ -10,12 +10,12 @@ import {
|
10
|
10
|
NativeModules,
|
11
|
11
|
Image,
|
12
|
12
|
findNodeHandle,
|
13
|
|
- NativeSyntheticEvent
|
14
|
|
-} from "react-native";
|
|
13
|
+ NativeSyntheticEvent,
|
|
14
|
+} from 'react-native';
|
15
|
15
|
|
16
|
|
-import invariant from "invariant";
|
|
16
|
+import invariant from 'invariant';
|
17
|
17
|
|
18
|
|
-import WebViewShared from "./WebViewShared";
|
|
18
|
+import WebViewShared from './WebViewShared';
|
19
|
19
|
import {
|
20
|
20
|
WebViewSourceUri,
|
21
|
21
|
WebViewError,
|
|
@@ -25,16 +25,16 @@ import {
|
25
|
25
|
WebViewNavigationEvent,
|
26
|
26
|
WebViewSharedProps,
|
27
|
27
|
WebViewSource,
|
28
|
|
- WebViewProgressEvent
|
29
|
|
-} from "./types/WebViewTypes";
|
|
28
|
+ WebViewProgressEvent,
|
|
29
|
+} from './types/WebViewTypes';
|
30
|
30
|
|
31
|
|
-type DecelerationRate = number | "normal" | "fast";
|
|
31
|
+type DecelerationRate = number | 'normal' | 'fast';
|
32
|
32
|
|
33
|
33
|
// Imported from https://github.com/facebook/react-native/blob/master/Libraries/Components/ScrollView/processDecelerationRate.js
|
34
|
|
-function processDecelerationRate(decelerationRate: DecelerationRate) {
|
35
|
|
- if (decelerationRate === "normal") {
|
|
34
|
+function processDecelerationRate(decelerationRate?: DecelerationRate) {
|
|
35
|
+ if (decelerationRate === 'normal') {
|
36
|
36
|
decelerationRate = 0.998;
|
37
|
|
- } else if (decelerationRate === "fast") {
|
|
37
|
+ } else if (decelerationRate === 'fast') {
|
38
|
38
|
decelerationRate = 0.99;
|
39
|
39
|
}
|
40
|
40
|
return decelerationRate;
|
|
@@ -43,28 +43,28 @@ function processDecelerationRate(decelerationRate: DecelerationRate) {
|
43
|
43
|
const RNCUIWebViewManager = NativeModules.RNCUIWebViewManager;
|
44
|
44
|
const RNCWKWebViewManager = NativeModules.RNCWKWebViewManager;
|
45
|
45
|
|
46
|
|
-const BGWASH = "rgba(255,255,255,0.8)";
|
|
46
|
+const BGWASH = 'rgba(255,255,255,0.8)';
|
47
|
47
|
|
48
|
48
|
enum WebViewState {
|
49
|
|
- IDLE = "IDLE",
|
50
|
|
- LOADING = "LOADING",
|
51
|
|
- ERROR = "ERROR"
|
|
49
|
+ IDLE = 'IDLE',
|
|
50
|
+ LOADING = 'LOADING',
|
|
51
|
+ ERROR = 'ERROR',
|
52
|
52
|
}
|
53
|
53
|
|
54
|
54
|
enum NavigationType {
|
55
|
|
- click = "click",
|
56
|
|
- formsubmit = "formsubmit",
|
57
|
|
- backforward = "backforward",
|
58
|
|
- reload = "reload",
|
59
|
|
- formresubmit = "formresubmit",
|
60
|
|
- other = "other"
|
|
55
|
+ click = 'click',
|
|
56
|
+ formsubmit = 'formsubmit',
|
|
57
|
+ backforward = 'backforward',
|
|
58
|
+ reload = 'reload',
|
|
59
|
+ formresubmit = 'formresubmit',
|
|
60
|
+ other = 'other',
|
61
|
61
|
}
|
62
|
62
|
|
63
|
|
-const JSNavigationScheme = "react-js-navigation";
|
|
63
|
+const JSNavigationScheme = 'react-js-navigation';
|
64
|
64
|
|
65
|
65
|
type State = {
|
66
|
66
|
viewState: WebViewState;
|
67
|
|
- lastErrorEvent: WebViewError;
|
|
67
|
+ lastErrorEvent: WebViewError | null;
|
68
|
68
|
};
|
69
|
69
|
|
70
|
70
|
const defaultRenderLoading = () => (
|
|
@@ -73,15 +73,15 @@ const defaultRenderLoading = () => (
|
73
|
73
|
</View>
|
74
|
74
|
);
|
75
|
75
|
const defaultRenderError = (
|
76
|
|
- errorDomain: string,
|
|
76
|
+ errorDomain: string | undefined,
|
77
|
77
|
errorCode: number,
|
78
|
|
- errorDesc: string
|
|
78
|
+ errorDesc: string,
|
79
|
79
|
) => (
|
80
|
80
|
<View style={styles.errorContainer}>
|
81
|
81
|
<Text style={styles.errorTextTitle}>Error loading page</Text>
|
82
|
|
- <Text style={styles.errorText}>{"Domain: " + errorDomain}</Text>
|
83
|
|
- <Text style={styles.errorText}>{"Error Code: " + errorCode}</Text>
|
84
|
|
- <Text style={styles.errorText}>{"Description: " + errorDesc}</Text>
|
|
82
|
+ <Text style={styles.errorText}>{'Domain: ' + errorDomain}</Text>
|
|
83
|
+ <Text style={styles.errorText}>{'Error Code: ' + errorCode}</Text>
|
|
84
|
+ <Text style={styles.errorText}>{'Description: ' + errorDesc}</Text>
|
85
|
85
|
</View>
|
86
|
86
|
);
|
87
|
87
|
|
|
@@ -116,7 +116,7 @@ export default class WebView extends React.Component<
|
116
|
116
|
|
117
|
117
|
static defaultProps = {
|
118
|
118
|
useWebKit: true,
|
119
|
|
- originWhitelist: WebViewShared.defaultOriginWhitelist
|
|
119
|
+ originWhitelist: WebViewShared.defaultOriginWhitelist,
|
120
|
120
|
};
|
121
|
121
|
|
122
|
122
|
static isFileUploadSupported = async () => {
|
|
@@ -128,7 +128,7 @@ export default class WebView extends React.Component<
|
128
|
128
|
viewState: this.props.startInLoadingState
|
129
|
129
|
? WebViewState.LOADING
|
130
|
130
|
: WebViewState.IDLE,
|
131
|
|
- lastErrorEvent: null
|
|
131
|
+ lastErrorEvent: null,
|
132
|
132
|
};
|
133
|
133
|
|
134
|
134
|
webViewRef = React.createRef<React.ComponentClass>();
|
|
@@ -139,7 +139,7 @@ export default class WebView extends React.Component<
|
139
|
139
|
this.props.scalesPageToFit !== undefined
|
140
|
140
|
) {
|
141
|
141
|
console.warn(
|
142
|
|
- "The scalesPageToFit property is not supported when useWebKit = true"
|
|
142
|
+ 'The scalesPageToFit property is not supported when useWebKit = true',
|
143
|
143
|
);
|
144
|
144
|
}
|
145
|
145
|
if (
|
|
@@ -147,7 +147,7 @@ export default class WebView extends React.Component<
|
147
|
147
|
this.props.allowsBackForwardNavigationGestures
|
148
|
148
|
) {
|
149
|
149
|
console.warn(
|
150
|
|
- "The allowsBackForwardNavigationGestures property is not supported when useWebKit = false"
|
|
150
|
+ 'The allowsBackForwardNavigationGestures property is not supported when useWebKit = false',
|
151
|
151
|
);
|
152
|
152
|
}
|
153
|
153
|
}
|
|
@@ -167,15 +167,15 @@ export default class WebView extends React.Component<
|
167
|
167
|
otherView = (this.props.renderLoading || defaultRenderLoading)();
|
168
|
168
|
} else if (this.state.viewState === WebViewState.ERROR) {
|
169
|
169
|
const errorEvent = this.state.lastErrorEvent;
|
170
|
|
- invariant(errorEvent != null, "lastErrorEvent expected to be non-null");
|
|
170
|
+ invariant(errorEvent != null, 'lastErrorEvent expected to be non-null');
|
171
|
171
|
otherView = (this.props.renderError || defaultRenderError)(
|
172
|
|
- errorEvent.domain,
|
173
|
|
- errorEvent.code,
|
174
|
|
- errorEvent.description
|
|
172
|
+ errorEvent!.domain,
|
|
173
|
+ errorEvent!.code,
|
|
174
|
+ errorEvent!.description,
|
175
|
175
|
);
|
176
|
176
|
} else if (this.state.viewState !== WebViewState.IDLE) {
|
177
|
177
|
console.error(
|
178
|
|
- "RNCWebView invalid state encountered: " + this.state.viewState
|
|
178
|
+ 'RNCWebView invalid state encountered: ' + this.state.viewState,
|
179
|
179
|
);
|
180
|
180
|
}
|
181
|
181
|
|
|
@@ -199,17 +199,17 @@ export default class WebView extends React.Component<
|
199
|
199
|
}
|
200
|
200
|
|
201
|
201
|
const compiledWhitelist = [
|
202
|
|
- "about:blank",
|
203
|
|
- ...(this.props.originWhitelist || [])
|
|
202
|
+ 'about:blank',
|
|
203
|
+ ...(this.props.originWhitelist || []),
|
204
|
204
|
].map(WebViewShared.originWhitelistToRegex);
|
205
|
205
|
const onShouldStartLoadWithRequest = (
|
206
|
|
- event: NativeSyntheticEvent<WebViewIOSLoadRequestEvent>
|
|
206
|
+ event: NativeSyntheticEvent<WebViewIOSLoadRequestEvent>,
|
207
|
207
|
) => {
|
208
|
208
|
let shouldStart = true;
|
209
|
209
|
const { url } = event.nativeEvent;
|
210
|
210
|
const origin = WebViewShared.extractOrigin(url);
|
211
|
211
|
const passesWhitelist = compiledWhitelist.some(x =>
|
212
|
|
- new RegExp(x).test(origin)
|
|
212
|
+ new RegExp(x).test(origin),
|
213
|
213
|
);
|
214
|
214
|
shouldStart = shouldStart && passesWhitelist;
|
215
|
215
|
if (!passesWhitelist) {
|
|
@@ -220,15 +220,15 @@ export default class WebView extends React.Component<
|
220
|
220
|
shouldStart &&
|
221
|
221
|
this.props.onShouldStartLoadWithRequest(event.nativeEvent);
|
222
|
222
|
}
|
223
|
|
- invariant(viewManager != null, "viewManager expected to be non-null");
|
|
223
|
+ invariant(viewManager != null, 'viewManager expected to be non-null');
|
224
|
224
|
viewManager.startLoadWithResult(
|
225
|
225
|
!!shouldStart,
|
226
|
|
- event.nativeEvent.lockIdentifier
|
|
226
|
+ event.nativeEvent.lockIdentifier,
|
227
|
227
|
);
|
228
|
228
|
};
|
229
|
229
|
|
230
|
230
|
const decelerationRate = processDecelerationRate(
|
231
|
|
- this.props.decelerationRate
|
|
231
|
+ this.props.decelerationRate,
|
232
|
232
|
);
|
233
|
233
|
|
234
|
234
|
let source: WebViewSource = this.props.source || {};
|
|
@@ -238,7 +238,7 @@ export default class WebView extends React.Component<
|
238
|
238
|
source = { uri: this.props.url };
|
239
|
239
|
}
|
240
|
240
|
|
241
|
|
- const messagingEnabled = typeof this.props.onMessage === "function";
|
|
241
|
+ const messagingEnabled = typeof this.props.onMessage === 'function';
|
242
|
242
|
|
243
|
243
|
let NativeWebView = nativeConfig.component;
|
244
|
244
|
|
|
@@ -309,7 +309,7 @@ export default class WebView extends React.Component<
|
309
|
309
|
UIManager.dispatchViewManagerCommand(
|
310
|
310
|
this.getWebViewHandle(),
|
311
|
311
|
this._getCommands().goForward,
|
312
|
|
- null
|
|
312
|
+ null,
|
313
|
313
|
);
|
314
|
314
|
};
|
315
|
315
|
|
|
@@ -320,7 +320,7 @@ export default class WebView extends React.Component<
|
320
|
320
|
UIManager.dispatchViewManagerCommand(
|
321
|
321
|
this.getWebViewHandle(),
|
322
|
322
|
this._getCommands().goBack,
|
323
|
|
- null
|
|
323
|
+ null,
|
324
|
324
|
);
|
325
|
325
|
};
|
326
|
326
|
|
|
@@ -332,7 +332,7 @@ export default class WebView extends React.Component<
|
332
|
332
|
UIManager.dispatchViewManagerCommand(
|
333
|
333
|
this.getWebViewHandle(),
|
334
|
334
|
this._getCommands().reload,
|
335
|
|
- null
|
|
335
|
+ null,
|
336
|
336
|
);
|
337
|
337
|
};
|
338
|
338
|
|
|
@@ -343,7 +343,7 @@ export default class WebView extends React.Component<
|
343
|
343
|
UIManager.dispatchViewManagerCommand(
|
344
|
344
|
this.getWebViewHandle(),
|
345
|
345
|
this._getCommands().stopLoading,
|
346
|
|
- null
|
|
346
|
+ null,
|
347
|
347
|
);
|
348
|
348
|
};
|
349
|
349
|
|
|
@@ -361,7 +361,7 @@ export default class WebView extends React.Component<
|
361
|
361
|
UIManager.dispatchViewManagerCommand(
|
362
|
362
|
this.getWebViewHandle(),
|
363
|
363
|
this._getCommands().postMessage,
|
364
|
|
- [String(data)]
|
|
364
|
+ [String(data)],
|
365
|
365
|
);
|
366
|
366
|
};
|
367
|
367
|
|
|
@@ -375,7 +375,7 @@ export default class WebView extends React.Component<
|
375
|
375
|
UIManager.dispatchViewManagerCommand(
|
376
|
376
|
this.getWebViewHandle(),
|
377
|
377
|
this._getCommands().injectJavaScript,
|
378
|
|
- [data]
|
|
378
|
+ [data],
|
379
|
379
|
);
|
380
|
380
|
};
|
381
|
381
|
|
|
@@ -407,11 +407,11 @@ export default class WebView extends React.Component<
|
407
|
407
|
const { onError, onLoadEnd } = this.props;
|
408
|
408
|
onError && onError(event);
|
409
|
409
|
onLoadEnd && onLoadEnd(event);
|
410
|
|
- console.warn("Encountered an error loading page", event.nativeEvent);
|
|
410
|
+ console.warn('Encountered an error loading page', event.nativeEvent);
|
411
|
411
|
|
412
|
412
|
this.setState({
|
413
|
413
|
lastErrorEvent: event.nativeEvent,
|
414
|
|
- viewState: WebViewState.ERROR
|
|
414
|
+ viewState: WebViewState.ERROR,
|
415
|
415
|
});
|
416
|
416
|
};
|
417
|
417
|
|
|
@@ -420,7 +420,7 @@ export default class WebView extends React.Component<
|
420
|
420
|
onLoad && onLoad(event);
|
421
|
421
|
onLoadEnd && onLoadEnd(event);
|
422
|
422
|
this.setState({
|
423
|
|
- viewState: WebViewState.IDLE
|
|
423
|
+ viewState: WebViewState.IDLE,
|
424
|
424
|
});
|
425
|
425
|
this._updateNavigationState(event);
|
426
|
426
|
};
|
|
@@ -440,13 +440,13 @@ export default class WebView extends React.Component<
|
440
|
440
|
return;
|
441
|
441
|
}
|
442
|
442
|
|
443
|
|
- this._showRedboxOnPropChanges(prevProps, "allowsInlineMediaPlayback");
|
444
|
|
- this._showRedboxOnPropChanges(prevProps, "mediaPlaybackRequiresUserAction");
|
445
|
|
- this._showRedboxOnPropChanges(prevProps, "dataDetectorTypes");
|
|
443
|
+ this._showRedboxOnPropChanges(prevProps, 'allowsInlineMediaPlayback');
|
|
444
|
+ this._showRedboxOnPropChanges(prevProps, 'mediaPlaybackRequiresUserAction');
|
|
445
|
+ this._showRedboxOnPropChanges(prevProps, 'dataDetectorTypes');
|
446
|
446
|
|
447
|
447
|
if (this.props.scalesPageToFit !== undefined) {
|
448
|
448
|
console.warn(
|
449
|
|
- "The scalesPageToFit property is not supported when useWebKit = true"
|
|
449
|
+ 'The scalesPageToFit property is not supported when useWebKit = true',
|
450
|
450
|
);
|
451
|
451
|
}
|
452
|
452
|
}
|
|
@@ -454,53 +454,53 @@ export default class WebView extends React.Component<
|
454
|
454
|
_showRedboxOnPropChanges(
|
455
|
455
|
prevProps: WebViewSharedProps,
|
456
|
456
|
propName:
|
457
|
|
- | "allowsInlineMediaPlayback"
|
458
|
|
- | "mediaPlaybackRequiresUserAction"
|
459
|
|
- | "dataDetectorTypes"
|
|
457
|
+ | 'allowsInlineMediaPlayback'
|
|
458
|
+ | 'mediaPlaybackRequiresUserAction'
|
|
459
|
+ | 'dataDetectorTypes',
|
460
|
460
|
) {
|
461
|
461
|
if (this.props[propName] !== prevProps[propName]) {
|
462
|
462
|
console.error(
|
463
|
|
- `Changes to property ${propName} do nothing after the initial render.`
|
|
463
|
+ `Changes to property ${propName} do nothing after the initial render.`,
|
464
|
464
|
);
|
465
|
465
|
}
|
466
|
466
|
}
|
467
|
467
|
}
|
468
|
468
|
|
469
|
|
-const RNCUIWebView = requireNativeComponent("RNCUIWebView");
|
470
|
|
-const RNCWKWebView = requireNativeComponent("RNCWKWebView");
|
|
469
|
+const RNCUIWebView = requireNativeComponent('RNCUIWebView');
|
|
470
|
+const RNCWKWebView = requireNativeComponent('RNCWKWebView');
|
471
|
471
|
|
472
|
472
|
const styles = StyleSheet.create({
|
473
|
473
|
container: {
|
474
|
|
- flex: 1
|
|
474
|
+ flex: 1,
|
475
|
475
|
},
|
476
|
476
|
errorContainer: {
|
477
|
477
|
flex: 1,
|
478
|
|
- justifyContent: "center",
|
479
|
|
- alignItems: "center",
|
480
|
|
- backgroundColor: BGWASH
|
|
478
|
+ justifyContent: 'center',
|
|
479
|
+ alignItems: 'center',
|
|
480
|
+ backgroundColor: BGWASH,
|
481
|
481
|
},
|
482
|
482
|
errorText: {
|
483
|
483
|
fontSize: 14,
|
484
|
|
- textAlign: "center",
|
485
|
|
- marginBottom: 2
|
|
484
|
+ textAlign: 'center',
|
|
485
|
+ marginBottom: 2,
|
486
|
486
|
},
|
487
|
487
|
errorTextTitle: {
|
488
|
488
|
fontSize: 15,
|
489
|
|
- fontWeight: "500",
|
490
|
|
- marginBottom: 10
|
|
489
|
+ fontWeight: '500',
|
|
490
|
+ marginBottom: 10,
|
491
|
491
|
},
|
492
|
492
|
hidden: {
|
493
|
493
|
height: 0,
|
494
|
|
- flex: 0 // disable 'flex:1' when hiding a View
|
|
494
|
+ flex: 0, // disable 'flex:1' when hiding a View
|
495
|
495
|
},
|
496
|
496
|
loadingView: {
|
497
|
497
|
backgroundColor: BGWASH,
|
498
|
498
|
flex: 1,
|
499
|
|
- justifyContent: "center",
|
500
|
|
- alignItems: "center",
|
501
|
|
- height: 100
|
|
499
|
+ justifyContent: 'center',
|
|
500
|
+ alignItems: 'center',
|
|
501
|
+ height: 100,
|
502
|
502
|
},
|
503
|
503
|
webView: {
|
504
|
|
- backgroundColor: "#ffffff"
|
505
|
|
- }
|
|
504
|
+ backgroundColor: '#ffffff',
|
|
505
|
+ },
|
506
|
506
|
});
|