Browse Source

Merge pull request #83 from Holdsport/master

  Robustness fix for clients returning weird stuff in the message data; Only insert wrapper element once; be able to use the component in a Typescript file;
iou90 6 years ago
parent
commit
418f35174a
No account linked to committer's email address
4 changed files with 25 additions and 15 deletions
  1. 4
    0
      autoHeightWebView/index.android.js
  2. 17
    15
      autoHeightWebView/index.ios.js
  3. 3
    0
      index.js
  4. 1
    0
      package.json

+ 4
- 0
autoHeightWebView/index.android.js View File

217
     if (!e.nativeEvent) {
217
     if (!e.nativeEvent) {
218
       return;
218
       return;
219
     }
219
     }
220
+    
221
+    // Sometimes the message is invalid JSON, so we ignore that case
222
+    try { JSON.parse(isBelowKitKat ? e.nativeEvent.message : e.nativeEvent.data) } catch(e) { return }
223
+
220
     const { height, width } = JSON.parse(isBelowKitKat ? e.nativeEvent.message : e.nativeEvent.data);
224
     const { height, width } = JSON.parse(isBelowKitKat ? e.nativeEvent.message : e.nativeEvent.data);
221
     const { height: oldHeight, width: oldWidth } = this.state;
225
     const { height: oldHeight, width: oldWidth } = this.state;
222
     if (isSizeChanged(height, oldHeight, width, oldWidth)) {
226
     if (isSizeChanged(height, oldHeight, width, oldWidth)) {

+ 17
- 15
autoHeightWebView/index.ios.js View File

205
     ;
205
     ;
206
     ${getCurrentSize}
206
     ${getCurrentSize}
207
     (function () {
207
     (function () {
208
-        var height = 0;
209
-        var width = ${getWidth(style)};
210
-        var wrapper = document.createElement('div');
211
-        wrapper.id = 'rnahw-wrapper';
212
-        while (document.body.firstChild instanceof Node) {
213
-            wrapper.appendChild(document.body.firstChild);
214
-        }
215
-        document.body.appendChild(wrapper);
216
-        function updateSize() {
217
-            if(document.body.offsetHeight !== height || document.body.offsetWidth !== width) {
218
-                var size = getSize(wrapper);
219
-                height = size.height;
220
-                width = size.width;
221
-                document.title = height.toString() + ',' + width.toString();
208
+      if (!document.getElementById("rnahw-wrapper")) { 
209
+          var height = 0;
210
+          var width = ${getWidth(style)};
211
+          var wrapper = document.createElement('div');
212
+          wrapper.id = 'rnahw-wrapper';
213
+          while (document.body.firstChild instanceof Node) {
214
+              wrapper.appendChild(document.body.firstChild);
215
+          }
216
+          document.body.appendChild(wrapper);
217
+          function updateSize() {
218
+            if (document.body.offsetHeight !== height || document.body.offsetWidth !== width) {
219
+               var size = getSize(wrapper);
220
+               height = size.height;
221
+               width = size.width;
222
+               document.title = height.toString() + ',' + width.toString();
222
             }
223
             }
223
-        }
224
+          }
224
         ${commonScript}
225
         ${commonScript}
225
         ${domMutationObserveScript}
226
         ${domMutationObserveScript}
227
+        }
226
     } ());
228
     } ());
227
     `;
229
     `;
228
 }
230
 }

+ 3
- 0
index.js View File

1
+var ReactNative = require('react-native')
2
+module.exports = ReactNative.NativeModules.AutoHeightWebView
3
+

+ 1
- 0
package.json View File

4
   "description": "An auto height webview for React Native",
4
   "description": "An auto height webview for React Native",
5
   "main": "autoHeightWebView",
5
   "main": "autoHeightWebView",
6
   "files": [
6
   "files": [
7
+    "index.js",
7
     "android",
8
     "android",
8
     "autoHeightWebView"
9
     "autoHeightWebView"
9
   ],
10
   ],