Browse Source

improved updating size on ios

iou90 6 years ago
parent
commit
5dac43b2c4
3 changed files with 64 additions and 64 deletions
  1. 11
    28
      autoHeightWebView/common.js
  2. 52
    35
      autoHeightWebView/index.ios.js
  3. 1
    1
      demo/App.js

+ 11
- 28
autoHeightWebView/common.js View File

97
   return isChanged(getReloadRelatedData(newProps), getReloadRelatedData(oldProps));
97
   return isChanged(getReloadRelatedData(newProps), getReloadRelatedData(oldProps));
98
 }
98
 }
99
 
99
 
100
-export function setState(props, getBaseScript, getIframeBaseScript) {
101
-  const { source, style } = props;
100
+export function setState(props, baseUrl, getBaseScript, getIframeBaseScript) {
101
+  const { source } = props;
102
   const script = getScript(props, getBaseScript, getIframeBaseScript);
102
   const script = getScript(props, getBaseScript, getIframeBaseScript);
103
-  let state = {
104
-    height: style && style.height ? style.height : 0,
105
-    width: getWidth(style)
106
-  };
103
+  let state = {};
107
   if (source.html) {
104
   if (source.html) {
108
-    Object.assign(state, {
109
-      source: Object.assign(
110
-        {},
111
-        {
112
-          html: getInjectedSource(source.html, script),
113
-          baseUrl: 'web/'
114
-        }
115
-      )
116
-    });
105
+    let currentSource = { html: getInjectedSource(source.html, script) };
106
+    baseUrl && Object.assign(currentSource, { baseUrl });
107
+    Object.assign(state, { source: currentSource });
117
   } else {
108
   } else {
109
+    let currentSource = Object.assign({}, source);
110
+    baseUrl && Object.assign(currentSource, { baseUrl });
118
     Object.assign(state, {
111
     Object.assign(state, {
119
-      source: Object.assign({}, source, { baseUrl: 'web/' }),
112
+      source: currentSource,
120
       script
113
       script
121
     });
114
     });
122
   }
115
   }
131
     });
124
     });
132
 }
125
 }
133
 
126
 
134
-export function getSize(newHeight, newWidth, height, width, updatingSize, calledOnce) {
135
-  if (!calledOnce || updatingSize) {
136
-    return {
137
-      h: height,
138
-      w: width
139
-    };
140
-  }
141
-  return {
142
-    h: height,
143
-    w: width
144
-  };
127
+export function isSizeChanged(height, oldHeight, width, oldWidth) {
128
+  return (height && height !== oldHeight) || (width && width !== oldWidth);
145
 }
129
 }
146
 
130
 
147
 export const domMutationObserveScript = `
131
 export const domMutationObserveScript = `
163
   };
147
   };
164
 }
148
 }
165
 `;
149
 `;
166
-

+ 52
- 35
autoHeightWebView/index.ios.js View File

6
 
6
 
7
 import PropTypes from 'prop-types';
7
 import PropTypes from 'prop-types';
8
 
8
 
9
-import { getSize, isEqual, setState, getWidth, handleSizeUpdated, domMutationObserveScript, getCurrentSize } from './common.js';
9
+import {
10
+  isEqual,
11
+  setState,
12
+  getWidth,
13
+  isSizeChanged,
14
+  handleSizeUpdated,
15
+  domMutationObserveScript,
16
+  getCurrentSize
17
+} from './common.js';
10
 
18
 
11
 import momoize from './momoize';
19
 import momoize from './momoize';
12
 
20
 
57
     enableAnimation && (this.opacityAnimatedValue = new Animated.Value(0));
65
     enableAnimation && (this.opacityAnimatedValue = new Animated.Value(0));
58
     this.webView = React.createRef();
66
     this.webView = React.createRef();
59
     this.state = {
67
     this.state = {
68
+      isSizeChanged: false,
60
       width: getWidth(style),
69
       width: getWidth(style),
61
       height: style && style.height ? style.height : 0
70
       height: style && style.height ? style.height : 0
62
     };
71
     };
64
 
73
 
65
   getUpdatedState = momoize(setState, isEqual);
74
   getUpdatedState = momoize(setState, isEqual);
66
 
75
 
76
+  static getDerivedStateFromProps(props, state) {
77
+    const { height: oldHeight, width: oldWidth } = state;
78
+    const height = props.style ? props.style.height : null;
79
+    const width = props.style ? props.style.width : null;
80
+    if (isSizeChanged(height, oldHeight, width, oldWidth)) {
81
+      return {
82
+        height,
83
+        width,
84
+        isSizeChanged: true
85
+      };
86
+    }
87
+    return null;
88
+  }
89
+
90
+  componentDidUpdate() {
91
+    const { height, width, isSizeChanged } = this.state;
92
+    if (isSizeChanged) {
93
+      const { enableAnimation, animationDuration, onSizeUpdated } = this.props;
94
+      if (enableAnimation) {
95
+        Animated.timing(this.opacityAnimatedValue, {
96
+          toValue: 1,
97
+          duration: animationDuration
98
+        }).start(() => {
99
+          handleSizeUpdated(height, width, onSizeUpdated);
100
+        });
101
+      } else {
102
+        handleSizeUpdated(height, width, onSizeUpdated);
103
+      }
104
+      this.setState({ isSizeChanged: false });
105
+    }
106
+  }
107
+
67
   handleNavigationStateChange = navState => {
108
   handleNavigationStateChange = navState => {
68
     const { title } = navState;
109
     const { title } = navState;
69
     if (!title) {
110
     if (!title) {
73
     const width = Number(widthValue);
114
     const width = Number(widthValue);
74
     const height = Number(heightValue);
115
     const height = Number(heightValue);
75
     const { height: oldHeight, width: oldWidth } = this.state;
116
     const { height: oldHeight, width: oldWidth } = this.state;
76
-    if ((height && height !== oldHeight) || (width && width !== oldWidth)) {
77
-      const { enableAnimation, animationDuration, onSizeUpdated } = this.props;
78
-      enableAnimation && this.opacityAnimatedValue.setValue(0);
79
-      this.updatingSize = true;
80
-      this.setState(
81
-        {
82
-          height,
83
-          width
84
-        },
85
-        () => {
86
-          if (enableAnimation) {
87
-            Animated.timing(this.opacityAnimatedValue, {
88
-              toValue: 1,
89
-              duration: animationDuration
90
-            }).start(() => {
91
-              handleSizeUpdated(height, width, onSizeUpdated);
92
-              this.updatingSize = false;
93
-            });
94
-          } else {
95
-            handleSizeUpdated(height, width, onSizeUpdated);
96
-            this.updatingSize = false;
97
-          }
98
-        }
99
-      );
117
+    if (isSizeChanged(height, oldHeight, width, oldWidth)) {
118
+      this.props.enableAnimation && this.opacityAnimatedValue.setValue(0);
119
+      this.setState({
120
+        isSizeChanged: true,
121
+        height,
122
+        width
123
+      });
100
     }
124
     }
101
   };
125
   };
102
 
126
 
119
       style,
143
       style,
120
       scrollEnabled
144
       scrollEnabled
121
     } = this.props;
145
     } = this.props;
122
-    const { height: newHeight, width: newWidth, source, script } = this.getUpdatedState(
123
-      this.props,
124
-      getBaseScript,
125
-      getIframeBaseScript
126
-    );
127
-    const { w, h } = getSize(newHeight, newWidth, height, width, this.updatingSize, this.calledOnce);
128
-    this.calledOnce = true;
146
+    const { source, script } = this.getUpdatedState(this.props, 'web/', getBaseScript, getIframeBaseScript);
129
     return (
147
     return (
130
       <Animated.View
148
       <Animated.View
131
         style={[
149
         style={[
132
           styles.container,
150
           styles.container,
133
           {
151
           {
134
             opacity: enableAnimation ? this.opacityAnimatedValue : 1,
152
             opacity: enableAnimation ? this.opacityAnimatedValue : 1,
135
-            width: w,
136
-            height: h + heightOffset
153
+            width,
154
+            height: height + heightOffset
137
           },
155
           },
138
           style
156
           style
139
         ]}
157
         ]}
175
     window.addEventListener('resize', updateSize);
193
     window.addEventListener('resize', updateSize);
176
     `;
194
     `;
177
 
195
 
178
-
179
 function getBaseScript(style) {
196
 function getBaseScript(style) {
180
   return `
197
   return `
181
     ;
198
     ;

+ 1
- 1
demo/App.js View File

125
           <Text>change style</Text>
125
           <Text>change style</Text>
126
         </TouchableOpacity>
126
         </TouchableOpacity>
127
         <TouchableOpacity onPress={this.changeScript} style={[styles.button, { marginBottom: 100 }]}>
127
         <TouchableOpacity onPress={this.changeScript} style={[styles.button, { marginBottom: 100 }]}>
128
-          <Text>change heightScript</Text>
128
+          <Text>change script</Text>
129
         </TouchableOpacity>
129
         </TouchableOpacity>
130
       </ScrollView>
130
       </ScrollView>
131
     );
131
     );