Parcourir la source

add getRenderSize to common; add onNavigationStateChange to index.ios/index.android

iou90 il y a 5 ans
Parent
révision
b5d2ee6ab1

+ 7
- 0
autoHeightWebView/common.js Voir le fichier

@@ -135,3 +135,10 @@ function getSize(container) {
135 135
   };
136 136
 }
137 137
 `;
138
+
139
+export function getRenderSize(enableAnimation, height, width, heightOffset, heightValue, widthValue) {
140
+  return {
141
+    height: enableAnimation ? heightValue : height ? height + heightOffset : 0,
142
+    width: enableAnimation ? widthValue : width
143
+  }
144
+}

+ 12
- 10
autoHeightWebView/index.android.js Voir le fichier

@@ -25,7 +25,8 @@ import {
25 25
   getWidth,
26 26
   getScript,
27 27
   domMutationObserveScript,
28
-  getCurrentSize
28
+  getCurrentSize,
29
+  getRenderSize
29 30
 } from './common.js';
30 31
 
31 32
 const RCTAutoHeightWebView = requireNativeComponent('RCTAutoHeightWebView', AutoHeightWebView, {
@@ -47,6 +48,7 @@ const getUpdatedState = momoize(setState, isEqual);
47 48
 
48 49
 export default class AutoHeightWebView extends PureComponent {
49 50
   static propTypes = {
51
+    onNavigationStateChange: PropTypes.func,
50 52
     onMessage: PropTypes.func,
51 53
     scrollEnabled: PropTypes.bool,
52 54
     source: WebView.propTypes.source,
@@ -59,6 +61,7 @@ export default class AutoHeightWebView extends PureComponent {
59 61
     scalesPageToFit: PropTypes.bool,
60 62
     // only works on enable animation
61 63
     animationDuration: PropTypes.number,
64
+    // only on android
62 65
     animationEasing: PropTypes.func,
63 66
     // offset of rn webView margin
64 67
     heightOffset: PropTypes.number,
@@ -95,18 +98,18 @@ export default class AutoHeightWebView extends PureComponent {
95 98
     isBelowKitKat && DeviceEventEmitter.addListener('webViewBridgeMessage', this.listenWebViewBridgeMessage);
96 99
     this.finishInterval = true;
97 100
     const initWidth = getWidth(style);
98
-    const height = style ? (style.height ? style.height : 0) : 0;
101
+    const initHeight = style ? (style.height ? style.height : 0) : 0;
99 102
     let state = {
100 103
       isSizeChanged: false,
101 104
       isSizeMayChange: false,
102
-      height: height,
105
+      height: initHeight,
103 106
       width: initWidth,
104 107
       script: getScript(props, getBaseScript),
105 108
       source: enableBaseUrl ? Object.assign({}, source, { baseUrl }) : source
106 109
     };
107 110
     if (enableAnimation) {
108 111
       Object.assign(state, {
109
-        heightValue: new Animated.Value(height + heightOffset),
112
+        heightValue: new Animated.Value(initHeight + heightOffset),
110 113
         widthValue: new Animated.Value(initWidth)
111 114
       });
112 115
     }
@@ -231,8 +234,9 @@ export default class AutoHeightWebView extends PureComponent {
231 234
   };
232 235
 
233 236
   onLoadingStart = event => {
234
-    const { onLoadStart } = this.props;
237
+    const { onLoadStart, onNavigationStateChange } = this.props;
235 238
     onLoadStart && onLoadStart(event);
239
+    onNavigationStateChange && onNavigationStateChange(event.nativeEvent);
236 240
   };
237 241
 
238 242
   onLoadingError = event => {
@@ -243,9 +247,10 @@ export default class AutoHeightWebView extends PureComponent {
243 247
   };
244 248
 
245 249
   onLoadingFinish = event => {
246
-    const { onLoad, onLoadEnd } = this.props;
250
+    const { onLoad, onLoadEnd, onNavigationStateChange } = this.props;
247 251
     onLoad && onLoad(event);
248 252
     onLoadEnd && onLoadEnd(event);
253
+    onNavigationStateChange && onNavigationStateChange(event.nativeEvent);
249 254
   };
250 255
 
251 256
   stopLoading() {
@@ -265,10 +270,7 @@ export default class AutoHeightWebView extends PureComponent {
265 270
       <Animated.View
266 271
         style={[
267 272
           styles.container,
268
-          {
269
-            height: enableAnimation ? heightValue : height ? height + heightOffset : 0,
270
-            width: enableAnimation ? widthValue : width
271
-          },
273
+          getRenderSize(enableAnimation, height, width, heightOffset, heightValue, widthValue),
272 274
           style
273 275
         ]}
274 276
       >

+ 7
- 2
autoHeightWebView/index.ios.js Voir le fichier

@@ -2,7 +2,7 @@
2 2
 
3 3
 import React, { PureComponent } from 'react';
4 4
 
5
-import { Animated, StyleSheet, ViewPropTypes, WebView } from 'react-native';
5
+import { Animated, Easing, StyleSheet, ViewPropTypes, WebView } from 'react-native';
6 6
 
7 7
 import PropTypes from 'prop-types';
8 8
 
@@ -13,7 +13,8 @@ import {
13 13
   isSizeChanged,
14 14
   handleSizeUpdated,
15 15
   domMutationObserveScript,
16
-  getCurrentSize
16
+  getCurrentSize,
17
+  getRenderSize
17 18
 } from './common.js';
18 19
 
19 20
 import momoize from './momoize';
@@ -21,6 +22,7 @@ import momoize from './momoize';
21 22
 export default class AutoHeightWebView extends PureComponent {
22 23
   static propTypes = {
23 24
     hasIframe: PropTypes.bool,
25
+    onNavigationStateChange: PropTypes.func,
24 26
     onMessage: PropTypes.func,
25 27
     source: WebView.propTypes.source,
26 28
     customScript: PropTypes.string,
@@ -107,7 +109,9 @@ export default class AutoHeightWebView extends PureComponent {
107 109
 
108 110
   handleNavigationStateChange = navState => {
109 111
     const { title } = navState;
112
+    const { onNavigationStateChange } = this.props;
110 113
     if (!title) {
114
+      onNavigationStateChange && onNavigationStateChange();
111 115
       return;
112 116
     }
113 117
     const [heightValue, widthValue] = title.split(',');
@@ -122,6 +126,7 @@ export default class AutoHeightWebView extends PureComponent {
122 126
         width
123 127
       });
124 128
     }
129
+    onNavigationStateChange && onNavigationStateChange();
125 130
   };
126 131
 
127 132
   stopLoading() {