Bläddra i källkod

Merge branch 'master' of https://github.com/iou90/react-native-autoheight-webview

# Conflicts:
#	autoHeightWebView/index.ios.js
iou90 6 år sedan
förälder
incheckning
c6b9137af2
2 ändrade filer med 60 tillägg och 72 borttagningar
  1. 53
    72
      autoHeightWebView/index.android.js
  2. 7
    0
      autoHeightWebView/index.ios.js

+ 53
- 72
autoHeightWebView/index.android.js Visa fil

@@ -1,6 +1,6 @@
1
-"use strict";
1
+'use strict';
2 2
 
3
-import React, { PureComponent } from "react";
3
+import React, { PureComponent } from 'react';
4 4
 
5 5
 import {
6 6
   findNodeHandle,
@@ -14,26 +14,22 @@ import {
14 14
   View,
15 15
   ViewPropTypes,
16 16
   WebView
17
-} from "react-native";
17
+} from 'react-native';
18 18
 
19
-import PropTypes from "prop-types";
19
+import PropTypes from 'prop-types';
20 20
 
21
-import Immutable from "immutable";
21
+import Immutable from 'immutable';
22 22
 
23
-const RCTAutoHeightWebView = requireNativeComponent(
24
-  "RCTAutoHeightWebView",
25
-  AutoHeightWebView,
26
-  { nativeOnly:
27
-    {
28
-      nativeOnly: {
29
-        onLoadingStart: true,
30
-        onLoadingError: true,
31
-        onLoadingFinish: true,
32
-        messagingEnabled: PropTypes.bool
33
-      }
23
+const RCTAutoHeightWebView = requireNativeComponent('RCTAutoHeightWebView', AutoHeightWebView, {
24
+  nativeOnly: {
25
+    nativeOnly: {
26
+      onLoadingStart: true,
27
+      onLoadingError: true,
28
+      onLoadingFinish: true,
29
+      messagingEnabled: PropTypes.bool
34 30
     }
35
-   }
36
-);
31
+  }
32
+});
37 33
 
38 34
 export default class AutoHeightWebView extends PureComponent {
39 35
   static propTypes = {
@@ -46,7 +42,7 @@ export default class AutoHeightWebView extends PureComponent {
46 42
     scalesPageToFit: PropTypes.bool,
47 43
     // only works on enable animation
48 44
     animationDuration: PropTypes.number,
49
-    // offset of rn webview margin
45
+    // offset of rn webView margin
50 46
     heightOffset: PropTypes.number,
51 47
     // baseUrl not work in android 4.3 or below version
52 48
     enableBaseUrl: PropTypes.bool,
@@ -81,16 +77,10 @@ export default class AutoHeightWebView extends PureComponent {
81 77
       this.opacityAnimatedValue = new Animated.Value(0);
82 78
     }
83 79
     if (IsBelowKitKat) {
84
-      this.listenWebViewBridgeMessage = this.listenWebViewBridgeMessage.bind(
85
-        this
86
-      );
80
+      this.listenWebViewBridgeMessage = this.listenWebViewBridgeMessage.bind(this);
87 81
     }
88
-    let initialScript = props.files
89
-      ? this.appendFilesToHead(props.files, BaseScript)
90
-      : BaseScript;
91
-    initialScript = props.customStyle
92
-      ? this.appendStylesToHead(props.customStyle, initialScript)
93
-      : initialScript;
82
+    let initialScript = props.files ? this.appendFilesToHead(props.files, BaseScript) : BaseScript;
83
+    initialScript = props.customStyle ? this.appendStylesToHead(props.customStyle, initialScript) : initialScript;
94 84
     this.state = {
95 85
       isChangingSource: false,
96 86
       height: 0,
@@ -101,10 +91,7 @@ export default class AutoHeightWebView extends PureComponent {
101 91
 
102 92
   componentWillMount() {
103 93
     if (IsBelowKitKat) {
104
-      DeviceEventEmitter.addListener(
105
-        "webViewBridgeMessage",
106
-        this.listenWebViewBridgeMessage
107
-      );
94
+      DeviceEventEmitter.addListener('webViewBridgeMessage', this.listenWebViewBridgeMessage);
108 95
     }
109 96
   }
110 97
 
@@ -113,13 +100,8 @@ export default class AutoHeightWebView extends PureComponent {
113 100
   }
114 101
 
115 102
   componentWillReceiveProps(nextProps) {
116
-    // injectedJavaScript only works when webview reload (source changed)
117
-    if (
118
-      Immutable.is(
119
-        Immutable.fromJS(this.props.source),
120
-        Immutable.fromJS(nextProps.source)
121
-      )
122
-    ) {
103
+    // injectedJavaScript only works when webView reload (source changed)
104
+    if (Immutable.is(Immutable.fromJS(this.props.source), Immutable.fromJS(nextProps.source))) {
123 105
       return;
124 106
     } else {
125 107
       this.setState(
@@ -147,10 +129,7 @@ export default class AutoHeightWebView extends PureComponent {
147 129
   componentWillUnmount() {
148 130
     this.stopInterval();
149 131
     if (IsBelowKitKat) {
150
-      DeviceEventEmitter.removeListener(
151
-        "webViewBridgeMessage",
152
-        this.listenWebViewBridgeMessage
153
-      );
132
+      DeviceEventEmitter.removeListener('webViewBridgeMessage', this.listenWebViewBridgeMessage);
154 133
     }
155 134
   }
156 135
 
@@ -162,7 +141,7 @@ export default class AutoHeightWebView extends PureComponent {
162 141
   // below kitkat
163 142
   sendToWebView(message) {
164 143
     UIManager.dispatchViewManagerCommand(
165
-      findNodeHandle(this.webview),
144
+      findNodeHandle(this.webView),
166 145
       UIManager.RCTAutoHeightWebView.Commands.sendToWebView,
167 146
       [String(message)]
168 147
     );
@@ -170,7 +149,7 @@ export default class AutoHeightWebView extends PureComponent {
170 149
 
171 150
   postMessage(data) {
172 151
     UIManager.dispatchViewManagerCommand(
173
-      findNodeHandle(this.webview),
152
+      findNodeHandle(this.webView),
174 153
       UIManager.RCTAutoHeightWebView.Commands.postMessage,
175 154
       [String(data)]
176 155
     );
@@ -180,9 +159,7 @@ export default class AutoHeightWebView extends PureComponent {
180 159
     this.finishInterval = false;
181 160
     this.interval = setInterval(() => {
182 161
       if (!this.finishInterval) {
183
-        IsBelowKitKat
184
-          ? this.sendToWebView("getBodyHeight")
185
-          : this.postMessage("getBodyHeight");
162
+        IsBelowKitKat ? this.sendToWebView('getBodyHeight') : this.postMessage('getBodyHeight');
186 163
       }
187 164
     }, 205);
188 165
   }
@@ -199,9 +176,7 @@ export default class AutoHeightWebView extends PureComponent {
199 176
   }
200 177
 
201 178
   onMessage(e) {
202
-    const height = parseInt(
203
-      IsBelowKitKat ? e.nativeEvent.message : e.nativeEvent.data
204
-    );
179
+    const height = parseInt(IsBelowKitKat ? e.nativeEvent.message : e.nativeEvent.data);
205 180
     if (height) {
206 181
       if (this.props.enableAnimation) {
207 182
         this.opacityAnimatedValue.setValue(0);
@@ -230,14 +205,17 @@ export default class AutoHeightWebView extends PureComponent {
230 205
     if (!files) {
231 206
       return script;
232 207
     }
233
-    return files.reduceRight((file, combinedScript) => `
208
+    return files.reduceRight(
209
+      (file, combinedScript) => `
234 210
       var link  = document.createElement('link');
235 211
       link.rel  = '${file.rel}';
236 212
       link.type = '${file.type}';
237 213
       link.href = '${file.href}';
238 214
       document.head.appendChild(link);
239 215
       ${combinedScript}
240
-    `, script);
216
+    `,
217
+      script
218
+    );
241 219
   }
242 220
 
243 221
   appendStylesToHead(styles, script) {
@@ -245,7 +223,7 @@ export default class AutoHeightWebView extends PureComponent {
245 223
       return script;
246 224
     }
247 225
     // Escape any single quotes or newlines in the CSS with .replace()
248
-    const escaped = styles.replace(/\'/g, "\\'").replace(/\n/g, '\\n')
226
+    const escaped = styles.replace(/\'/g, "\\'").replace(/\n/g, '\\n');
249 227
     return `
250 228
       var styleElement = document.createElement('style');
251 229
       var styleText = document.createTextNode('${escaped}');
@@ -255,38 +233,41 @@ export default class AutoHeightWebView extends PureComponent {
255 233
     `;
256 234
   }
257 235
 
258
-  onLoadingStart = (event) => {
236
+  onLoadingStart = event => {
259 237
     var onLoadStart = this.props.onLoadStart;
260 238
     onLoadStart && onLoadStart(event);
261 239
   };
262 240
 
263
-  onLoadingError = (event) => {
264
-    var {onError, onLoadEnd} = this.props;
241
+  onLoadingError = event => {
242
+    var { onError, onLoadEnd } = this.props;
265 243
     onError && onError(event);
266 244
     onLoadEnd && onLoadEnd(event);
267 245
     console.warn('Encountered an error loading page', event.nativeEvent);
268 246
   };
269 247
 
270
-  onLoadingFinish = (event) => {
271
-    var {onLoad, onLoadEnd} = this.props;
248
+  onLoadingFinish = event => {
249
+    var { onLoad, onLoadEnd } = this.props;
272 250
     onLoad && onLoad(event);
273 251
     onLoadEnd && onLoadEnd(event);
274 252
   };
275 253
 
254
+  getWebView = webView => (this.webView = webView);
255
+
256
+  stopLoading() {
257
+    UIManager.dispatchViewManagerCommand(
258
+      findNodeHandle(this.webView),
259
+      UIManager.RCTAutoHeightWebView.Commands.stopLoading,
260
+      null
261
+    );
262
+  }
263
+
276 264
   render() {
277 265
     const { height, script, isChangingSource, heightOffset } = this.state;
278
-    const {
279
-      scalesPageToFit,
280
-      enableAnimation,
281
-      source,
282
-      customScript,
283
-      style,
284
-      enableBaseUrl
285
-    } = this.props;
266
+    const { scalesPageToFit, enableAnimation, source, customScript, style, enableBaseUrl } = this.props;
286 267
     let webViewSource = source;
287 268
     if (enableBaseUrl) {
288 269
       webViewSource = Object.assign({}, source, {
289
-        baseUrl: "file:///android_asset/web/"
270
+        baseUrl: 'file:///android_asset/web/'
290 271
       });
291 272
     }
292 273
     return (
@@ -305,7 +286,7 @@ export default class AutoHeightWebView extends PureComponent {
305 286
             onLoadingStart={this.onLoadingStart}
306 287
             onLoadingFinish={this.onLoadingFinish}
307 288
             onLoadingError={this.onLoadingError}
308
-            ref={webview => (this.webview = webview)}
289
+            ref={this.getWebView}
309 290
             style={Styles.webView}
310 291
             javaScriptEnabled={true}
311 292
             injectedJavaScript={script + customScript}
@@ -322,18 +303,18 @@ export default class AutoHeightWebView extends PureComponent {
322 303
   }
323 304
 }
324 305
 
325
-const ScreenWidth = Dimensions.get("window").width;
306
+const ScreenWidth = Dimensions.get('window').width;
326 307
 
327 308
 const IsBelowKitKat = Platform.Version < 19;
328 309
 
329 310
 const Styles = StyleSheet.create({
330 311
   container: {
331 312
     width: ScreenWidth,
332
-    backgroundColor: "transparent"
313
+    backgroundColor: 'transparent'
333 314
   },
334 315
   webView: {
335 316
     flex: 1,
336
-    backgroundColor: "transparent"
317
+    backgroundColor: 'transparent'
337 318
   }
338 319
 });
339 320
 

+ 7
- 0
autoHeightWebView/index.ios.js Visa fil

@@ -79,6 +79,12 @@ export default class AutoHeightWebView extends PureComponent {
79 79
     }
80 80
   };
81 81
 
82
+  getWebView = webView => (this.webView = webView);
83
+
84
+  stopLoading() {
85
+    this.webView.stopLoading();
86
+  }
87
+
82 88
   render() {
83 89
     const { height, script } = this.state;
84 90
     const {
@@ -107,6 +113,7 @@ export default class AutoHeightWebView extends PureComponent {
107 113
         ]}
108 114
       >
109 115
         <WebView
116
+          ref={this.getWebView}
110 117
           onError={onError}
111 118
           onLoad={onLoad}
112 119
           onLoadStart={onLoadStart}