@exodus/react-native-webview 11.26.1-exodus.9 → 13.16.0-exodus.0
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/README.md +36 -63
- package/android/build.gradle +83 -110
- package/android/gradle.properties +3 -4
- package/android/src/main/AndroidManifest.xml +12 -0
- package/android/src/main/AndroidManifestNew.xml +26 -0
- package/android/src/main/java/com/reactnativecommunity/webview/RNCBasicAuthCredential.java +11 -0
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebChromeClient.java +407 -0
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java +468 -0
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewClient.java +330 -0
- package/android/src/main/java/com/reactnativecommunity/webview/{WebViewConfig.java → RNCWebViewConfig.java} +3 -4
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewFileProvider.java +1 -1
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt +746 -0
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewMessagingModule.kt +9 -0
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewModuleImpl.java +554 -0
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewPackage.java +57 -12
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewWrapper.kt +39 -0
- package/android/src/main/java/com/reactnativecommunity/webview/events/SubResourceErrorEvent.kt +25 -0
- package/android/src/main/java/com/reactnativecommunity/webview/events/TopCustomMenuSelectionEvent.kt +24 -0
- package/android/src/main/java/com/reactnativecommunity/webview/events/TopHttpErrorEvent.kt +25 -0
- package/android/src/main/java/com/reactnativecommunity/webview/events/TopNewWindowEvent.kt +25 -0
- package/android/src/main/java/com/reactnativecommunity/webview/events/TopRenderProcessGoneEvent.kt +25 -0
- package/android/src/newarch/com/reactnativecommunity/webview/RNCWebViewManager.java +570 -0
- package/android/src/newarch/com/reactnativecommunity/webview/RNCWebViewModule.java +57 -0
- package/android/src/oldarch/com/reactnativecommunity/webview/RNCWebViewManager.java +341 -0
- package/android/src/oldarch/com/reactnativecommunity/webview/RNCWebViewModule.java +59 -0
- package/apple/RCTConvert+WKDataDetectorTypes.h +11 -0
- package/apple/RCTConvert+WKDataDetectorTypes.m +27 -0
- package/apple/RNCWebView.h +26 -100
- package/apple/RNCWebView.mm +555 -0
- package/apple/RNCWebViewDecisionManager.h +20 -0
- package/apple/RNCWebViewDecisionManager.m +47 -0
- package/apple/RNCWebViewImpl.h +164 -0
- package/apple/{RNCWebView.m → RNCWebViewImpl.m} +803 -226
- package/apple/RNCWebViewManager.h +4 -8
- package/apple/RNCWebViewManager.mm +221 -0
- package/apple/RNCWebViewModule.h +23 -0
- package/apple/RNCWebViewModule.mm +34 -0
- package/index.d.ts +2 -3
- package/lib/NativeRNCWebViewModule.d.ts +8 -0
- package/lib/NativeRNCWebViewModule.js +1 -0
- package/lib/RNCWebViewNativeComponent.d.ts +245 -0
- package/lib/RNCWebViewNativeComponent.js +1 -0
- package/lib/WebView.android.d.ts +0 -1
- package/lib/WebView.android.js +1 -135
- package/lib/WebView.d.ts +2 -3
- package/lib/WebView.ios.d.ts +0 -1
- package/lib/WebView.ios.js +1 -114
- package/lib/WebView.js +1 -11
- package/lib/WebView.macos.d.ts +6 -0
- package/lib/WebView.macos.js +1 -0
- package/lib/WebView.styles.d.ts +37 -11
- package/lib/WebView.styles.js +1 -33
- package/lib/WebView.windows.d.ts +17 -0
- package/lib/WebView.windows.js +1 -0
- package/lib/WebViewNativeComponent.macos.d.ts +3 -0
- package/lib/WebViewNativeComponent.macos.js +1 -0
- package/lib/WebViewNativeComponent.windows.d.ts +3 -0
- package/lib/WebViewNativeComponent.windows.js +1 -0
- package/lib/WebViewShared.d.ts +30 -9
- package/lib/WebViewShared.js +1 -174
- package/lib/WebViewTypes.d.ts +514 -98
- package/lib/WebViewTypes.js +1 -6
- package/lib/index.d.ts +0 -1
- package/lib/index.js +1 -3
- package/lib/validation.d.ts +3 -0
- package/lib/validation.js +1 -0
- package/package.json +57 -33
- package/react-native-webview.podspec +32 -5
- package/react-native.config.js +22 -18
- package/src/NativeRNCWebViewModule.ts +13 -0
- package/src/RNCWebViewNativeComponent.ts +348 -0
- package/src/WebView.android.tsx +345 -0
- package/src/WebView.ios.tsx +341 -0
- package/src/WebView.macos.tsx +252 -0
- package/src/WebView.styles.ts +41 -0
- package/src/WebView.tsx +25 -0
- package/src/WebView.windows.tsx +217 -0
- package/src/WebViewNativeComponent.macos.ts +7 -0
- package/src/WebViewNativeComponent.windows.ts +8 -0
- package/src/WebViewShared.tsx +476 -0
- package/src/WebViewTypes.ts +1402 -0
- package/src/__tests__/WebViewShared-test.js +323 -0
- package/src/__tests__/__snapshots__/WebViewShared-test.js.snap +8 -0
- package/src/__tests__/validation-test.js +38 -0
- package/src/index.ts +4 -0
- package/src/validation.ts +20 -0
- package/android/.editorconfig +0 -6
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java +0 -1408
- package/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewModule.java +0 -506
- package/apple/RNCWebViewManager.m +0 -278
- package/lib/WebViewNativeComponent.android.d.ts +0 -4
- package/lib/WebViewNativeComponent.android.js +0 -3
- package/lib/WebViewNativeComponent.ios.d.ts +0 -4
- package/lib/WebViewNativeComponent.ios.js +0 -3
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
forwardRef,
|
|
3
|
+
useCallback,
|
|
4
|
+
useImperativeHandle,
|
|
5
|
+
useRef,
|
|
6
|
+
} from 'react';
|
|
7
|
+
import { Image, View, ImageSourcePropType, HostComponent } from 'react-native';
|
|
8
|
+
import invariant from 'invariant';
|
|
9
|
+
import RNCWebView, { Commands, NativeProps } from './RNCWebViewNativeComponent';
|
|
10
|
+
import RNCWebViewModule from './NativeRNCWebViewModule';
|
|
11
|
+
import {
|
|
12
|
+
defaultOriginWhitelist,
|
|
13
|
+
defaultDeeplinkWhitelist,
|
|
14
|
+
defaultRenderError,
|
|
15
|
+
defaultRenderLoading,
|
|
16
|
+
useWebViewLogic,
|
|
17
|
+
} from './WebViewShared';
|
|
18
|
+
import { MacOSWebViewProps, WebViewSourceUri } from './WebViewTypes';
|
|
19
|
+
|
|
20
|
+
import styles from './WebView.styles';
|
|
21
|
+
|
|
22
|
+
const { resolveAssetSource } = Image;
|
|
23
|
+
|
|
24
|
+
const useWarnIfChanges = <T extends unknown>(value: T, name: string) => {
|
|
25
|
+
const ref = useRef(value);
|
|
26
|
+
if (ref.current !== value) {
|
|
27
|
+
console.warn(
|
|
28
|
+
`Changes to property ${name} do nothing after the initial render.`
|
|
29
|
+
);
|
|
30
|
+
ref.current = value;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const WebViewComponent = forwardRef<{}, MacOSWebViewProps>(
|
|
35
|
+
(
|
|
36
|
+
{
|
|
37
|
+
javaScriptEnabled = true,
|
|
38
|
+
cacheEnabled = true,
|
|
39
|
+
originWhitelist = defaultOriginWhitelist,
|
|
40
|
+
deeplinkWhitelist = defaultDeeplinkWhitelist,
|
|
41
|
+
useSharedProcessPool = true,
|
|
42
|
+
injectedJavaScript,
|
|
43
|
+
injectedJavaScriptBeforeContentLoaded,
|
|
44
|
+
startInLoadingState,
|
|
45
|
+
onNavigationStateChange,
|
|
46
|
+
onLoadStart,
|
|
47
|
+
onError,
|
|
48
|
+
onLoad,
|
|
49
|
+
onLoadEnd,
|
|
50
|
+
onLoadProgress,
|
|
51
|
+
onHttpError: onHttpErrorProp,
|
|
52
|
+
onMessage: onMessageProp,
|
|
53
|
+
renderLoading,
|
|
54
|
+
renderError,
|
|
55
|
+
style,
|
|
56
|
+
containerStyle,
|
|
57
|
+
source,
|
|
58
|
+
nativeConfig,
|
|
59
|
+
allowsInlineMediaPlayback,
|
|
60
|
+
allowsPictureInPictureMediaPlayback = true,
|
|
61
|
+
allowsAirPlayForMediaPlayback,
|
|
62
|
+
mediaPlaybackRequiresUserAction,
|
|
63
|
+
incognito,
|
|
64
|
+
onShouldStartLoadWithRequest: onShouldStartLoadWithRequestProp,
|
|
65
|
+
validateMeta,
|
|
66
|
+
validateData,
|
|
67
|
+
...otherProps
|
|
68
|
+
},
|
|
69
|
+
ref
|
|
70
|
+
) => {
|
|
71
|
+
const webViewRef = useRef<React.ComponentRef<
|
|
72
|
+
HostComponent<NativeProps>
|
|
73
|
+
> | null>(null);
|
|
74
|
+
|
|
75
|
+
const onShouldStartLoadWithRequestCallback = useCallback(
|
|
76
|
+
(shouldStart: boolean, _url: string, lockIdentifier = 0) => {
|
|
77
|
+
RNCWebViewModule.shouldStartLoadWithLockIdentifier(
|
|
78
|
+
!!shouldStart,
|
|
79
|
+
lockIdentifier
|
|
80
|
+
);
|
|
81
|
+
},
|
|
82
|
+
[]
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const {
|
|
86
|
+
onLoadingStart,
|
|
87
|
+
onShouldStartLoadWithRequest,
|
|
88
|
+
onMessage,
|
|
89
|
+
viewState,
|
|
90
|
+
setViewState,
|
|
91
|
+
lastErrorEvent,
|
|
92
|
+
onHttpError,
|
|
93
|
+
onLoadingError,
|
|
94
|
+
onLoadingFinish,
|
|
95
|
+
onLoadingProgress,
|
|
96
|
+
onContentProcessDidTerminate,
|
|
97
|
+
} = useWebViewLogic({
|
|
98
|
+
onNavigationStateChange,
|
|
99
|
+
onLoad,
|
|
100
|
+
onError,
|
|
101
|
+
onHttpErrorProp,
|
|
102
|
+
onLoadEnd,
|
|
103
|
+
onLoadProgress,
|
|
104
|
+
onLoadStart,
|
|
105
|
+
onMessageProp,
|
|
106
|
+
startInLoadingState,
|
|
107
|
+
originWhitelist,
|
|
108
|
+
deeplinkWhitelist,
|
|
109
|
+
onShouldStartLoadWithRequestProp,
|
|
110
|
+
onShouldStartLoadWithRequestCallback,
|
|
111
|
+
validateMeta,
|
|
112
|
+
validateData,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
useImperativeHandle(
|
|
116
|
+
ref,
|
|
117
|
+
() => ({
|
|
118
|
+
goForward: () =>
|
|
119
|
+
webViewRef.current && Commands.goForward(webViewRef.current),
|
|
120
|
+
goBack: () => webViewRef.current && Commands.goBack(webViewRef.current),
|
|
121
|
+
reload: () => {
|
|
122
|
+
setViewState('LOADING');
|
|
123
|
+
if (webViewRef.current) {
|
|
124
|
+
Commands.reload(webViewRef.current);
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
stopLoading: () =>
|
|
128
|
+
webViewRef.current && Commands.stopLoading(webViewRef.current),
|
|
129
|
+
postMessage: (data: string) =>
|
|
130
|
+
webViewRef.current && Commands.postMessage(webViewRef.current, data),
|
|
131
|
+
injectJavaScript: (data: string) =>
|
|
132
|
+
webViewRef.current &&
|
|
133
|
+
Commands.injectJavaScript(webViewRef.current, data),
|
|
134
|
+
requestFocus: () =>
|
|
135
|
+
webViewRef.current && Commands.requestFocus(webViewRef.current),
|
|
136
|
+
}),
|
|
137
|
+
[setViewState, webViewRef]
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
useWarnIfChanges(allowsInlineMediaPlayback, 'allowsInlineMediaPlayback');
|
|
141
|
+
useWarnIfChanges(
|
|
142
|
+
allowsPictureInPictureMediaPlayback,
|
|
143
|
+
'allowsPictureInPictureMediaPlayback'
|
|
144
|
+
);
|
|
145
|
+
useWarnIfChanges(
|
|
146
|
+
allowsAirPlayForMediaPlayback,
|
|
147
|
+
'allowsAirPlayForMediaPlayback'
|
|
148
|
+
);
|
|
149
|
+
useWarnIfChanges(incognito, 'incognito');
|
|
150
|
+
useWarnIfChanges(
|
|
151
|
+
mediaPlaybackRequiresUserAction,
|
|
152
|
+
'mediaPlaybackRequiresUserAction'
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
let otherView = null;
|
|
156
|
+
if (viewState === 'LOADING') {
|
|
157
|
+
otherView = (renderLoading || defaultRenderLoading)();
|
|
158
|
+
} else if (viewState === 'ERROR') {
|
|
159
|
+
invariant(
|
|
160
|
+
lastErrorEvent != null,
|
|
161
|
+
'lastErrorEvent expected to be non-null'
|
|
162
|
+
);
|
|
163
|
+
otherView = (renderError || defaultRenderError)(
|
|
164
|
+
lastErrorEvent?.domain,
|
|
165
|
+
lastErrorEvent?.code || 0,
|
|
166
|
+
lastErrorEvent?.description ?? ''
|
|
167
|
+
);
|
|
168
|
+
} else if (viewState !== 'IDLE') {
|
|
169
|
+
console.error(`RNCWebView invalid state encountered: ${viewState}`);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const webViewStyles = [styles.container, styles.webView, style];
|
|
173
|
+
const webViewContainerStyle = [styles.container, containerStyle];
|
|
174
|
+
|
|
175
|
+
const NativeWebView =
|
|
176
|
+
(nativeConfig?.component as typeof RNCWebView | undefined) || RNCWebView;
|
|
177
|
+
|
|
178
|
+
const sourceResolved = resolveAssetSource(source as ImageSourcePropType);
|
|
179
|
+
const newSource =
|
|
180
|
+
typeof sourceResolved === 'object'
|
|
181
|
+
? Object.entries(sourceResolved as WebViewSourceUri).reduce(
|
|
182
|
+
(prev, [currKey, currValue]) => {
|
|
183
|
+
return {
|
|
184
|
+
...prev,
|
|
185
|
+
[currKey]:
|
|
186
|
+
currKey === 'headers' &&
|
|
187
|
+
currValue &&
|
|
188
|
+
typeof currValue === 'object'
|
|
189
|
+
? Object.entries(currValue).map(([key, value]) => {
|
|
190
|
+
return {
|
|
191
|
+
name: key,
|
|
192
|
+
value,
|
|
193
|
+
};
|
|
194
|
+
})
|
|
195
|
+
: currValue,
|
|
196
|
+
};
|
|
197
|
+
},
|
|
198
|
+
{}
|
|
199
|
+
)
|
|
200
|
+
: sourceResolved;
|
|
201
|
+
|
|
202
|
+
const webView = (
|
|
203
|
+
<NativeWebView
|
|
204
|
+
key="webViewKey"
|
|
205
|
+
{...otherProps}
|
|
206
|
+
javaScriptEnabled={javaScriptEnabled}
|
|
207
|
+
cacheEnabled={cacheEnabled}
|
|
208
|
+
useSharedProcessPool={useSharedProcessPool}
|
|
209
|
+
messagingEnabled={typeof onMessageProp === 'function'}
|
|
210
|
+
newSource={newSource}
|
|
211
|
+
onLoadingError={onLoadingError}
|
|
212
|
+
onLoadingFinish={onLoadingFinish}
|
|
213
|
+
onLoadingProgress={onLoadingProgress}
|
|
214
|
+
onLoadingStart={onLoadingStart}
|
|
215
|
+
onHttpError={onHttpError}
|
|
216
|
+
onMessage={onMessage}
|
|
217
|
+
onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
|
|
218
|
+
onContentProcessDidTerminate={onContentProcessDidTerminate}
|
|
219
|
+
injectedJavaScript={injectedJavaScript}
|
|
220
|
+
injectedJavaScriptBeforeContentLoaded={
|
|
221
|
+
injectedJavaScriptBeforeContentLoaded
|
|
222
|
+
}
|
|
223
|
+
allowsAirPlayForMediaPlayback={allowsAirPlayForMediaPlayback}
|
|
224
|
+
allowsInlineMediaPlayback={allowsInlineMediaPlayback}
|
|
225
|
+
allowsPictureInPictureMediaPlayback={
|
|
226
|
+
allowsPictureInPictureMediaPlayback
|
|
227
|
+
}
|
|
228
|
+
incognito={incognito}
|
|
229
|
+
mediaPlaybackRequiresUserAction={mediaPlaybackRequiresUserAction}
|
|
230
|
+
ref={webViewRef}
|
|
231
|
+
// @ts-expect-error old arch only
|
|
232
|
+
source={sourceResolved}
|
|
233
|
+
style={webViewStyles}
|
|
234
|
+
{...nativeConfig?.props}
|
|
235
|
+
/>
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
return (
|
|
239
|
+
<View style={webViewContainerStyle}>
|
|
240
|
+
{webView}
|
|
241
|
+
{otherView}
|
|
242
|
+
</View>
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
);
|
|
246
|
+
|
|
247
|
+
// no native implementation for macOS, depends only on permissions
|
|
248
|
+
const isFileUploadSupported: () => Promise<boolean> = async () => true;
|
|
249
|
+
|
|
250
|
+
const WebView = Object.assign(WebViewComponent, { isFileUploadSupported });
|
|
251
|
+
|
|
252
|
+
export default WebView;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { StyleSheet } from 'react-native';
|
|
2
|
+
|
|
3
|
+
const styles = StyleSheet.create({
|
|
4
|
+
container: {
|
|
5
|
+
flex: 1,
|
|
6
|
+
overflow: 'hidden',
|
|
7
|
+
},
|
|
8
|
+
loadingOrErrorView: {
|
|
9
|
+
position: 'absolute',
|
|
10
|
+
flex: 1,
|
|
11
|
+
justifyContent: 'center',
|
|
12
|
+
alignItems: 'center',
|
|
13
|
+
height: '100%',
|
|
14
|
+
width: '100%',
|
|
15
|
+
backgroundColor: 'white',
|
|
16
|
+
},
|
|
17
|
+
loadingProgressBar: {
|
|
18
|
+
height: 20,
|
|
19
|
+
},
|
|
20
|
+
errorText: {
|
|
21
|
+
fontSize: 14,
|
|
22
|
+
textAlign: 'center',
|
|
23
|
+
marginBottom: 2,
|
|
24
|
+
},
|
|
25
|
+
errorTextTitle: {
|
|
26
|
+
fontSize: 15,
|
|
27
|
+
fontWeight: '500',
|
|
28
|
+
marginBottom: 10,
|
|
29
|
+
},
|
|
30
|
+
webView: {
|
|
31
|
+
backgroundColor: '#ffffff',
|
|
32
|
+
},
|
|
33
|
+
flexStart: {
|
|
34
|
+
alignSelf: 'flex-start',
|
|
35
|
+
},
|
|
36
|
+
colorRed: {
|
|
37
|
+
color: 'red',
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
export default styles;
|
package/src/WebView.tsx
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { Text, View } from 'react-native';
|
|
3
|
+
import {
|
|
4
|
+
IOSWebViewProps,
|
|
5
|
+
AndroidWebViewProps,
|
|
6
|
+
WindowsWebViewProps,
|
|
7
|
+
} from './WebViewTypes';
|
|
8
|
+
import styles from './WebView.styles';
|
|
9
|
+
|
|
10
|
+
export type WebViewProps = IOSWebViewProps &
|
|
11
|
+
AndroidWebViewProps &
|
|
12
|
+
WindowsWebViewProps;
|
|
13
|
+
|
|
14
|
+
// This "dummy" WebView is to render something for unsupported platforms,
|
|
15
|
+
// like for example Expo SDK "web" platform.
|
|
16
|
+
const WebView: React.FunctionComponent<WebViewProps> = () => (
|
|
17
|
+
<View style={styles.flexStart}>
|
|
18
|
+
<Text style={styles.colorRed}>
|
|
19
|
+
React Native WebView does not support this platform.
|
|
20
|
+
</Text>
|
|
21
|
+
</View>
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
export { WebView };
|
|
25
|
+
export default WebView;
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
* Portions copyright for react-native-windows:
|
|
8
|
+
*
|
|
9
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
10
|
+
* Licensed under the MIT License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import React, {
|
|
14
|
+
forwardRef,
|
|
15
|
+
useCallback,
|
|
16
|
+
useImperativeHandle,
|
|
17
|
+
useRef,
|
|
18
|
+
} from 'react';
|
|
19
|
+
import { View, Image, ImageSourcePropType, NativeModules } from 'react-native';
|
|
20
|
+
import codegenNativeCommands from 'react-native/Libraries/Utilities/codegenNativeCommands';
|
|
21
|
+
import invariant from 'invariant';
|
|
22
|
+
import { RCTWebView, RCTWebView2 } from './WebViewNativeComponent.windows';
|
|
23
|
+
import {
|
|
24
|
+
useWebViewLogic,
|
|
25
|
+
defaultOriginWhitelist,
|
|
26
|
+
defaultDeeplinkWhitelist,
|
|
27
|
+
defaultRenderError,
|
|
28
|
+
defaultRenderLoading,
|
|
29
|
+
} from './WebViewShared';
|
|
30
|
+
import { NativeWebViewWindows, WindowsWebViewProps } from './WebViewTypes';
|
|
31
|
+
|
|
32
|
+
import styles from './WebView.styles';
|
|
33
|
+
|
|
34
|
+
const Commands = codegenNativeCommands({
|
|
35
|
+
supportedCommands: [
|
|
36
|
+
'goBack',
|
|
37
|
+
'goForward',
|
|
38
|
+
'reload',
|
|
39
|
+
'stopLoading',
|
|
40
|
+
'injectJavaScript',
|
|
41
|
+
'requestFocus',
|
|
42
|
+
'clearCache',
|
|
43
|
+
'postMessage',
|
|
44
|
+
'loadUrl',
|
|
45
|
+
],
|
|
46
|
+
});
|
|
47
|
+
const { resolveAssetSource } = Image;
|
|
48
|
+
|
|
49
|
+
const WebViewComponent = forwardRef<{}, WindowsWebViewProps>(
|
|
50
|
+
(
|
|
51
|
+
{
|
|
52
|
+
cacheEnabled = true,
|
|
53
|
+
originWhitelist = defaultOriginWhitelist,
|
|
54
|
+
deeplinkWhitelist = defaultDeeplinkWhitelist,
|
|
55
|
+
startInLoadingState,
|
|
56
|
+
onNavigationStateChange,
|
|
57
|
+
onLoadStart,
|
|
58
|
+
onError,
|
|
59
|
+
onLoad,
|
|
60
|
+
onLoadEnd,
|
|
61
|
+
onLoadProgress,
|
|
62
|
+
onOpenWindow: onOpenWindowProp,
|
|
63
|
+
onSourceChanged,
|
|
64
|
+
onHttpError: onHttpErrorProp,
|
|
65
|
+
onMessage: onMessageProp,
|
|
66
|
+
renderLoading,
|
|
67
|
+
renderError,
|
|
68
|
+
style,
|
|
69
|
+
containerStyle,
|
|
70
|
+
source,
|
|
71
|
+
nativeConfig,
|
|
72
|
+
onShouldStartLoadWithRequest: onShouldStartLoadWithRequestProp,
|
|
73
|
+
useWebView2,
|
|
74
|
+
validateMeta,
|
|
75
|
+
validateData,
|
|
76
|
+
...otherProps
|
|
77
|
+
},
|
|
78
|
+
ref
|
|
79
|
+
) => {
|
|
80
|
+
const webViewRef = useRef<NativeWebViewWindows | null>(null);
|
|
81
|
+
|
|
82
|
+
const RCTWebViewString = useWebView2 ? 'RCTWebView2' : 'RCTWebView';
|
|
83
|
+
|
|
84
|
+
const onShouldStartLoadWithRequestCallback = useCallback(
|
|
85
|
+
(shouldStart: boolean, url: string, lockIdentifier?: number) => {
|
|
86
|
+
if (lockIdentifier) {
|
|
87
|
+
if (RCTWebViewString === 'RCTWebView') {
|
|
88
|
+
NativeModules.RCTWebView.onShouldStartLoadWithRequestCallback(
|
|
89
|
+
shouldStart,
|
|
90
|
+
lockIdentifier
|
|
91
|
+
);
|
|
92
|
+
} else {
|
|
93
|
+
NativeModules.RCTWebView2.onShouldStartLoadWithRequestCallback(
|
|
94
|
+
shouldStart,
|
|
95
|
+
lockIdentifier
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
} else if (shouldStart) {
|
|
99
|
+
Commands.loadUrl(webViewRef, url);
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
[RCTWebViewString]
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
const {
|
|
106
|
+
onLoadingStart,
|
|
107
|
+
onShouldStartLoadWithRequest,
|
|
108
|
+
onMessage,
|
|
109
|
+
viewState,
|
|
110
|
+
setViewState,
|
|
111
|
+
lastErrorEvent,
|
|
112
|
+
onHttpError,
|
|
113
|
+
onLoadingError,
|
|
114
|
+
onLoadingFinish,
|
|
115
|
+
onLoadingProgress,
|
|
116
|
+
onOpenWindow,
|
|
117
|
+
} = useWebViewLogic({
|
|
118
|
+
onNavigationStateChange,
|
|
119
|
+
onLoad,
|
|
120
|
+
onError,
|
|
121
|
+
onHttpErrorProp,
|
|
122
|
+
onLoadEnd,
|
|
123
|
+
onLoadProgress,
|
|
124
|
+
onLoadStart,
|
|
125
|
+
onMessageProp,
|
|
126
|
+
startInLoadingState,
|
|
127
|
+
originWhitelist,
|
|
128
|
+
deeplinkWhitelist,
|
|
129
|
+
onShouldStartLoadWithRequestProp,
|
|
130
|
+
onShouldStartLoadWithRequestCallback,
|
|
131
|
+
onOpenWindowProp,
|
|
132
|
+
validateMeta,
|
|
133
|
+
validateData,
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
useImperativeHandle(
|
|
137
|
+
ref,
|
|
138
|
+
() => ({
|
|
139
|
+
goForward: () => Commands.goForward(webViewRef.current),
|
|
140
|
+
goBack: () => Commands.goBack(webViewRef.current),
|
|
141
|
+
reload: () => {
|
|
142
|
+
setViewState('LOADING');
|
|
143
|
+
Commands.reload(webViewRef.current);
|
|
144
|
+
},
|
|
145
|
+
stopLoading: () => Commands.stopLoading(webViewRef.current),
|
|
146
|
+
postMessage: (data: string) =>
|
|
147
|
+
Commands.postMessage(webViewRef.current, data),
|
|
148
|
+
injectJavaScript: (data: string) =>
|
|
149
|
+
Commands.injectJavaScript(webViewRef.current, data),
|
|
150
|
+
requestFocus: () => Commands.requestFocus(webViewRef.current),
|
|
151
|
+
clearCache: () => Commands.clearCache(webViewRef.current),
|
|
152
|
+
loadUrl: (url: string) => Commands.loadUrl(webViewRef.current, url),
|
|
153
|
+
}),
|
|
154
|
+
[setViewState, webViewRef]
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
let otherView = null;
|
|
158
|
+
if (viewState === 'LOADING') {
|
|
159
|
+
otherView = (renderLoading || defaultRenderLoading)();
|
|
160
|
+
} else if (viewState === 'ERROR') {
|
|
161
|
+
invariant(
|
|
162
|
+
lastErrorEvent != null,
|
|
163
|
+
'lastErrorEvent expected to be non-null'
|
|
164
|
+
);
|
|
165
|
+
otherView = (renderError || defaultRenderError)(
|
|
166
|
+
lastErrorEvent.domain,
|
|
167
|
+
lastErrorEvent.code,
|
|
168
|
+
lastErrorEvent.description
|
|
169
|
+
);
|
|
170
|
+
} else if (viewState !== 'IDLE') {
|
|
171
|
+
console.error(`RNCWebView invalid state encountered: ${viewState}`);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const webViewStyles = [styles.container, styles.webView, style];
|
|
175
|
+
const webViewContainerStyle = [styles.container, containerStyle];
|
|
176
|
+
|
|
177
|
+
const NativeWebView = useWebView2 ? RCTWebView2 : RCTWebView;
|
|
178
|
+
|
|
179
|
+
const webView = (
|
|
180
|
+
<NativeWebView
|
|
181
|
+
key="webViewKey"
|
|
182
|
+
{...otherProps}
|
|
183
|
+
messagingEnabled={typeof onMessageProp === 'function'}
|
|
184
|
+
linkHandlingEnabled={typeof onOpenWindowProp === 'function'}
|
|
185
|
+
onLoadingError={onLoadingError}
|
|
186
|
+
onLoadingFinish={onLoadingFinish}
|
|
187
|
+
onLoadingProgress={onLoadingProgress}
|
|
188
|
+
onLoadingStart={onLoadingStart}
|
|
189
|
+
onHttpError={onHttpError}
|
|
190
|
+
onMessage={onMessage}
|
|
191
|
+
onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
|
|
192
|
+
onOpenWindow={onOpenWindow}
|
|
193
|
+
onSourceChanged={onSourceChanged}
|
|
194
|
+
ref={webViewRef}
|
|
195
|
+
// TODO: find a better way to type this.
|
|
196
|
+
source={resolveAssetSource(source as ImageSourcePropType)}
|
|
197
|
+
style={webViewStyles}
|
|
198
|
+
cacheEnabled={cacheEnabled}
|
|
199
|
+
{...nativeConfig?.props}
|
|
200
|
+
/>
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
return (
|
|
204
|
+
<View style={webViewContainerStyle}>
|
|
205
|
+
{webView}
|
|
206
|
+
{otherView}
|
|
207
|
+
</View>
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
// native implementation should return "true" only for Android 5+
|
|
213
|
+
const isFileUploadSupported: () => Promise<boolean> = async () => false;
|
|
214
|
+
|
|
215
|
+
const WebView = Object.assign(WebViewComponent, { isFileUploadSupported });
|
|
216
|
+
|
|
217
|
+
export default WebView;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { requireNativeComponent } from 'react-native';
|
|
2
|
+
import type { NativeWebViewWindows } from './WebViewTypes';
|
|
3
|
+
|
|
4
|
+
export const RCTWebView: typeof NativeWebViewWindows =
|
|
5
|
+
requireNativeComponent('RCTWebView');
|
|
6
|
+
|
|
7
|
+
export const RCTWebView2: typeof NativeWebViewWindows =
|
|
8
|
+
requireNativeComponent('RCTWebView2');
|