|
@@ -19,7 +19,7 @@ import PropTypes from 'prop-types';
|
19
|
19
|
|
20
|
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
|
24
|
const RCTAutoHeightWebView = requireNativeComponent('RCTAutoHeightWebView', AutoHeightWebView, {
|
25
|
25
|
nativeOnly: {
|
|
@@ -32,10 +32,15 @@ const RCTAutoHeightWebView = requireNativeComponent('RCTAutoHeightWebView', Auto
|
32
|
32
|
}
|
33
|
33
|
});
|
34
|
34
|
|
|
35
|
+const screenWidth = Dimensions.get('window').width;
|
|
36
|
+
|
35
|
37
|
export default class AutoHeightWebView extends PureComponent {
|
36
|
38
|
static propTypes = {
|
37
|
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
|
44
|
customScript: PropTypes.string,
|
40
|
45
|
customStyle: PropTypes.string,
|
41
|
46
|
enableAnimation: PropTypes.bool,
|
|
@@ -44,7 +49,8 @@ export default class AutoHeightWebView extends PureComponent {
|
44
|
49
|
// only works on enable animation
|
45
|
50
|
animationDuration: PropTypes.number,
|
46
|
51
|
// offset of rn webView margin
|
47
|
|
- heightOffset: PropTypes.number,
|
|
52
|
+ heightOffset: PropTypes.number,
|
|
53
|
+ widthOffset: PropTypes.number,
|
48
|
54
|
// baseUrl not work in android 4.3 or below version
|
49
|
55
|
enableBaseUrl: PropTypes.bool,
|
50
|
56
|
style: ViewPropTypes.style,
|
|
@@ -68,7 +74,9 @@ export default class AutoHeightWebView extends PureComponent {
|
68
|
74
|
enableBaseUrl: false,
|
69
|
75
|
enableAnimation: true,
|
70
|
76
|
animationDuration: 555,
|
71
|
|
- heightOffset: 20
|
|
77
|
+ heightOffset: 20,
|
|
78
|
+ widthOffset: 20,
|
|
79
|
+ shouldResizeWidth: false
|
72
|
80
|
};
|
73
|
81
|
|
74
|
82
|
constructor(props) {
|
|
@@ -78,7 +86,9 @@ export default class AutoHeightWebView extends PureComponent {
|
78
|
86
|
this.state = {
|
79
|
87
|
isChangingSource: false,
|
80
|
88
|
height: 0,
|
81
|
|
- heightOffset: 0,
|
|
89
|
+ heightOffset: 0,
|
|
90
|
+ width: screenWidth,
|
|
91
|
+ widthOffset: 0,
|
82
|
92
|
script: getScript(props, baseScript)
|
83
|
93
|
};
|
84
|
94
|
}
|
|
@@ -89,21 +99,24 @@ export default class AutoHeightWebView extends PureComponent {
|
89
|
99
|
|
90
|
100
|
componentWillReceiveProps(nextProps) {
|
91
|
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
|
120
|
this.setState({ script: getScript(nextProps, baseScript) });
|
108
|
121
|
}
|
109
|
122
|
|
|
@@ -147,26 +160,29 @@ export default class AutoHeightWebView extends PureComponent {
|
147
|
160
|
}
|
148
|
161
|
|
149
|
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
|
188
|
onLoadingStart = event => {
|
|
@@ -198,7 +214,7 @@ export default class AutoHeightWebView extends PureComponent {
|
198
|
214
|
}
|
199
|
215
|
|
200
|
216
|
render() {
|
201
|
|
- const { height, script, isChangingSource, heightOffset } = this.state;
|
|
217
|
+ const { height, width, script, isChangingSource, heightOffset, widthOffset } = this.state;
|
202
|
218
|
const { scalesPageToFit, enableAnimation, source, customScript, style, enableBaseUrl } = this.props;
|
203
|
219
|
let webViewSource = source;
|
204
|
220
|
if (enableBaseUrl) {
|
|
@@ -206,13 +222,15 @@ export default class AutoHeightWebView extends PureComponent {
|
206
|
222
|
baseUrl: 'file:///android_asset/web/'
|
207
|
223
|
});
|
208
|
224
|
}
|
|
225
|
+
|
209
|
226
|
return (
|
210
|
227
|
<Animated.View
|
211
|
228
|
style={[
|
212
|
229
|
styles.container,
|
213
|
230
|
{
|
214
|
231
|
opacity: enableAnimation ? this.opacityAnimatedValue : 1,
|
215
|
|
- height: height + heightOffset
|
|
232
|
+ height: height + heightOffset,
|
|
233
|
+ width: width + widthOffset
|
216
|
234
|
},
|
217
|
235
|
style
|
218
|
236
|
]}
|
|
@@ -239,13 +257,11 @@ export default class AutoHeightWebView extends PureComponent {
|
239
|
257
|
}
|
240
|
258
|
}
|
241
|
259
|
|
242
|
|
-const screenWidth = Dimensions.get('window').width;
|
243
|
|
-
|
244
|
260
|
const isBelowKitKat = Platform.Version < 19;
|
245
|
261
|
|
246
|
262
|
const styles = StyleSheet.create({
|
247
|
263
|
container: {
|
248
|
|
- width: screenWidth,
|
|
264
|
+ //width: screenWidth,
|
249
|
265
|
backgroundColor: 'transparent'
|
250
|
266
|
},
|
251
|
267
|
webView: {
|
|
@@ -257,17 +273,38 @@ const styles = StyleSheet.create({
|
257
|
273
|
const baseScript = isBelowKitKat
|
258
|
274
|
? `
|
259
|
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
|
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
|
289
|
${domMutationObserveScript}
|
264
|
290
|
} ());
|
265
|
291
|
`
|
266
|
292
|
: `
|
267
|
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
|
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
|
307
|
${domMutationObserveScript}
|
272
|
308
|
} ());
|
273
|
309
|
`;
|
|
310
|
+
|