|
@@ -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
|
`;
|