Browse Source

Adding autoWidth Adjustment

Tested! Works!
ShaMan123 6 years ago
parent
commit
1f59b599e1
No account linked to committer's email address
1 changed files with 84 additions and 47 deletions
  1. 84
    47
      autoHeightWebView/index.android.js

+ 84
- 47
autoHeightWebView/index.android.js View File

19
 
19
 
20
 import Immutable from 'immutable';
20
 import Immutable from 'immutable';
21
 
21
 
22
-import { getScript, onHeightUpdated, domMutationObserveScript, getHeight } from './common.js';
22
+import { getScript, onHeightUpdated, onWidthUpdated, onHeightWidthUpdated, domMutationObserveScript } from './common.js';
23
 
23
 
24
 const RCTAutoHeightWebView = requireNativeComponent('RCTAutoHeightWebView', AutoHeightWebView, {
24
 const RCTAutoHeightWebView = requireNativeComponent('RCTAutoHeightWebView', AutoHeightWebView, {
25
   nativeOnly: {
25
   nativeOnly: {
32
   }
32
   }
33
 });
33
 });
34
 
34
 
35
+const screenWidth = Dimensions.get('window').width;
36
+
35
 export default class AutoHeightWebView extends PureComponent {
37
 export default class AutoHeightWebView extends PureComponent {
36
   static propTypes = {
38
   static propTypes = {
37
     source: WebView.propTypes.source,
39
     source: WebView.propTypes.source,
38
-    onHeightUpdated: PropTypes.func,
40
+      onHeightUpdated: PropTypes.func,
41
+      onWidthUpdated: PropTypes.func,
42
+      onHeightWidthUpdated: PropTypes.func,
43
+      shouldResizeWidth: PropTypes.bool,
39
     customScript: PropTypes.string,
44
     customScript: PropTypes.string,
40
     customStyle: PropTypes.string,
45
     customStyle: PropTypes.string,
41
     enableAnimation: PropTypes.bool,
46
     enableAnimation: PropTypes.bool,
44
     // only works on enable animation
49
     // only works on enable animation
45
     animationDuration: PropTypes.number,
50
     animationDuration: PropTypes.number,
46
     // offset of rn webView margin
51
     // offset of rn webView margin
47
-    heightOffset: PropTypes.number,
52
+      heightOffset: PropTypes.number,
53
+      widthOffset: PropTypes.number,
48
     // baseUrl not work in android 4.3 or below version
54
     // baseUrl not work in android 4.3 or below version
49
     enableBaseUrl: PropTypes.bool,
55
     enableBaseUrl: PropTypes.bool,
50
     style: ViewPropTypes.style,
56
     style: ViewPropTypes.style,
68
     enableBaseUrl: false,
74
     enableBaseUrl: false,
69
     enableAnimation: true,
75
     enableAnimation: true,
70
     animationDuration: 555,
76
     animationDuration: 555,
71
-    heightOffset: 20
77
+      heightOffset: 20,
78
+      widthOffset: 20,
79
+      shouldResizeWidth: false
72
   };
80
   };
73
 
81
 
74
   constructor(props) {
82
   constructor(props) {
78
     this.state = {
86
     this.state = {
79
       isChangingSource: false,
87
       isChangingSource: false,
80
       height: 0,
88
       height: 0,
81
-      heightOffset: 0,
89
+        heightOffset: 0,
90
+        width: screenWidth,
91
+        widthOffset: 0,
82
       script: getScript(props, baseScript)
92
       script: getScript(props, baseScript)
83
     };
93
     };
84
   }
94
   }
89
 
99
 
90
   componentWillReceiveProps(nextProps) {
100
   componentWillReceiveProps(nextProps) {
91
     // injectedJavaScript only works when webView reload (source changed)
101
     // injectedJavaScript only works when webView reload (source changed)
92
-    if (Immutable.is(Immutable.fromJS(this.props.source), Immutable.fromJS(nextProps.source))) {
93
-      return;
94
-    } else {
95
-      this.setState(
96
-        {
97
-          isChangingSource: true,
98
-          height: 0,
99
-          heightOffset: 0
100
-        },
101
-        () => {
102
-          this.startInterval();
103
-          this.setState({ isChangingSource: false });
104
-        }
105
-      );
106
-    }
102
+      if (Immutable.is(Immutable.fromJS(this.props.source), Immutable.fromJS(nextProps.source))) {
103
+          return;
104
+      }
105
+      else {
106
+          this.setState(
107
+              {
108
+                  isChangingSource: true,
109
+                  height: 0,
110
+                  heightOffset: 0,
111
+                  width: 0,
112
+                  widthOffset: 0,
113
+              },
114
+              () => {
115
+                  this.startInterval();
116
+                  this.setState({ isChangingSource: false });
117
+              }
118
+          );
119
+      }
107
     this.setState({ script: getScript(nextProps, baseScript) });
120
     this.setState({ script: getScript(nextProps, baseScript) });
108
   }
121
   }
109
 
122
 
147
   }
160
   }
148
 
161
 
149
   onMessage = e => {
162
   onMessage = e => {
150
-    const height = parseInt(isBelowKitKat ? e.nativeEvent.message : e.nativeEvent.data);
151
-    if (height && height !== this.state.height) {
152
-      const { enableAnimation, animationDuration, heightOffset } = this.props;
153
-      enableAnimation && this.opacityAnimatedValue.setValue(0);
154
-      this.stopInterval();
155
-      this.setState(
156
-        {
157
-          heightOffset,
158
-          height
159
-        },
160
-        () => {
161
-          enableAnimation
162
-            ? Animated.timing(this.opacityAnimatedValue, {
163
-                toValue: 1,
164
-                duration: animationDuration
165
-              }).start(() => onHeightUpdated(height, this.props))
166
-            : onHeightUpdated(height, this.props);
167
-        }
168
-      );
169
-    }
163
+      const { height, width } = JSON.parse(isBelowKitKat ? e.nativeEvent.message : e.nativeEvent.data);
164
+      if (height && height !== this.state.height && width && width !== this.state.width) {
165
+          const { enableAnimation, animationDuration, heightOffset, widthOffset } = this.props;
166
+          enableAnimation && this.opacityAnimatedValue.setValue(0);
167
+          this.stopInterval();
168
+          
169
+          this.setState(
170
+              {
171
+                  heightOffset,
172
+                  height,
173
+                  widthOffset,
174
+                  width
175
+              },
176
+              () => {
177
+                  enableAnimation
178
+                      ? Animated.timing(this.opacityAnimatedValue, {
179
+                          toValue: 1,
180
+                          duration: animationDuration
181
+                      }).start(() => onHeightWidthUpdated(height, width, this.props))
182
+                      : onHeightWidthUpdated(height, width, this.props);
183
+              }
184
+          );
185
+      }
170
   };
186
   };
171
 
187
 
172
   onLoadingStart = event => {
188
   onLoadingStart = event => {
198
   }
214
   }
199
 
215
 
200
   render() {
216
   render() {
201
-    const { height, script, isChangingSource, heightOffset } = this.state;
217
+      const { height, width, script, isChangingSource, heightOffset, widthOffset } = this.state;
202
     const { scalesPageToFit, enableAnimation, source, customScript, style, enableBaseUrl } = this.props;
218
     const { scalesPageToFit, enableAnimation, source, customScript, style, enableBaseUrl } = this.props;
203
     let webViewSource = source;
219
     let webViewSource = source;
204
     if (enableBaseUrl) {
220
     if (enableBaseUrl) {
206
         baseUrl: 'file:///android_asset/web/'
222
         baseUrl: 'file:///android_asset/web/'
207
       });
223
       });
208
     }
224
     }
225
+      
209
     return (
226
     return (
210
       <Animated.View
227
       <Animated.View
211
         style={[
228
         style={[
212
           styles.container,
229
           styles.container,
213
           {
230
           {
214
             opacity: enableAnimation ? this.opacityAnimatedValue : 1,
231
             opacity: enableAnimation ? this.opacityAnimatedValue : 1,
215
-            height: height + heightOffset
232
+              height: height + heightOffset,
233
+              width: width + widthOffset
216
           },
234
           },
217
           style
235
           style
218
         ]}
236
         ]}
239
   }
257
   }
240
 }
258
 }
241
 
259
 
242
-const screenWidth = Dimensions.get('window').width;
243
-
244
 const isBelowKitKat = Platform.Version < 19;
260
 const isBelowKitKat = Platform.Version < 19;
245
 
261
 
246
 const styles = StyleSheet.create({
262
 const styles = StyleSheet.create({
247
   container: {
263
   container: {
248
-    width: screenWidth,
264
+    //width: screenWidth,
249
     backgroundColor: 'transparent'
265
     backgroundColor: 'transparent'
250
   },
266
   },
251
   webView: {
267
   webView: {
257
 const baseScript = isBelowKitKat
273
 const baseScript = isBelowKitKat
258
   ? `
274
   ? `
259
     ; (function () {
275
     ; (function () {
276
+        var wrapper = document.createElement('div');
277
+        wrapper.id = 'wrapper';
278
+        while (document.body.firstChild instanceof Node) {
279
+            wrapper.appendChild(document.body.firstChild);
280
+        }
281
+        document.body.appendChild(wrapper);
282
+
260
         AutoHeightWebView.onMessage = function (message) {
283
         AutoHeightWebView.onMessage = function (message) {
261
-            AutoHeightWebView.send(String(document.body.offsetHeight));
284
+            var rect = document.body.firstElementChild.getBoundingClientRect().toJSON();
285
+            var width = Math.round(rect.width);
286
+            var height = Math.round(rect.height);
287
+            AutoHeightWebView.send(JSON.stringify({ width, height }));
262
         };
288
         };
263
         ${domMutationObserveScript}
289
         ${domMutationObserveScript}
264
     } ());
290
     } ());
265
     `
291
     `
266
   : `
292
   : `
267
     ; (function () {
293
     ; (function () {
294
+        var wrapper = document.createElement('div');
295
+        wrapper.id = 'wrapper';
296
+        while (document.body.firstChild instanceof Node) {
297
+            wrapper.appendChild(document.body.firstChild);
298
+        }
299
+        document.body.appendChild(wrapper);
300
+
268
         document.addEventListener('message', function (e) {
301
         document.addEventListener('message', function (e) {
269
-            window.postMessage(String(document.body.offsetHeight));
302
+            var rect = document.body.firstElementChild.getBoundingClientRect().toJSON();
303
+            var width = Math.round(rect.width);
304
+            var height = Math.round(rect.height);
305
+            window.postMessage(JSON.stringify({ width, height }));
270
         });
306
         });
271
         ${domMutationObserveScript}
307
         ${domMutationObserveScript}
272
     } ());
308
     } ());
273
     `;
309
     `;
310
+