@momo-kits/webview 0.0.65-beta → 0.0.65-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +1 -46
- package/index.js +0 -2
- package/lib/WebView.android.d.ts +4 -72
- package/lib/WebView.android.d.ts.map +1 -1
- package/lib/WebView.android.js +101 -307
- package/lib/WebView.d.ts +2 -2
- package/lib/WebView.d.ts.map +1 -1
- package/lib/WebView.ios.d.ts +4 -80
- package/lib/WebView.ios.d.ts.map +1 -1
- package/lib/WebView.ios.js +86 -221
- package/lib/WebView.js +7 -12
- package/lib/WebView.macos.d.ts +4 -80
- package/lib/WebView.macos.d.ts.map +1 -1
- package/lib/WebView.macos.js +83 -219
- package/lib/WebView.styles.js +0 -2
- package/lib/WebView.windows.d.ts +5 -26
- package/lib/WebView.windows.d.ts.map +1 -1
- package/lib/WebView.windows.js +114 -161
- package/lib/WebViewNativeComponent.android.d.ts +4 -0
- package/lib/WebViewNativeComponent.android.d.ts.map +1 -0
- package/lib/WebViewNativeComponent.android.js +3 -0
- package/lib/WebViewNativeComponent.ios.d.ts +4 -0
- package/lib/WebViewNativeComponent.ios.d.ts.map +1 -0
- package/lib/WebViewNativeComponent.ios.js +3 -0
- package/lib/WebViewNativeComponent.macos.d.ts +4 -0
- package/lib/WebViewNativeComponent.macos.d.ts.map +1 -0
- package/lib/WebViewNativeComponent.macos.js +3 -0
- package/lib/WebViewNativeComponent.windows.d.ts +4 -0
- package/lib/WebViewNativeComponent.windows.d.ts.map +1 -0
- package/lib/WebViewNativeComponent.windows.js +3 -0
- package/lib/WebViewShared.d.ts +32 -2
- package/lib/WebViewShared.d.ts.map +1 -1
- package/lib/WebViewShared.js +99 -16
- package/lib/WebViewTypes.d.ts +242 -14
- package/lib/WebViewTypes.d.ts.map +1 -1
- package/lib/WebViewTypes.js +4 -3
- package/lib/index.d.ts.map +1 -0
- package/package.json +15 -15
- package/publish.sh +2 -2
package/lib/WebView.windows.js
CHANGED
|
@@ -9,21 +9,42 @@
|
|
|
9
9
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
10
10
|
* Licensed under the MIT License.
|
|
11
11
|
*/
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
12
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
22
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
23
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
24
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
25
|
+
function step(op) {
|
|
26
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
27
|
+
while (_) try {
|
|
28
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
29
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
30
|
+
switch (op[0]) {
|
|
31
|
+
case 0: case 1: t = op; break;
|
|
32
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
33
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
34
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
35
|
+
default:
|
|
36
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
37
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
38
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
39
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
40
|
+
if (t[2]) _.ops.pop();
|
|
41
|
+
_.trys.pop(); continue;
|
|
42
|
+
}
|
|
43
|
+
op = body.call(thisArg, _);
|
|
44
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
45
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
|
+
}
|
|
47
|
+
};
|
|
27
48
|
var __rest = (this && this.__rest) || function (s, e) {
|
|
28
49
|
var t = {};
|
|
29
50
|
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
@@ -35,155 +56,87 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
35
56
|
}
|
|
36
57
|
return t;
|
|
37
58
|
};
|
|
38
|
-
import React from 'react';
|
|
39
|
-
import {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
height: 0,
|
|
50
|
-
flex: 0
|
|
51
|
-
},
|
|
52
|
-
loadingView: {
|
|
53
|
-
flex: 1,
|
|
54
|
-
justifyContent: 'center',
|
|
55
|
-
alignItems: 'center'
|
|
56
|
-
},
|
|
57
|
-
loadingProgressBar: {
|
|
58
|
-
height: 20
|
|
59
|
-
}
|
|
59
|
+
import React, { forwardRef, useCallback, useImperativeHandle, useRef } from 'react';
|
|
60
|
+
import { View, Image, NativeModules, } from 'react-native';
|
|
61
|
+
// @ts-expect-error react-native doesn't have this type
|
|
62
|
+
import codegenNativeCommandsUntyped from 'react-native/Libraries/Utilities/codegenNativeCommands';
|
|
63
|
+
import invariant from 'invariant';
|
|
64
|
+
import { RCTWebView, RCTWebView2 } from "./WebViewNativeComponent.windows";
|
|
65
|
+
import { useWebWiewLogic, defaultOriginWhitelist, defaultRenderError, defaultRenderLoading, } from './WebViewShared';
|
|
66
|
+
import styles from './WebView.styles';
|
|
67
|
+
var codegenNativeCommands = codegenNativeCommandsUntyped;
|
|
68
|
+
var Commands = codegenNativeCommands({
|
|
69
|
+
supportedCommands: ['goBack', 'goForward', 'reload', 'stopLoading', 'injectJavaScript', 'requestFocus', 'postMessage', 'loadUrl']
|
|
60
70
|
});
|
|
61
|
-
var
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
_this.goForward = function () {
|
|
71
|
-
UIManager.dispatchViewManagerCommand(_this.getWebViewHandle(), UIManager.getViewManagerConfig('RCTWebView').Commands.goForward, undefined);
|
|
72
|
-
};
|
|
73
|
-
_this.goBack = function () {
|
|
74
|
-
UIManager.dispatchViewManagerCommand(_this.getWebViewHandle(), UIManager.getViewManagerConfig('RCTWebView').Commands.goBack, undefined);
|
|
75
|
-
};
|
|
76
|
-
_this.reload = function () {
|
|
77
|
-
UIManager.dispatchViewManagerCommand(_this.getWebViewHandle(), UIManager.getViewManagerConfig('RCTWebView').Commands.reload, undefined);
|
|
78
|
-
};
|
|
79
|
-
_this.injectJavaScript = function (data) {
|
|
80
|
-
UIManager.dispatchViewManagerCommand(_this.getWebViewHandle(), UIManager.getViewManagerConfig('RCTWebView').Commands.injectJavaScript, [data]);
|
|
81
|
-
};
|
|
82
|
-
_this.postMessage = function (data) {
|
|
83
|
-
UIManager.dispatchViewManagerCommand(_this.getWebViewHandle(), UIManager.getViewManagerConfig('RCTWebView').Commands.postMessage, [String(data)]);
|
|
84
|
-
};
|
|
85
|
-
/**
|
|
86
|
-
* We return an event with a bunch of fields including:
|
|
87
|
-
* url, title, loading, canGoBack, canGoForward
|
|
88
|
-
*/
|
|
89
|
-
_this.updateNavigationState = function (event) {
|
|
90
|
-
if (_this.props.onNavigationStateChange) {
|
|
91
|
-
_this.props.onNavigationStateChange(event.nativeEvent);
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
_this.getWebViewHandle = function () {
|
|
95
|
-
// eslint-disable-next-line react/no-string-refs
|
|
96
|
-
return findNodeHandle(_this.webViewRef.current);
|
|
97
|
-
};
|
|
98
|
-
_this.onLoadingStart = function (event) {
|
|
99
|
-
var onLoadStart = _this.props.onLoadStart;
|
|
100
|
-
if (onLoadStart) {
|
|
101
|
-
onLoadStart(event);
|
|
102
|
-
}
|
|
103
|
-
_this.updateNavigationState(event);
|
|
104
|
-
};
|
|
105
|
-
_this.onLoadingProgress = function (event) {
|
|
106
|
-
var onLoadProgress = _this.props.onLoadProgress;
|
|
107
|
-
if (onLoadProgress) {
|
|
108
|
-
onLoadProgress(event);
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
_this.onLoadingError = function (event) {
|
|
112
|
-
event.persist(); // persist this event because we need to store it
|
|
113
|
-
var _a = _this.props, onError = _a.onError, onLoadEnd = _a.onLoadEnd;
|
|
114
|
-
if (onError) {
|
|
115
|
-
onError(event);
|
|
116
|
-
}
|
|
117
|
-
if (onLoadEnd) {
|
|
118
|
-
onLoadEnd(event);
|
|
119
|
-
}
|
|
120
|
-
console.error('Encountered an error loading page', event.nativeEvent);
|
|
121
|
-
_this.setState({
|
|
122
|
-
lastErrorEvent: event.nativeEvent,
|
|
123
|
-
viewState: 'ERROR'
|
|
124
|
-
});
|
|
125
|
-
};
|
|
126
|
-
_this.onLoadingFinish = function (event) {
|
|
127
|
-
var _a = _this.props, onLoad = _a.onLoad, onLoadEnd = _a.onLoadEnd;
|
|
128
|
-
if (onLoad) {
|
|
129
|
-
onLoad(event);
|
|
130
|
-
}
|
|
131
|
-
if (onLoadEnd) {
|
|
132
|
-
onLoadEnd(event);
|
|
133
|
-
}
|
|
134
|
-
_this.setState({
|
|
135
|
-
viewState: 'IDLE'
|
|
136
|
-
});
|
|
137
|
-
_this.updateNavigationState(event);
|
|
138
|
-
};
|
|
139
|
-
_this.onMessage = function (event) {
|
|
140
|
-
var onMessage = _this.props.onMessage;
|
|
141
|
-
if (onMessage) {
|
|
142
|
-
onMessage(event);
|
|
71
|
+
var resolveAssetSource = Image.resolveAssetSource;
|
|
72
|
+
var WebViewComponent = forwardRef(function (_a, ref) {
|
|
73
|
+
var _b = _a.cacheEnabled, cacheEnabled = _b === void 0 ? true : _b, _c = _a.originWhitelist, originWhitelist = _c === void 0 ? defaultOriginWhitelist : _c, startInLoadingState = _a.startInLoadingState, onNavigationStateChange = _a.onNavigationStateChange, onLoadStart = _a.onLoadStart, onError = _a.onError, onLoad = _a.onLoad, onLoadEnd = _a.onLoadEnd, onLoadProgress = _a.onLoadProgress, onHttpErrorProp = _a.onHttpError, onMessageProp = _a.onMessage, renderLoading = _a.renderLoading, renderError = _a.renderError, style = _a.style, containerStyle = _a.containerStyle, source = _a.source, nativeConfig = _a.nativeConfig, onShouldStartLoadWithRequestProp = _a.onShouldStartLoadWithRequest, useWebView2 = _a.useWebView2, otherProps = __rest(_a, ["cacheEnabled", "originWhitelist", "startInLoadingState", "onNavigationStateChange", "onLoadStart", "onError", "onLoad", "onLoadEnd", "onLoadProgress", "onHttpError", "onMessage", "renderLoading", "renderError", "style", "containerStyle", "source", "nativeConfig", "onShouldStartLoadWithRequest", "useWebView2"]);
|
|
74
|
+
var webViewRef = useRef(null);
|
|
75
|
+
var RCTWebViewString = useWebView2 ? 'RCTWebView2' : 'RCTWebView';
|
|
76
|
+
var onShouldStartLoadWithRequestCallback = useCallback(function (shouldStart, url, lockIdentifier) {
|
|
77
|
+
if (lockIdentifier) {
|
|
78
|
+
if (RCTWebViewString === 'RCTWebView') {
|
|
79
|
+
NativeModules.RCTWebView.onShouldStartLoadWithRequestCallback(shouldStart, lockIdentifier);
|
|
143
80
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
var onHttpError = _this.props.onHttpError;
|
|
147
|
-
if (onHttpError) {
|
|
148
|
-
onHttpError(event);
|
|
81
|
+
else {
|
|
82
|
+
NativeModules.RCTWebView2.onShouldStartLoadWithRequestCallback(shouldStart, lockIdentifier);
|
|
149
83
|
}
|
|
150
|
-
};
|
|
151
|
-
return _this;
|
|
152
|
-
}
|
|
153
|
-
WebView.prototype.render = function () {
|
|
154
|
-
var _a = this.props, _b = _a.nativeConfig, nativeConfig = _b === void 0 ? {} : _b, onMessage = _a.onMessage, onShouldStartLoadWithRequestProp = _a.onShouldStartLoadWithRequest, originWhitelist = _a.originWhitelist, renderError = _a.renderError, renderLoading = _a.renderLoading, style = _a.style, containerStyle = _a.containerStyle, otherProps = __rest(_a, ["nativeConfig", "onMessage", "onShouldStartLoadWithRequest", "originWhitelist", "renderError", "renderLoading", "style", "containerStyle"]);
|
|
155
|
-
var otherView = null;
|
|
156
|
-
if (this.state.viewState === 'LOADING') {
|
|
157
|
-
otherView = this.props.renderLoading && this.props.renderLoading();
|
|
158
|
-
}
|
|
159
|
-
else if (this.state.viewState === 'ERROR') {
|
|
160
|
-
var errorEvent = this.state.lastErrorEvent;
|
|
161
|
-
otherView = this.props.renderError
|
|
162
|
-
&& this.props.renderError(errorEvent.domain, errorEvent.code, errorEvent.description);
|
|
163
84
|
}
|
|
164
|
-
else if (
|
|
165
|
-
|
|
85
|
+
else if (shouldStart) {
|
|
86
|
+
Commands.loadUrl(webViewRef, url);
|
|
166
87
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
88
|
+
}, [RCTWebViewString]);
|
|
89
|
+
var _d = useWebWiewLogic({
|
|
90
|
+
onNavigationStateChange: onNavigationStateChange,
|
|
91
|
+
onLoad: onLoad,
|
|
92
|
+
onError: onError,
|
|
93
|
+
onHttpErrorProp: onHttpErrorProp,
|
|
94
|
+
onLoadEnd: onLoadEnd,
|
|
95
|
+
onLoadProgress: onLoadProgress,
|
|
96
|
+
onLoadStart: onLoadStart,
|
|
97
|
+
onMessageProp: onMessageProp,
|
|
98
|
+
startInLoadingState: startInLoadingState,
|
|
99
|
+
originWhitelist: originWhitelist,
|
|
100
|
+
onShouldStartLoadWithRequestProp: onShouldStartLoadWithRequestProp,
|
|
101
|
+
onShouldStartLoadWithRequestCallback: onShouldStartLoadWithRequestCallback
|
|
102
|
+
}), onLoadingStart = _d.onLoadingStart, onShouldStartLoadWithRequest = _d.onShouldStartLoadWithRequest, onMessage = _d.onMessage, viewState = _d.viewState, setViewState = _d.setViewState, lastErrorEvent = _d.lastErrorEvent, onHttpError = _d.onHttpError, onLoadingError = _d.onLoadingError, onLoadingFinish = _d.onLoadingFinish, onLoadingProgress = _d.onLoadingProgress;
|
|
103
|
+
useImperativeHandle(ref, function () { return ({
|
|
104
|
+
goForward: function () { return Commands.goForward(webViewRef.current); },
|
|
105
|
+
goBack: function () { return Commands.goBack(webViewRef.current); },
|
|
106
|
+
reload: function () {
|
|
107
|
+
setViewState('LOADING');
|
|
108
|
+
Commands.reload(webViewRef.current);
|
|
109
|
+
},
|
|
110
|
+
stopLoading: function () { return Commands.stopLoading(webViewRef.current); },
|
|
111
|
+
postMessage: function (data) { return Commands.postMessage(webViewRef.current, data); },
|
|
112
|
+
injectJavaScript: function (data) { return Commands.injectJavaScript(webViewRef.current, data); },
|
|
113
|
+
requestFocus: function () { return Commands.requestFocus(webViewRef.current); }
|
|
114
|
+
}); }, [setViewState, webViewRef]);
|
|
115
|
+
var otherView = null;
|
|
116
|
+
if (viewState === 'LOADING') {
|
|
117
|
+
otherView = (renderLoading || defaultRenderLoading)();
|
|
118
|
+
}
|
|
119
|
+
else if (viewState === 'ERROR') {
|
|
120
|
+
invariant(lastErrorEvent != null, 'lastErrorEvent expected to be non-null');
|
|
121
|
+
otherView = (renderError || defaultRenderError)(lastErrorEvent.domain, lastErrorEvent.code, lastErrorEvent.description);
|
|
122
|
+
}
|
|
123
|
+
else if (viewState !== 'IDLE') {
|
|
124
|
+
console.error("RNCWebView invalid state encountered: ".concat(viewState));
|
|
125
|
+
}
|
|
126
|
+
var webViewStyles = [styles.container, styles.webView, style];
|
|
127
|
+
var webViewContainerStyle = [styles.container, containerStyle];
|
|
128
|
+
var NativeWebView = useWebView2 ? RCTWebView2 : RCTWebView;
|
|
129
|
+
var webView = <NativeWebView key="webViewKey" {...otherProps} messagingEnabled={typeof onMessageProp === 'function'} onLoadingError={onLoadingError} onLoadingFinish={onLoadingFinish} onLoadingProgress={onLoadingProgress} onLoadingStart={onLoadingStart} onHttpError={onHttpError} onMessage={onMessage} onShouldStartLoadWithRequest={onShouldStartLoadWithRequest} ref={webViewRef}
|
|
130
|
+
// TODO: find a better way to type this.
|
|
131
|
+
source={resolveAssetSource(source)} style={webViewStyles} cacheEnabled={cacheEnabled} {...nativeConfig === null || nativeConfig === void 0 ? void 0 : nativeConfig.props}/>;
|
|
132
|
+
return (<View style={webViewContainerStyle}>
|
|
133
|
+
{webView}
|
|
134
|
+
{otherView}
|
|
135
|
+
</View>);
|
|
136
|
+
});
|
|
137
|
+
// native implementation should return "true" only for Android 5+
|
|
138
|
+
var isFileUploadSupported = function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
139
|
+
return [2 /*return*/, false];
|
|
140
|
+
}); }); };
|
|
141
|
+
var WebView = Object.assign(WebViewComponent, { isFileUploadSupported: isFileUploadSupported });
|
|
189
142
|
export default WebView;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WebViewNativeComponent.android.d.ts","sourceRoot":"","sources":["../src/WebViewNativeComponent.android.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAE3D,QAAA,MAAM,UAAU,EAAE,OAAO,oBAExB,CAAC;AAEF,eAAe,UAAU,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WebViewNativeComponent.ios.d.ts","sourceRoot":"","sources":["../src/WebViewNativeComponent.ios.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEvD,QAAA,MAAM,UAAU,EAAE,OAAO,gBAExB,CAAC;AAEF,eAAe,UAAU,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WebViewNativeComponent.macos.d.ts","sourceRoot":"","sources":["../src/WebViewNativeComponent.macos.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAEzD,QAAA,MAAM,UAAU,EAAE,OAAO,kBAExB,CAAC;AAEF,eAAe,UAAU,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WebViewNativeComponent.windows.d.ts","sourceRoot":"","sources":["../src/WebViewNativeComponent.windows.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAE3D,eAAO,MAAM,UAAU,EAAE,OAAO,oBAE/B,CAAC;AAEF,eAAO,MAAM,WAAW,EAAE,OAAO,oBAEhC,CAAC"}
|
package/lib/WebViewShared.d.ts
CHANGED
|
@@ -1,7 +1,37 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { OnShouldStartLoadWithRequest, ShouldStartLoadRequestEvent, WebViewError, WebViewErrorEvent, WebViewHttpErrorEvent, WebViewMessageEvent, WebViewNavigation, WebViewNavigationEvent, WebViewProgressEvent, WebViewRenderProcessGoneEvent, WebViewTerminatedEvent } from './WebViewTypes';
|
|
3
|
+
declare const defaultOriginWhitelist: readonly ["http://*", "https://*"];
|
|
3
4
|
declare const createOnShouldStartLoadWithRequest: (loadRequest: (shouldStart: boolean, url: string, lockIdentifier: number) => void, originWhitelist: readonly string[], onShouldStartLoadWithRequest?: OnShouldStartLoadWithRequest | undefined) => ({ nativeEvent }: ShouldStartLoadRequestEvent) => void;
|
|
4
5
|
declare const defaultRenderLoading: () => JSX.Element;
|
|
5
6
|
declare const defaultRenderError: (errorDomain: string | undefined, errorCode: number, errorDesc: string) => JSX.Element;
|
|
6
7
|
export { defaultOriginWhitelist, createOnShouldStartLoadWithRequest, defaultRenderLoading, defaultRenderError, };
|
|
8
|
+
export declare const useWebWiewLogic: ({ startInLoadingState, onNavigationStateChange, onLoadStart, onLoad, onLoadProgress, onLoadEnd, onError, onHttpErrorProp, onMessageProp, onRenderProcessGoneProp, onContentProcessDidTerminateProp, originWhitelist, onShouldStartLoadWithRequestProp, onShouldStartLoadWithRequestCallback, }: {
|
|
9
|
+
startInLoadingState?: boolean | undefined;
|
|
10
|
+
onNavigationStateChange?: ((event: WebViewNavigation) => void) | undefined;
|
|
11
|
+
onLoadStart?: ((event: WebViewNavigationEvent) => void) | undefined;
|
|
12
|
+
onLoad?: ((event: WebViewNavigationEvent) => void) | undefined;
|
|
13
|
+
onLoadProgress?: ((event: WebViewProgressEvent) => void) | undefined;
|
|
14
|
+
onLoadEnd?: ((event: WebViewNavigationEvent | WebViewErrorEvent) => void) | undefined;
|
|
15
|
+
onError?: ((event: WebViewErrorEvent) => void) | undefined;
|
|
16
|
+
onHttpErrorProp?: ((event: WebViewHttpErrorEvent) => void) | undefined;
|
|
17
|
+
onMessageProp?: ((event: WebViewMessageEvent) => void) | undefined;
|
|
18
|
+
onRenderProcessGoneProp?: ((event: WebViewRenderProcessGoneEvent) => void) | undefined;
|
|
19
|
+
onContentProcessDidTerminateProp?: ((event: WebViewTerminatedEvent) => void) | undefined;
|
|
20
|
+
originWhitelist: readonly string[];
|
|
21
|
+
onShouldStartLoadWithRequestProp?: OnShouldStartLoadWithRequest | undefined;
|
|
22
|
+
onShouldStartLoadWithRequestCallback: (shouldStart: boolean, url: string, lockIdentifier?: number | undefined) => void;
|
|
23
|
+
}) => {
|
|
24
|
+
onShouldStartLoadWithRequest: ({ nativeEvent }: ShouldStartLoadRequestEvent) => void;
|
|
25
|
+
onLoadingStart: (event: WebViewNavigationEvent) => void;
|
|
26
|
+
onLoadingProgress: (event: WebViewProgressEvent) => void;
|
|
27
|
+
onLoadingError: (event: WebViewErrorEvent) => void;
|
|
28
|
+
onLoadingFinish: (event: WebViewNavigationEvent) => void;
|
|
29
|
+
onHttpError: (event: WebViewHttpErrorEvent) => void;
|
|
30
|
+
onRenderProcessGone: (event: WebViewRenderProcessGoneEvent) => void;
|
|
31
|
+
onContentProcessDidTerminate: (event: WebViewTerminatedEvent) => void;
|
|
32
|
+
onMessage: (event: WebViewMessageEvent) => void;
|
|
33
|
+
viewState: "IDLE" | "LOADING" | "ERROR";
|
|
34
|
+
setViewState: React.Dispatch<React.SetStateAction<"IDLE" | "LOADING" | "ERROR">>;
|
|
35
|
+
lastErrorEvent: WebViewError | null;
|
|
36
|
+
};
|
|
7
37
|
//# sourceMappingURL=WebViewShared.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebViewShared.d.ts","sourceRoot":"","sources":["../src/WebViewShared.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"WebViewShared.d.ts","sourceRoot":"","sources":["../src/WebViewShared.tsx"],"names":[],"mappings":"AACA,OAAO,KAAiD,MAAM,OAAO,CAAC;AAEtE,OAAO,EACL,4BAA4B,EAC5B,2BAA2B,EAC3B,YAAY,EACZ,iBAAiB,EACjB,qBAAqB,EACrB,mBAAmB,EACnB,iBAAiB,EACjB,sBAAsB,EACtB,oBAAoB,EACpB,6BAA6B,EAC7B,sBAAsB,EACvB,MAAM,gBAAgB,CAAC;AAGxB,QAAA,MAAM,sBAAsB,oCAAqC,CAAC;AAuBlE,QAAA,MAAM,kCAAkC,8BAEvB,OAAO,OACf,MAAM,kBACK,MAAM,KACnB,IAAI,mBACQ,SAAS,MAAM,EAAE,oIAwBnC,CAAC;AAEF,QAAA,MAAM,oBAAoB,mBAIzB,CAAC;AACF,QAAA,MAAM,kBAAkB,gBACT,MAAM,GAAG,SAAS,aACpB,MAAM,aACN,MAAM,gBAQlB,CAAC;AAEF,OAAO,EACL,sBAAsB,EACtB,kCAAkC,EAClC,oBAAoB,EACpB,kBAAkB,GACnB,CAAC;AAEF,eAAO,MAAM,eAAe;;uCAiBQ,iBAAiB,KAAK,IAAI;sDACX,IAAI;iDACT,IAAI;uDACE,IAAI;yBAClC,sBAAsB,GAAG,iBAAiB,KAAK,IAAI;6CAC/B,IAAI;yDACQ,IAAI;qDACR,IAAI;yEACgB,IAAI;2EACF,IAAI;qBACzD,SAAS,MAAM,EAAE;;wDAEkB,OAAO,OAAO,MAAM,mBAAmB,MAAM,GAAG,SAAS,KAAK,IAAI;;;;;;;;;;;;;;CAiGvH,CAAC"}
|
package/lib/WebViewShared.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
return
|
|
1
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
2
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
3
|
+
if (ar || !(i in from)) {
|
|
4
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
5
|
+
ar[i] = from[i];
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
9
9
|
};
|
|
10
10
|
import escapeStringRegexp from 'escape-string-regexp';
|
|
11
|
-
import React from 'react';
|
|
12
|
-
import { Linking, View, ActivityIndicator, Text } from 'react-native';
|
|
11
|
+
import React, { useCallback, useMemo, useRef, useState } from 'react';
|
|
12
|
+
import { Linking, View, ActivityIndicator, Text, Platform } from 'react-native';
|
|
13
13
|
import styles from './WebView.styles';
|
|
14
14
|
var defaultOriginWhitelist = ['http://*', 'https://*'];
|
|
15
15
|
var extractOrigin = function (url) {
|
|
@@ -17,14 +17,14 @@ var extractOrigin = function (url) {
|
|
|
17
17
|
return result === null ? '' : result[0];
|
|
18
18
|
};
|
|
19
19
|
var originWhitelistToRegex = function (originWhitelist) {
|
|
20
|
-
return "^"
|
|
20
|
+
return "^".concat(escapeStringRegexp(originWhitelist).replace(/\\\*/g, '.*'));
|
|
21
21
|
};
|
|
22
22
|
var passesWhitelist = function (compiledWhitelist, url) {
|
|
23
23
|
var origin = extractOrigin(url);
|
|
24
24
|
return compiledWhitelist.some(function (x) { return new RegExp(x).test(origin); });
|
|
25
25
|
};
|
|
26
26
|
var compileWhitelist = function (originWhitelist) {
|
|
27
|
-
return
|
|
27
|
+
return __spreadArray(['about:blank'], (originWhitelist || []), true).map(originWhitelistToRegex);
|
|
28
28
|
};
|
|
29
29
|
var createOnShouldStartLoadWithRequest = function (loadRequest, originWhitelist, onShouldStartLoadWithRequest) {
|
|
30
30
|
return function (_a) {
|
|
@@ -36,7 +36,7 @@ var createOnShouldStartLoadWithRequest = function (loadRequest, originWhitelist,
|
|
|
36
36
|
if (supported) {
|
|
37
37
|
return Linking.openURL(url);
|
|
38
38
|
}
|
|
39
|
-
console.warn("Can't open url: "
|
|
39
|
+
console.warn("Can't open url: ".concat(url));
|
|
40
40
|
return undefined;
|
|
41
41
|
})["catch"](function (e) {
|
|
42
42
|
console.warn('Error opening URL: ', e);
|
|
@@ -54,8 +54,91 @@ var defaultRenderLoading = function () { return (<View style={styles.loadingOrEr
|
|
|
54
54
|
</View>); };
|
|
55
55
|
var defaultRenderError = function (errorDomain, errorCode, errorDesc) { return (<View style={styles.loadingOrErrorView}>
|
|
56
56
|
<Text style={styles.errorTextTitle}>Error loading page</Text>
|
|
57
|
-
<Text style={styles.errorText}>{"Domain: "
|
|
58
|
-
<Text style={styles.errorText}>{"Error Code: "
|
|
59
|
-
<Text style={styles.errorText}>{"Description: "
|
|
57
|
+
<Text style={styles.errorText}>{"Domain: ".concat(errorDomain)}</Text>
|
|
58
|
+
<Text style={styles.errorText}>{"Error Code: ".concat(errorCode)}</Text>
|
|
59
|
+
<Text style={styles.errorText}>{"Description: ".concat(errorDesc)}</Text>
|
|
60
60
|
</View>); };
|
|
61
61
|
export { defaultOriginWhitelist, createOnShouldStartLoadWithRequest, defaultRenderLoading, defaultRenderError, };
|
|
62
|
+
export var useWebWiewLogic = function (_a) {
|
|
63
|
+
var startInLoadingState = _a.startInLoadingState, onNavigationStateChange = _a.onNavigationStateChange, onLoadStart = _a.onLoadStart, onLoad = _a.onLoad, onLoadProgress = _a.onLoadProgress, onLoadEnd = _a.onLoadEnd, onError = _a.onError, onHttpErrorProp = _a.onHttpErrorProp, onMessageProp = _a.onMessageProp, onRenderProcessGoneProp = _a.onRenderProcessGoneProp, onContentProcessDidTerminateProp = _a.onContentProcessDidTerminateProp, originWhitelist = _a.originWhitelist, onShouldStartLoadWithRequestProp = _a.onShouldStartLoadWithRequestProp, onShouldStartLoadWithRequestCallback = _a.onShouldStartLoadWithRequestCallback;
|
|
64
|
+
var _b = useState(startInLoadingState ? "LOADING" : "IDLE"), viewState = _b[0], setViewState = _b[1];
|
|
65
|
+
var _c = useState(null), lastErrorEvent = _c[0], setLastErrorEvent = _c[1];
|
|
66
|
+
var startUrl = useRef(null);
|
|
67
|
+
var updateNavigationState = useCallback(function (event) {
|
|
68
|
+
onNavigationStateChange === null || onNavigationStateChange === void 0 ? void 0 : onNavigationStateChange(event.nativeEvent);
|
|
69
|
+
}, [onNavigationStateChange]);
|
|
70
|
+
var onLoadingStart = useCallback(function (event) {
|
|
71
|
+
// Needed for android
|
|
72
|
+
startUrl.current = event.nativeEvent.url;
|
|
73
|
+
// !Needed for android
|
|
74
|
+
onLoadStart === null || onLoadStart === void 0 ? void 0 : onLoadStart(event);
|
|
75
|
+
updateNavigationState(event);
|
|
76
|
+
}, [onLoadStart, updateNavigationState]);
|
|
77
|
+
var onLoadingError = useCallback(function (event) {
|
|
78
|
+
event.persist();
|
|
79
|
+
if (onError) {
|
|
80
|
+
onError(event);
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
console.warn('Encountered an error loading page', event.nativeEvent);
|
|
84
|
+
}
|
|
85
|
+
onLoadEnd === null || onLoadEnd === void 0 ? void 0 : onLoadEnd(event);
|
|
86
|
+
if (event.isDefaultPrevented()) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
;
|
|
90
|
+
setViewState('ERROR');
|
|
91
|
+
setLastErrorEvent(event.nativeEvent);
|
|
92
|
+
}, [onError, onLoadEnd]);
|
|
93
|
+
var onHttpError = useCallback(function (event) {
|
|
94
|
+
onHttpErrorProp === null || onHttpErrorProp === void 0 ? void 0 : onHttpErrorProp(event);
|
|
95
|
+
}, [onHttpErrorProp]);
|
|
96
|
+
// Android Only
|
|
97
|
+
var onRenderProcessGone = useCallback(function (event) {
|
|
98
|
+
onRenderProcessGoneProp === null || onRenderProcessGoneProp === void 0 ? void 0 : onRenderProcessGoneProp(event);
|
|
99
|
+
}, [onRenderProcessGoneProp]);
|
|
100
|
+
// !Android Only
|
|
101
|
+
// iOS Only
|
|
102
|
+
var onContentProcessDidTerminate = useCallback(function (event) {
|
|
103
|
+
onContentProcessDidTerminateProp === null || onContentProcessDidTerminateProp === void 0 ? void 0 : onContentProcessDidTerminateProp(event);
|
|
104
|
+
}, [onContentProcessDidTerminateProp]);
|
|
105
|
+
// !iOS Only
|
|
106
|
+
var onLoadingFinish = useCallback(function (event) {
|
|
107
|
+
onLoad === null || onLoad === void 0 ? void 0 : onLoad(event);
|
|
108
|
+
onLoadEnd === null || onLoadEnd === void 0 ? void 0 : onLoadEnd(event);
|
|
109
|
+
var url = event.nativeEvent.url;
|
|
110
|
+
// on Android, only if url === startUrl
|
|
111
|
+
if (Platform.OS !== "android" || url === startUrl.current) {
|
|
112
|
+
setViewState('IDLE');
|
|
113
|
+
}
|
|
114
|
+
// !on Android, only if url === startUrl
|
|
115
|
+
updateNavigationState(event);
|
|
116
|
+
}, [onLoad, onLoadEnd, updateNavigationState]);
|
|
117
|
+
var onMessage = useCallback(function (event) {
|
|
118
|
+
onMessageProp === null || onMessageProp === void 0 ? void 0 : onMessageProp(event);
|
|
119
|
+
}, [onMessageProp]);
|
|
120
|
+
var onLoadingProgress = useCallback(function (event) {
|
|
121
|
+
var progress = event.nativeEvent.progress;
|
|
122
|
+
// patch for Android only
|
|
123
|
+
if (Platform.OS === "android" && progress === 1) {
|
|
124
|
+
setViewState(function (prevViewState) { return prevViewState === 'LOADING' ? 'IDLE' : prevViewState; });
|
|
125
|
+
}
|
|
126
|
+
// !patch for Android only
|
|
127
|
+
onLoadProgress === null || onLoadProgress === void 0 ? void 0 : onLoadProgress(event);
|
|
128
|
+
}, [onLoadProgress]);
|
|
129
|
+
var onShouldStartLoadWithRequest = useMemo(function () { return createOnShouldStartLoadWithRequest(onShouldStartLoadWithRequestCallback, originWhitelist, onShouldStartLoadWithRequestProp); }, [originWhitelist, onShouldStartLoadWithRequestProp, onShouldStartLoadWithRequestCallback]);
|
|
130
|
+
return {
|
|
131
|
+
onShouldStartLoadWithRequest: onShouldStartLoadWithRequest,
|
|
132
|
+
onLoadingStart: onLoadingStart,
|
|
133
|
+
onLoadingProgress: onLoadingProgress,
|
|
134
|
+
onLoadingError: onLoadingError,
|
|
135
|
+
onLoadingFinish: onLoadingFinish,
|
|
136
|
+
onHttpError: onHttpError,
|
|
137
|
+
onRenderProcessGone: onRenderProcessGone,
|
|
138
|
+
onContentProcessDidTerminate: onContentProcessDidTerminate,
|
|
139
|
+
onMessage: onMessage,
|
|
140
|
+
viewState: viewState,
|
|
141
|
+
setViewState: setViewState,
|
|
142
|
+
lastErrorEvent: lastErrorEvent
|
|
143
|
+
};
|
|
144
|
+
};
|