|
@@ -7,7 +7,6 @@ import {
|
7
|
7
|
requireNativeComponent,
|
8
|
8
|
Animated,
|
9
|
9
|
DeviceEventEmitter,
|
10
|
|
- Dimensions,
|
11
|
10
|
StyleSheet,
|
12
|
11
|
Platform,
|
13
|
12
|
UIManager,
|
|
@@ -19,7 +18,7 @@ import PropTypes from 'prop-types';
|
19
|
18
|
|
20
|
19
|
import Immutable from 'immutable';
|
21
|
20
|
|
22
|
|
-import { getScript, onHeightUpdated, onWidthUpdated, onHeightWidthUpdated, domMutationObserveScript } from './common.js';
|
|
21
|
+import { handleSizeUpdated, getWidth, getScript, domMutationObserveScript } from './common.js';
|
23
|
22
|
|
24
|
23
|
const RCTAutoHeightWebView = requireNativeComponent('RCTAutoHeightWebView', AutoHeightWebView, {
|
25
|
24
|
nativeOnly: {
|
|
@@ -32,25 +31,22 @@ const RCTAutoHeightWebView = requireNativeComponent('RCTAutoHeightWebView', Auto
|
32
|
31
|
}
|
33
|
32
|
});
|
34
|
33
|
|
35
|
|
-const screenWidth = Dimensions.get('window').width;
|
36
|
|
-
|
37
|
34
|
export default class AutoHeightWebView extends PureComponent {
|
38
|
35
|
static propTypes = {
|
|
36
|
+ onMessage: PropTypes.func,
|
|
37
|
+ scrollEnabled: PropTypes.bool,
|
39
|
38
|
source: WebView.propTypes.source,
|
40
|
|
- onHeightUpdated: PropTypes.func,
|
41
|
|
- onWidthUpdated: PropTypes.func,
|
42
|
|
- onHeightWidthUpdated: PropTypes.func,
|
43
|
|
- shouldResizeWidth: PropTypes.bool,
|
44
|
39
|
customScript: PropTypes.string,
|
45
|
40
|
customStyle: PropTypes.string,
|
46
|
41
|
enableAnimation: PropTypes.bool,
|
|
42
|
+ // either height or width updated will trigger this
|
|
43
|
+ onSizeUpdated: PropTypes.func,
|
47
|
44
|
// if set to false may cause some layout issues (width of container will be than width of screen)
|
48
|
45
|
scalesPageToFit: PropTypes.bool,
|
49
|
46
|
// only works on enable animation
|
50
|
47
|
animationDuration: PropTypes.number,
|
51
|
48
|
// offset of rn webView margin
|
52
|
|
- heightOffset: PropTypes.number,
|
53
|
|
- widthOffset: PropTypes.number,
|
|
49
|
+ heightOffset: PropTypes.number,
|
54
|
50
|
// baseUrl not work in android 4.3 or below version
|
55
|
51
|
enableBaseUrl: PropTypes.bool,
|
56
|
52
|
style: ViewPropTypes.style,
|
|
@@ -74,22 +70,21 @@ export default class AutoHeightWebView extends PureComponent {
|
74
|
70
|
enableBaseUrl: false,
|
75
|
71
|
enableAnimation: true,
|
76
|
72
|
animationDuration: 555,
|
77
|
|
- heightOffset: 20,
|
78
|
|
- widthOffset: 20,
|
79
|
|
- shouldResizeWidth: false
|
|
73
|
+ heightOffset: 20
|
80
|
74
|
};
|
81
|
75
|
|
82
|
76
|
constructor(props) {
|
83
|
77
|
super(props);
|
84
|
|
- props.enableAnimation && (this.opacityAnimatedValue = new Animated.Value(0));
|
|
78
|
+ const { enableAnimation, style } = props;
|
|
79
|
+ enableAnimation && (this.opacityAnimatedValue = new Animated.Value(0));
|
|
80
|
+ this.webView = React.createRef();
|
85
|
81
|
isBelowKitKat && DeviceEventEmitter.addListener('webViewBridgeMessage', this.listenWebViewBridgeMessage);
|
86
|
82
|
this.state = {
|
87
|
83
|
isChangingSource: false,
|
88
|
84
|
height: 0,
|
89
|
|
- heightOffset: 0,
|
90
|
|
- width: screenWidth,
|
91
|
|
- widthOffset: 0,
|
92
|
|
- script: getScript(props, baseScript)
|
|
85
|
+ heightOffset: 0,
|
|
86
|
+ width: getWidth(style),
|
|
87
|
+ script: getScript(props, getBaseScript)
|
93
|
88
|
};
|
94
|
89
|
}
|
95
|
90
|
|
|
@@ -97,27 +92,25 @@ export default class AutoHeightWebView extends PureComponent {
|
97
|
92
|
this.startInterval();
|
98
|
93
|
}
|
99
|
94
|
|
100
|
|
- componentWillReceiveProps(nextProps) {
|
|
95
|
+ UNSAFE_componentWillReceiveProps(nextProps) {
|
101
|
96
|
// injectedJavaScript only works when webView reload (source changed)
|
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
|
|
- }
|
120
|
|
- this.setState({ script: getScript(nextProps, baseScript) });
|
|
97
|
+ if (Immutable.is(Immutable.fromJS(this.props.source), Immutable.fromJS(nextProps.source))) {
|
|
98
|
+ return;
|
|
99
|
+ } else {
|
|
100
|
+ this.setState(
|
|
101
|
+ {
|
|
102
|
+ isChangingSource: true,
|
|
103
|
+ height: 0,
|
|
104
|
+ heightOffset: 0,
|
|
105
|
+ width: 0
|
|
106
|
+ },
|
|
107
|
+ () => {
|
|
108
|
+ this.startInterval();
|
|
109
|
+ this.setState({ isChangingSource: false });
|
|
110
|
+ }
|
|
111
|
+ );
|
|
112
|
+ }
|
|
113
|
+ this.setState({ script: getScript(nextProps, getBaseScript) });
|
121
|
114
|
}
|
122
|
115
|
|
123
|
116
|
componentWillUnmount() {
|
|
@@ -160,29 +153,34 @@ export default class AutoHeightWebView extends PureComponent {
|
160
|
153
|
}
|
161
|
154
|
|
162
|
155
|
onMessage = e => {
|
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
|
|
- }
|
|
156
|
+ const { height, width } = JSON.parse(isBelowKitKat ? e.nativeEvent.message : e.nativeEvent.data);
|
|
157
|
+ const { height: oldHeight, width: oldWidth } = this.state;
|
|
158
|
+ if ((height && height !== oldHeight) || (width && width !== oldWidth)) {
|
|
159
|
+ const { enableAnimation, animationDuration, heightOffset, onSizeUpdated } = this.props;
|
|
160
|
+ enableAnimation && this.opacityAnimatedValue.setValue(0);
|
|
161
|
+ this.stopInterval();
|
|
162
|
+ this.setState(
|
|
163
|
+ {
|
|
164
|
+ heightOffset,
|
|
165
|
+ height,
|
|
166
|
+ width
|
|
167
|
+ },
|
|
168
|
+ () => {
|
|
169
|
+ if (enableAnimation) {
|
|
170
|
+ Animated.timing(this.opacityAnimatedValue, {
|
|
171
|
+ toValue: 1,
|
|
172
|
+ duration: animationDuration
|
|
173
|
+ }).start(() => {
|
|
174
|
+ handleSizeUpdated(height, width, onSizeUpdated);
|
|
175
|
+ });
|
|
176
|
+ } else {
|
|
177
|
+ handleSizeUpdated(height, width, onSizeUpdated);
|
|
178
|
+ }
|
|
179
|
+ }
|
|
180
|
+ );
|
|
181
|
+ }
|
|
182
|
+ const { onMessage } = this.props;
|
|
183
|
+ onMessage && onMessage(e);
|
186
|
184
|
};
|
187
|
185
|
|
188
|
186
|
onLoadingStart = event => {
|
|
@@ -203,8 +201,6 @@ export default class AutoHeightWebView extends PureComponent {
|
203
|
201
|
onLoadEnd && onLoadEnd(event);
|
204
|
202
|
};
|
205
|
203
|
|
206
|
|
- getWebView = webView => (this.webView = webView);
|
207
|
|
-
|
208
|
204
|
stopLoading() {
|
209
|
205
|
UIManager.dispatchViewManagerCommand(
|
210
|
206
|
findNodeHandle(this.webView),
|
|
@@ -214,23 +210,22 @@ export default class AutoHeightWebView extends PureComponent {
|
214
|
210
|
}
|
215
|
211
|
|
216
|
212
|
render() {
|
217
|
|
- const { height, width, script, isChangingSource, heightOffset, widthOffset } = this.state;
|
218
|
|
- const { scalesPageToFit, enableAnimation, source, customScript, style, enableBaseUrl } = this.props;
|
|
213
|
+ const { height, width, script, isChangingSource, heightOffset } = this.state;
|
|
214
|
+ const { scalesPageToFit, enableAnimation, source, customScript, style, enableBaseUrl, scrollEnabled } = this.props;
|
219
|
215
|
let webViewSource = source;
|
220
|
216
|
if (enableBaseUrl) {
|
221
|
217
|
webViewSource = Object.assign({}, source, {
|
222
|
218
|
baseUrl: 'file:///android_asset/web/'
|
223
|
219
|
});
|
224
|
220
|
}
|
225
|
|
-
|
226
|
221
|
return (
|
227
|
222
|
<Animated.View
|
228
|
223
|
style={[
|
229
|
224
|
styles.container,
|
230
|
225
|
{
|
231
|
226
|
opacity: enableAnimation ? this.opacityAnimatedValue : 1,
|
232
|
|
- height: height + heightOffset,
|
233
|
|
- width: width + widthOffset
|
|
227
|
+ height: height + heightOffset,
|
|
228
|
+ width: width
|
234
|
229
|
},
|
235
|
230
|
style
|
236
|
231
|
]}
|
|
@@ -240,11 +235,13 @@ export default class AutoHeightWebView extends PureComponent {
|
240
|
235
|
onLoadingStart={this.onLoadingStart}
|
241
|
236
|
onLoadingFinish={this.onLoadingFinish}
|
242
|
237
|
onLoadingError={this.onLoadingError}
|
243
|
|
- ref={this.getWebView}
|
|
238
|
+ originWhitelist={['*']}
|
|
239
|
+ ref={this.webView}
|
244
|
240
|
style={styles.webView}
|
245
|
241
|
javaScriptEnabled={true}
|
246
|
242
|
injectedJavaScript={script + customScript}
|
247
|
243
|
scalesPageToFit={scalesPageToFit}
|
|
244
|
+ scrollEnabled={!!scrollEnabled}
|
248
|
245
|
source={webViewSource}
|
249
|
246
|
onMessage={this.onMessage}
|
250
|
247
|
messagingEnabled={true}
|
|
@@ -261,7 +258,6 @@ const isBelowKitKat = Platform.Version < 19;
|
261
|
258
|
|
262
|
259
|
const styles = StyleSheet.create({
|
263
|
260
|
container: {
|
264
|
|
- //width: screenWidth,
|
265
|
261
|
backgroundColor: 'transparent'
|
266
|
262
|
},
|
267
|
263
|
webView: {
|
|
@@ -270,41 +266,42 @@ const styles = StyleSheet.create({
|
270
|
266
|
}
|
271
|
267
|
});
|
272
|
268
|
|
273
|
|
-const baseScript = isBelowKitKat
|
274
|
|
- ? `
|
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
|
|
-
|
283
|
|
- AutoHeightWebView.onMessage = function (message) {
|
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 }));
|
288
|
|
- };
|
289
|
|
- ${domMutationObserveScript}
|
290
|
|
- } ());
|
291
|
|
- `
|
292
|
|
- : `
|
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
|
|
-
|
301
|
|
- document.addEventListener('message', function (e) {
|
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 }));
|
306
|
|
- });
|
307
|
|
- ${domMutationObserveScript}
|
308
|
|
- } ());
|
309
|
|
- `;
|
310
|
|
-
|
|
269
|
+const getBaseScript = isBelowKitKat
|
|
270
|
+ ? function() {
|
|
271
|
+ return `
|
|
272
|
+ ; (function () {
|
|
273
|
+ var wrapper = document.createElement('div');
|
|
274
|
+ wrapper.id = 'wrapper';
|
|
275
|
+ while (document.body.firstChild instanceof Node) {
|
|
276
|
+ wrapper.appendChild(document.body.firstChild);
|
|
277
|
+ }
|
|
278
|
+ document.body.appendChild(wrapper);
|
|
279
|
+ AutoHeightWebView.onMessage = function (message) {
|
|
280
|
+ var rect = document.body.firstElementChild.getBoundingClientRect().toJSON();
|
|
281
|
+ var width = Math.round(rect.width);
|
|
282
|
+ var height = Math.round(rect.height);
|
|
283
|
+ AutoHeightWebView.send(JSON.stringify({ width, height }));
|
|
284
|
+ };
|
|
285
|
+ ${domMutationObserveScript}
|
|
286
|
+} ());
|
|
287
|
+ `;
|
|
288
|
+ }
|
|
289
|
+ : function() {
|
|
290
|
+ return `
|
|
291
|
+ ; (function () {
|
|
292
|
+ var wrapper = document.createElement('div');
|
|
293
|
+ wrapper.id = 'wrapper';
|
|
294
|
+ while (document.body.firstChild instanceof Node) {
|
|
295
|
+ wrapper.appendChild(document.body.firstChild);
|
|
296
|
+ }
|
|
297
|
+ document.body.appendChild(wrapper);
|
|
298
|
+ document.addEventListener('message', function (e) {
|
|
299
|
+ var rect = document.body.firstElementChild.getBoundingClientRect().toJSON();
|
|
300
|
+ var width = Math.round(rect.width);
|
|
301
|
+ var height = Math.round(rect.height);
|
|
302
|
+ window.postMessage(JSON.stringify({ width, height }));
|
|
303
|
+ });
|
|
304
|
+ ${domMutationObserveScript}
|
|
305
|
+} ());
|
|
306
|
+ `;
|
|
307
|
+ };
|