Преглед изворни кода

improve componentWillReceiveProps for index.ios.js

iou90 пре 6 година
родитељ
комит
d83aa2b404
3 измењених фајлова са 30 додато и 17 уклоњено
  1. 19
    4
      autoHeightWebView/common.js
  2. 10
    12
      autoHeightWebView/index.ios.js
  3. 1
    1
      demo/App.js

+ 19
- 4
autoHeightWebView/common.js Прегледај датотеку

62
 }
62
 }
63
 
63
 
64
 function getScript(props, getBaseScript, getIframeBaseScript) {
64
 function getScript(props, getBaseScript, getIframeBaseScript) {
65
-  const { hasIframe, files, customStyle, customScript, style } = props;
65
+  const { hasIframe, files, customStyle, customScript, style } = getReloadRelatedData(props);
66
   const baseScript = getBaseScript(style);
66
   const baseScript = getBaseScript(style);
67
   let script = hasIframe ? baseScript : getIframeBaseScript(style);
67
   let script = hasIframe ? baseScript : getIframeBaseScript(style);
68
   script = files ? appendFilesToHead(files, baseScript) : baseScript;
68
   script = files ? appendFilesToHead(files, baseScript) : baseScript;
71
   return script;
71
   return script;
72
 }
72
 }
73
 
73
 
74
-function needChangeSource(nextProps, props) {
75
- return nextProps && props && isChanged(getReloadRelatedData(nextProps), getReloadRelatedData(props));
74
+function getSize(nextStyle, prevStyle) {
75
+  if (!nextStyle) {
76
+    return null;
77
+  }
78
+  if (isChanged(nextStyle, prevStyle)) {
79
+    let size = null;
80
+    const { height, width } = nextStyle;
81
+    height && Object.assign(size, { height });
82
+    width && Object.assign(size, { width });
83
+    return size;
84
+  } else {
85
+    return null;
86
+  }
87
+}
88
+
89
+function isScriptChanged(nextProps, props) {
90
+  return nextProps && props && isChanged(getReloadRelatedData(nextProps), getReloadRelatedData(props));
76
 }
91
 }
77
 
92
 
78
 function handleSizeUpdated(height, width, onSizeUpdated) {
93
 function handleSizeUpdated(height, width, onSizeUpdated) {
92
 });
107
 });
93
 `;
108
 `;
94
 
109
 
95
-export { needChangeSource, getWidth, getScript, handleSizeUpdated, domMutationObserveScript };
110
+export { getSize, isScriptChanged, getWidth, getScript, handleSizeUpdated, domMutationObserveScript };

+ 10
- 12
autoHeightWebView/index.ios.js Прегледај датотеку

6
 
6
 
7
 import PropTypes from 'prop-types';
7
 import PropTypes from 'prop-types';
8
 
8
 
9
-import { needChangeSource, getWidth, getScript, handleSizeUpdated, domMutationObserveScript } from './common.js';
9
+import { isScriptChanged, getSize, getWidth, getScript, handleSizeUpdated, domMutationObserveScript } from './common.js';
10
 
10
 
11
 export default class AutoHeightWebView extends PureComponent {
11
 export default class AutoHeightWebView extends PureComponent {
12
   static propTypes = {
12
   static propTypes = {
53
     const { enableAnimation, style } = props;
53
     const { enableAnimation, style } = props;
54
     enableAnimation && (this.opacityAnimatedValue = new Animated.Value(0));
54
     enableAnimation && (this.opacityAnimatedValue = new Animated.Value(0));
55
     this.state = {
55
     this.state = {
56
+      isScriptChanged: false,
56
       width: getWidth(style),
57
       width: getWidth(style),
57
       height: style && style.height ? style.height : 0,
58
       height: style && style.height ? style.height : 0,
58
       script: getScript(props, getBaseScript, getIframeBaseScript)
59
       script: getScript(props, getBaseScript, getIframeBaseScript)
60
   }
61
   }
61
 
62
 
62
   componentWillReceiveProps(nextProps) {
63
   componentWillReceiveProps(nextProps) {
63
-    if (nextProps.style) {
64
-      const { width, height } = nextProps.style;
65
-      width && this.setState({ width });
66
-      height && this.setState({ height });
67
-    }
68
-    this.setState({ script: getScript(nextProps, getBaseScript, getIframeBaseScript) });
69
-    this.needChangeSource = needChangeSource(nextProps, this.props);
64
+    const size = getSize(nextProps, this.props);
65
+    size && this.setState(size); 
66
+    this.isScriptChanged = isScriptChanged(nextProps, this.props);
67
+    this.isScriptChanged && this.setState({ script: getScript(nextProps, getBaseScript, getIframeBaseScript) });
70
   }
68
   }
71
 
69
 
72
   handleNavigationStateChange = navState => {
70
   handleNavigationStateChange = navState => {
121
       scrollEnabled
119
       scrollEnabled
122
     } = this.props;
120
     } = this.props;
123
     let webViewSource = Object.assign({}, source, { baseUrl: 'web/' });
121
     let webViewSource = Object.assign({}, source, { baseUrl: 'web/' });
124
-    if (this.needChangeSource) {
122
+    if (this.isScriptChanged) {
125
       this.changeSourceFlag = !this.changeSourceFlag;
123
       this.changeSourceFlag = !this.changeSourceFlag;
126
       webViewSource = Object.assign(webViewSource, { changeSourceFlag: this.changeSourceFlag });
124
       webViewSource = Object.assign(webViewSource, { changeSourceFlag: this.changeSourceFlag });
127
     }
125
     }
173
     window.addEventListener('resize', updateSize);
171
     window.addEventListener('resize', updateSize);
174
     `;
172
     `;
175
 
173
 
176
-const getSize = `
174
+const getCurrentSize = `
177
     function getSize(container) {
175
     function getSize(container) {
178
       var height = container.clientHeight || document.body.offsetHeight;
176
       var height = container.clientHeight || document.body.offsetHeight;
179
       var width = container.clientWidth || document.body.offsetWidth;
177
       var width = container.clientWidth || document.body.offsetWidth;
187
 function getBaseScript(style) {
185
 function getBaseScript(style) {
188
   return `
186
   return `
189
     ;
187
     ;
190
-    ${getSize}
188
+    ${getCurrentSize}
191
     (function () {
189
     (function () {
192
         var height = 0;
190
         var height = 0;
193
         var width = ${getWidth(style)};
191
         var width = ${getWidth(style)};
214
 function getIframeBaseScript(style) {
212
 function getIframeBaseScript(style) {
215
   return `
213
   return `
216
     ;
214
     ;
217
-    ${getSize}
215
+    ${getCurrentSize}
218
     (function () {
216
     (function () {
219
         var height = 0;
217
         var height = 0;
220
         var width = ${getWidth(style)};
218
         var width = ${getWidth(style)};

+ 1
- 1
demo/App.js Прегледај датотеку

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