Browse Source

Merge branch 'master' into feat-removeScript

sunzhongliang 4 years ago
parent
commit
c8e8bb3eb6
No account linked to committer's email address

+ 39
- 2
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java View File

41
 import com.facebook.react.views.scroll.ScrollEventType;
41
 import com.facebook.react.views.scroll.ScrollEventType;
42
 import com.facebook.react.views.scroll.OnScrollDispatchHelper;
42
 import com.facebook.react.views.scroll.OnScrollDispatchHelper;
43
 import com.facebook.react.bridge.Arguments;
43
 import com.facebook.react.bridge.Arguments;
44
+import com.facebook.react.bridge.CatalystInstance;
44
 import com.facebook.react.bridge.LifecycleEventListener;
45
 import com.facebook.react.bridge.LifecycleEventListener;
45
 import com.facebook.react.bridge.ReactContext;
46
 import com.facebook.react.bridge.ReactContext;
46
 import com.facebook.react.bridge.ReadableArray;
47
 import com.facebook.react.bridge.ReadableArray;
47
 import com.facebook.react.bridge.ReadableMap;
48
 import com.facebook.react.bridge.ReadableMap;
48
 import com.facebook.react.bridge.ReadableMapKeySetIterator;
49
 import com.facebook.react.bridge.ReadableMapKeySetIterator;
49
 import com.facebook.react.bridge.WritableMap;
50
 import com.facebook.react.bridge.WritableMap;
51
+import com.facebook.react.bridge.WritableNativeArray;
52
+import com.facebook.react.bridge.WritableNativeMap;
50
 import com.facebook.react.common.MapBuilder;
53
 import com.facebook.react.common.MapBuilder;
51
 import com.facebook.react.common.build.ReactBuildConfig;
54
 import com.facebook.react.common.build.ReactBuildConfig;
52
 import com.facebook.react.module.annotations.ReactModule;
55
 import com.facebook.react.module.annotations.ReactModule;
973
     protected boolean messagingEnabled = false;
976
     protected boolean messagingEnabled = false;
974
     protected @Nullable
977
     protected @Nullable
975
     RNCWebViewClient mRNCWebViewClient;
978
     RNCWebViewClient mRNCWebViewClient;
979
+    protected @Nullable
980
+    CatalystInstance mCatalystInstance;
976
     protected boolean sendContentSizeChangeEvents = false;
981
     protected boolean sendContentSizeChangeEvents = false;
977
     private OnScrollDispatchHelper mOnScrollDispatchHelper;
982
     private OnScrollDispatchHelper mOnScrollDispatchHelper;
978
     protected boolean hasScrollEvent = false;
983
     protected boolean hasScrollEvent = false;
1047
       return new RNCWebViewBridge(webView);
1052
       return new RNCWebViewBridge(webView);
1048
     }
1053
     }
1049
 
1054
 
1055
+    protected void createCatalystInstance() {
1056
+      ReactContext reactContext = (ReactContext) this.getContext();
1057
+
1058
+      if (reactContext != null) {
1059
+        mCatalystInstance = reactContext.getCatalystInstance();
1060
+      }
1061
+    }
1062
+
1050
     @SuppressLint("AddJavascriptInterface")
1063
     @SuppressLint("AddJavascriptInterface")
1051
     public void setMessagingEnabled(boolean enabled) {
1064
     public void setMessagingEnabled(boolean enabled) {
1052
       if (messagingEnabled == enabled) {
1065
       if (messagingEnabled == enabled) {
1057
 
1070
 
1058
       if (enabled) {
1071
       if (enabled) {
1059
         addJavascriptInterface(createRNCWebViewBridge(this), JAVASCRIPT_INTERFACE);
1072
         addJavascriptInterface(createRNCWebViewBridge(this), JAVASCRIPT_INTERFACE);
1073
+        this.createCatalystInstance();
1060
       } else {
1074
       } else {
1061
         removeJavascriptInterface(JAVASCRIPT_INTERFACE);
1075
         removeJavascriptInterface(JAVASCRIPT_INTERFACE);
1062
       }
1076
       }
1085
     }
1099
     }
1086
 
1100
 
1087
     public void onMessage(String message) {
1101
     public void onMessage(String message) {
1102
+      ReactContext reactContext = (ReactContext) this.getContext();
1103
+      RNCWebView mContext = this;
1104
+
1088
       if (mRNCWebViewClient != null) {
1105
       if (mRNCWebViewClient != null) {
1089
         WebView webView = this;
1106
         WebView webView = this;
1090
         webView.post(new Runnable() {
1107
         webView.post(new Runnable() {
1095
             }
1112
             }
1096
             WritableMap data = mRNCWebViewClient.createWebViewEvent(webView, webView.getUrl());
1113
             WritableMap data = mRNCWebViewClient.createWebViewEvent(webView, webView.getUrl());
1097
             data.putString("data", message);
1114
             data.putString("data", message);
1098
-            dispatchEvent(webView, new TopMessageEvent(webView.getId(), data));
1115
+
1116
+            if (mCatalystInstance != null) {
1117
+              mContext.sendDirectMessage(data);
1118
+            } else {
1119
+              dispatchEvent(webView, new TopMessageEvent(webView.getId(), data));
1120
+            }
1099
           }
1121
           }
1100
         });
1122
         });
1101
       } else {
1123
       } else {
1102
         WritableMap eventData = Arguments.createMap();
1124
         WritableMap eventData = Arguments.createMap();
1103
         eventData.putString("data", message);
1125
         eventData.putString("data", message);
1104
-        dispatchEvent(this, new TopMessageEvent(this.getId(), eventData));
1126
+
1127
+        if (mCatalystInstance != null) {
1128
+          this.sendDirectMessage(eventData);
1129
+        } else {
1130
+          dispatchEvent(this, new TopMessageEvent(this.getId(), eventData));
1131
+        }
1105
       }
1132
       }
1106
     }
1133
     }
1107
 
1134
 
1135
+    protected void sendDirectMessage(WritableMap data) {
1136
+      WritableNativeMap event = new WritableNativeMap();
1137
+      event.putMap("nativeEvent", data);
1138
+
1139
+      WritableNativeArray params = new WritableNativeArray();
1140
+      params.pushMap(event);
1141
+
1142
+      mCatalystInstance.callFunction("WebViewMessageHandler", "onMessage", params);
1143
+    }
1144
+
1108
     protected void onScrollChanged(int x, int y, int oldX, int oldY) {
1145
     protected void onScrollChanged(int x, int y, int oldX, int oldY) {
1109
       super.onScrollChanged(x, y, oldX, oldY);
1146
       super.onScrollChanged(x, y, oldX, oldY);
1110
 
1147
 

+ 4
- 4
ios/RNCWebView.m View File

1244
                                                                        name:MessageHandlerName];
1244
                                                                        name:MessageHandlerName];
1245
       [wkWebViewConfig.userContentController addUserScript:self.postMessageScript];
1245
       [wkWebViewConfig.userContentController addUserScript:self.postMessageScript];
1246
     }
1246
     }
1247
-    // FIXME: For a separate (minor) PR: these two shouldn't be gated by messagingEnabled; just keeping consistency with previous behaviour.
1248
-    if (self.atStartScript) {
1249
-      [wkWebViewConfig.userContentController addUserScript:self.atStartScript];
1250
-    }
1251
     if (self.atEndScript) {
1247
     if (self.atEndScript) {
1252
       [wkWebViewConfig.userContentController addUserScript:self.atEndScript];
1248
       [wkWebViewConfig.userContentController addUserScript:self.atEndScript];
1253
     }
1249
     }
1254
   }
1250
   }
1251
+  // Whether or not messaging is enabled, add the startup script if it exists.
1252
+  if (self.atStartScript) {
1253
+    [wkWebViewConfig.userContentController addUserScript:self.atStartScript];
1254
+  }
1255
 }
1255
 }
1256
 
1256
 
1257
 - (NSURLRequest *)requestForSource:(id)json {
1257
 - (NSURLRequest *)requestForSource:(id)json {

+ 1
- 1
package.json View File

8
     "Thibault Malbranche <malbranche.thibault@gmail.com>"
8
     "Thibault Malbranche <malbranche.thibault@gmail.com>"
9
   ],
9
   ],
10
   "license": "MIT",
10
   "license": "MIT",
11
-  "version": "9.0.1",
11
+  "version": "9.1.1",
12
   "homepage": "https://github.com/react-native-community/react-native-webview#readme",
12
   "homepage": "https://github.com/react-native-community/react-native-webview#readme",
13
   "scripts": {
13
   "scripts": {
14
     "start": "node node_modules/react-native/local-cli/cli.js start",
14
     "start": "node node_modules/react-native/local-cli/cli.js start",

+ 6
- 0
src/WebView.android.tsx View File

10
   findNodeHandle,
10
   findNodeHandle,
11
 } from 'react-native';
11
 } from 'react-native';
12
 
12
 
13
+import BatchedBridge from 'react-native/Libraries/BatchedBridge/BatchedBridge';
14
+
13
 import invariant from 'invariant';
15
 import invariant from 'invariant';
14
 
16
 
15
 import {
17
 import {
70
 
72
 
71
   webViewRef = React.createRef<NativeWebViewAndroid>();
73
   webViewRef = React.createRef<NativeWebViewAndroid>();
72
 
74
 
75
+  componentDidMount = () => {
76
+    BatchedBridge.registerCallableModule('WebViewMessageHandler', this);
77
+  }
78
+
73
   getCommands = () => UIManager.getViewManagerConfig('RNCWebView').Commands;
79
   getCommands = () => UIManager.getViewManagerConfig('RNCWebView').Commands;
74
 
80
 
75
   goForward = () => {
81
   goForward = () => {

+ 5
- 5
src/WebViewTypes.ts View File

272
   saveFormDataDisabled?: boolean;
272
   saveFormDataDisabled?: boolean;
273
   textZoom?: number;
273
   textZoom?: number;
274
   thirdPartyCookiesEnabled?: boolean;
274
   thirdPartyCookiesEnabled?: boolean;
275
-  urlPrefixesForDefaultIntent?: readonly string[];
275
+  readonly urlPrefixesForDefaultIntent?: string[];
276
 }
276
 }
277
 
277
 
278
 export enum ContentInsetAdjustmentBehavior {
278
 export enum ContentInsetAdjustmentBehavior {
291
   bounces?: boolean;
291
   bounces?: boolean;
292
   contentInset?: ContentInsetProp;
292
   contentInset?: ContentInsetProp;
293
   contentInsetAdjustmentBehavior?: ContentInsetAdjustmentBehavior;
293
   contentInsetAdjustmentBehavior?: ContentInsetAdjustmentBehavior;
294
-  dataDetectorTypes?: DataDetectorTypes | readonly DataDetectorTypes[];
294
+  readonly dataDetectorTypes?: DataDetectorTypes | DataDetectorTypes[];
295
   decelerationRate?: number;
295
   decelerationRate?: number;
296
   directionalLockEnabled?: boolean;
296
   directionalLockEnabled?: boolean;
297
   hideKeyboardAccessoryView?: boolean;
297
   hideKeyboardAccessoryView?: boolean;
409
    *
409
    *
410
    * @platform ios
410
    * @platform ios
411
    */
411
    */
412
-  dataDetectorTypes?: DataDetectorTypes | readonly DataDetectorTypes[];
412
+  readonly dataDetectorTypes?: DataDetectorTypes | DataDetectorTypes[];
413
 
413
 
414
   /**
414
   /**
415
    * Boolean that determines whether HTML5 videos play inline or use the
415
    * Boolean that determines whether HTML5 videos play inline or use the
728
    * Use this to list URLs that WebView cannot handle, e.g. a PDF url.
728
    * Use this to list URLs that WebView cannot handle, e.g. a PDF url.
729
    * @platform android
729
    * @platform android
730
    */
730
    */
731
-  urlPrefixesForDefaultIntent?: readonly string[];
731
+  readonly urlPrefixesForDefaultIntent?: string[];
732
 
732
 
733
   /**
733
   /**
734
    * Boolean value to disable Hardware Acceleration in the `WebView`. Used on Android only
734
    * Boolean value to disable Hardware Acceleration in the `WebView`. Used on Android only
907
    * this whitelist, we will open the URL in Safari.
907
    * this whitelist, we will open the URL in Safari.
908
    * The default whitelisted origins are "http://*" and "https://*".
908
    * The default whitelisted origins are "http://*" and "https://*".
909
    */
909
    */
910
-  originWhitelist?: readonly string[];
910
+  readonly originWhitelist?: string[];
911
 
911
 
912
   /**
912
   /**
913
    * Function that allows custom handling of any web view requests. Return
913
    * Function that allows custom handling of any web view requests. Return