Browse Source

clean up code style for index.android, index.ios

iou90 6 years ago
parent
commit
78e5a9888f
3 changed files with 39 additions and 46 deletions
  1. 2
    2
      autoHeightWebView/common.js
  2. 24
    31
      autoHeightWebView/index.android.js
  3. 13
    13
      autoHeightWebView/index.ios.js

+ 2
- 2
autoHeightWebView/common.js View File

@@ -44,7 +44,7 @@ function onHeightUpdated(height, props) {
44 44
   props.onHeightUpdated && props.onHeightUpdated(height);
45 45
 }
46 46
 
47
-const DomMutationObserveScript = 
47
+const domMutationObserveScript = 
48 48
 `
49 49
 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
50 50
 var observer = new MutationObserver(updateHeight);
@@ -54,4 +54,4 @@ observer.observe(document, {
54 54
 });
55 55
 `
56 56
 
57
-export { getScript, onHeightUpdated, DomMutationObserveScript };
57
+export { getScript, onHeightUpdated, domMutationObserveScript };

+ 24
- 31
autoHeightWebView/index.android.js View File

@@ -19,7 +19,7 @@ import PropTypes from 'prop-types';
19 19
 
20 20
 import Immutable from 'immutable';
21 21
 
22
-import { getScript, onHeightUpdated, DomMutationObserveScript } from './common.js';
22
+import { getScript, onHeightUpdated, domMutationObserveScript } from './common.js';
23 23
 
24 24
 const RCTAutoHeightWebView = requireNativeComponent('RCTAutoHeightWebView', AutoHeightWebView, {
25 25
   nativeOnly: {
@@ -74,18 +74,15 @@ export default class AutoHeightWebView extends PureComponent {
74 74
   constructor(props) {
75 75
     super(props);
76 76
     props.enableAnimation && (this.opacityAnimatedValue = new Animated.Value(0));
77
+    isBelowKitKat && DeviceEventEmitter.addListener('webViewBridgeMessage', this.listenWebViewBridgeMessage);
77 78
     this.state = {
78 79
       isChangingSource: false,
79 80
       height: 0,
80 81
       heightOffset: 0,
81
-      script: getScript(props, BaseScript)
82
+      script: getScript(props, baseScript)
82 83
     };
83 84
   }
84 85
 
85
-  componentWillMount() {
86
-    IsBelowKitKat && DeviceEventEmitter.addListener('webViewBridgeMessage', this.listenWebViewBridgeMessage);
87
-  }
88
-
89 86
   componentDidMount() {
90 87
     this.startInterval();
91 88
   }
@@ -107,12 +104,12 @@ export default class AutoHeightWebView extends PureComponent {
107 104
         }
108 105
       );
109 106
     }
110
-    this.setState({ script: getScript(nextProps, BaseScript) });
107
+    this.setState({ script: getScript(nextProps, baseScript) });
111 108
   }
112 109
 
113 110
   componentWillUnmount() {
114 111
     this.stopInterval();
115
-    IsBelowKitKat && DeviceEventEmitter.removeListener('webViewBridgeMessage', this.listenWebViewBridgeMessage);
112
+    isBelowKitKat && DeviceEventEmitter.removeListener('webViewBridgeMessage', this.listenWebViewBridgeMessage);
116 113
   }
117 114
 
118 115
   // below kitkat
@@ -139,7 +136,7 @@ export default class AutoHeightWebView extends PureComponent {
139 136
     this.finishInterval = false;
140 137
     this.interval = setInterval(() => {
141 138
       if (!this.finishInterval) {
142
-        IsBelowKitKat ? this.sendToWebView('getBodyHeight') : this.postMessage('getBodyHeight');
139
+        isBelowKitKat ? this.sendToWebView('getBodyHeight') : this.postMessage('getBodyHeight');
143 140
       }
144 141
     }, 205);
145 142
   }
@@ -150,12 +147,10 @@ export default class AutoHeightWebView extends PureComponent {
150 147
   }
151 148
 
152 149
   onMessage = e => {
153
-    const height = parseInt(IsBelowKitKat ? e.nativeEvent.message : e.nativeEvent.data);
154
-    if (height) {
150
+    const height = parseInt(isBelowKitKat ? e.nativeEvent.message : e.nativeEvent.data);
151
+    if (height && height !== this.state.height) {
155 152
       const { enableAnimation, animationDuration, heightOffset } = this.props;
156
-      if (enableAnimation) {
157
-        this.opacityAnimatedValue.setValue(0);
158
-      }
153
+      enableAnimation && this.opacityAnimatedValue.setValue(0);
159 154
       this.stopInterval();
160 155
       this.setState(
161 156
         {
@@ -163,14 +158,12 @@ export default class AutoHeightWebView extends PureComponent {
163 158
           height
164 159
         },
165 160
         () => {
166
-          if (enableAnimation) {
167
-            Animated.timing(this.opacityAnimatedValue, {
168
-              toValue: 1,
169
-              duration: animationDuration
170
-            }).start(() => onHeightUpdated(height, this.props));
171
-          } else {
172
-            onHeightUpdated(height, this.props);
173
-          }
161
+          enableAnimation
162
+            ? Animated.timing(this.opacityAnimatedValue, {
163
+                toValue: 1,
164
+                duration: animationDuration
165
+              }).start(() => onHeightUpdated(height, this.props))
166
+            : onHeightUpdated(height, this.props);
174 167
         }
175 168
       );
176 169
     }
@@ -216,7 +209,7 @@ export default class AutoHeightWebView extends PureComponent {
216 209
     return (
217 210
       <Animated.View
218 211
         style={[
219
-          Styles.container,
212
+          styles.container,
220 213
           {
221 214
             opacity: enableAnimation ? this.opacityAnimatedValue : 1,
222 215
             height: height + heightOffset
@@ -230,7 +223,7 @@ export default class AutoHeightWebView extends PureComponent {
230 223
             onLoadingFinish={this.onLoadingFinish}
231 224
             onLoadingError={this.onLoadingError}
232 225
             ref={this.getWebView}
233
-            style={Styles.webView}
226
+            style={styles.webView}
234 227
             javaScriptEnabled={true}
235 228
             injectedJavaScript={script + customScript}
236 229
             scalesPageToFit={scalesPageToFit}
@@ -246,13 +239,13 @@ export default class AutoHeightWebView extends PureComponent {
246 239
   }
247 240
 }
248 241
 
249
-const ScreenWidth = Dimensions.get('window').width;
242
+const screenWidth = Dimensions.get('window').width;
250 243
 
251
-const IsBelowKitKat = Platform.Version < 19;
244
+const isBelowKitKat = Platform.Version < 19;
252 245
 
253
-const Styles = StyleSheet.create({
246
+const styles = StyleSheet.create({
254 247
   container: {
255
-    width: ScreenWidth,
248
+    width: screenWidth,
256 249
     backgroundColor: 'transparent'
257 250
   },
258 251
   webView: {
@@ -261,13 +254,13 @@ const Styles = StyleSheet.create({
261 254
   }
262 255
 });
263 256
 
264
-const BaseScript = IsBelowKitKat
257
+const baseScript = isBelowKitKat
265 258
   ? `
266 259
     ; (function () {
267 260
         AutoHeightWebView.onMessage = function (message) {
268 261
             AutoHeightWebView.send(String(document.body.offsetHeight));
269 262
         };
270
-        ${DomMutationObserveScript}
263
+        ${domMutationObserveScript}
271 264
     } ());
272 265
     `
273 266
   : `
@@ -275,6 +268,6 @@ const BaseScript = IsBelowKitKat
275 268
         document.addEventListener('message', function (e) {
276 269
             window.postMessage(String(document.body.offsetHeight));
277 270
         });
278
-        ${DomMutationObserveScript}
271
+        ${domMutationObserveScript}
279 272
     } ());
280 273
     `;

+ 13
- 13
autoHeightWebView/index.ios.js View File

@@ -2,11 +2,11 @@
2 2
 
3 3
 import React, { PureComponent } from 'react';
4 4
 
5
-import { Animated, Dimensions, StyleSheet, View, ViewPropTypes, WebView } from 'react-native';
5
+import { Animated, Dimensions, StyleSheet, ViewPropTypes, WebView } from 'react-native';
6 6
 
7 7
 import PropTypes from 'prop-types';
8 8
 
9
-import { getScript, onHeightUpdated, DomMutationObserveScript } from './common.js';
9
+import { getScript, onHeightUpdated, domMutationObserveScript } from './common.js';
10 10
 
11 11
 export default class AutoHeightWebView extends PureComponent {
12 12
   static propTypes = {
@@ -51,12 +51,12 @@ export default class AutoHeightWebView extends PureComponent {
51 51
     props.enableAnimation && (this.opacityAnimatedValue = new Animated.Value(0));
52 52
     this.state = {
53 53
       height: 0,
54
-      script: getScript(props, BaseScript, IframeBaseScript)
54
+      script: getScript(props, baseScript, iframeBaseScript)
55 55
     };
56 56
   }
57 57
 
58 58
   componentWillReceiveProps(nextProps) {
59
-    this.setState({ script: getScript(nextProps, BaseScript, IframeBaseScript) });
59
+    this.setState({ script: getScript(nextProps, baseScript, iframeBaseScript) });
60 60
   }
61 61
 
62 62
   handleNavigationStateChange = navState => {
@@ -100,7 +100,7 @@ export default class AutoHeightWebView extends PureComponent {
100 100
     return (
101 101
       <Animated.View
102 102
         style={[
103
-          Styles.container,
103
+          styles.container,
104 104
           {
105 105
             opacity: enableAnimation ? this.opacityAnimatedValue : 1,
106 106
             height: height + heightOffset
@@ -115,7 +115,7 @@ export default class AutoHeightWebView extends PureComponent {
115 115
           onLoadStart={onLoadStart}
116 116
           onLoadEnd={onLoadEnd}
117 117
           onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
118
-          style={Styles.webView}
118
+          style={styles.webView}
119 119
           injectedJavaScript={script + customScript}
120 120
           scrollEnabled={false}
121 121
           scalesPageToFit={scalesPageToFit}
@@ -127,11 +127,11 @@ export default class AutoHeightWebView extends PureComponent {
127 127
   }
128 128
 }
129 129
 
130
-const ScreenWidth = Dimensions.get('window').width;
130
+const screenWidth = Dimensions.get('window').width;
131 131
 
132
-const Styles = StyleSheet.create({
132
+const styles = StyleSheet.create({
133 133
   container: {
134
-    width: ScreenWidth,
134
+    width: screenWidth,
135 135
     backgroundColor: 'transparent'
136 136
   },
137 137
   webView: {
@@ -140,7 +140,7 @@ const Styles = StyleSheet.create({
140 140
   }
141 141
 });
142 142
 
143
-const BaseScript = `
143
+const baseScript = `
144 144
     ;
145 145
     (function () {
146 146
         var i = 0;
@@ -158,11 +158,11 @@ const BaseScript = `
158 158
                 window.location.hash = ++i;
159 159
             }
160 160
         }
161
-        ${DomMutationObserveScript}
161
+        ${domMutationObserveScript}
162 162
     } ());
163 163
     `;
164 164
 
165
-const IframeBaseScript = `
165
+const iframeBaseScript = `
166 166
     ;
167 167
     (function () {
168 168
         var i = 0;
@@ -177,6 +177,6 @@ const IframeBaseScript = `
177 177
         updateHeight();
178 178
         window.addEventListener('load', updateHeight);
179 179
         window.addEventListener('resize', updateHeight);
180
-        ${DomMutationObserveScript}
180
+        ${domMutationObserveScript}
181 181
     } ());
182 182
     `;