|
@@ -6,13 +6,18 @@ import { Animated, Dimensions, StyleSheet, ViewPropTypes, WebView } from 'react-
|
6
|
6
|
|
7
|
7
|
import PropTypes from 'prop-types';
|
8
|
8
|
|
9
|
|
-import { getScript, onHeightUpdated, domMutationObserveScript } from './common.js';
|
|
9
|
+import { getScript, onHeightUpdated, onWidthUpdated, onHeightWidthUpdated, domMutationObserveScript } from './common.js';
|
|
10
|
+
|
|
11
|
+const screenWidth = Dimensions.get('window').width;
|
10
|
12
|
|
11
|
13
|
export default class AutoHeightWebView extends PureComponent {
|
12
|
14
|
static propTypes = {
|
13
|
15
|
hasIframe: PropTypes.bool,
|
14
|
16
|
source: WebView.propTypes.source,
|
15
|
|
- onHeightUpdated: PropTypes.func,
|
|
17
|
+ onHeightUpdated: PropTypes.func,
|
|
18
|
+ onWidthUpdated: PropTypes.func,
|
|
19
|
+ onHeightWidthUpdated: PropTypes.func,
|
|
20
|
+ shouldResizeWidth: PropTypes.bool,
|
16
|
21
|
customScript: PropTypes.string,
|
17
|
22
|
customStyle: PropTypes.string,
|
18
|
23
|
enableAnimation: PropTypes.bool,
|
|
@@ -21,7 +26,8 @@ export default class AutoHeightWebView extends PureComponent {
|
21
|
26
|
// only works on enable animation
|
22
|
27
|
animationDuration: PropTypes.number,
|
23
|
28
|
// offset of rn webview margin
|
24
|
|
- heightOffset: PropTypes.number,
|
|
29
|
+ heightOffset: PropTypes.number,
|
|
30
|
+ widthOffset: PropTypes.number,
|
25
|
31
|
style: ViewPropTypes.style,
|
26
|
32
|
// rn WebView callback
|
27
|
33
|
onError: PropTypes.func,
|
|
@@ -43,14 +49,19 @@ export default class AutoHeightWebView extends PureComponent {
|
43
|
49
|
scalesPageToFit: false,
|
44
|
50
|
enableAnimation: true,
|
45
|
51
|
animationDuration: 555,
|
46
|
|
- heightOffset: 12
|
|
52
|
+ heightOffset: 12,
|
|
53
|
+ widthOffset: 12,
|
|
54
|
+ shouldResizeWidth: false
|
47
|
55
|
};
|
48
|
56
|
|
49
|
57
|
constructor(props) {
|
50
|
58
|
super(props);
|
51
|
59
|
props.enableAnimation && (this.opacityAnimatedValue = new Animated.Value(0));
|
52
|
60
|
this.state = {
|
53
|
|
- height: 0,
|
|
61
|
+ height: 0,
|
|
62
|
+ //heightOffset: 0, //?? I added this
|
|
63
|
+ width: screenWidth,
|
|
64
|
+ //widthOffset: 0,
|
54
|
65
|
script: getScript(props, baseScript, iframeBaseScript)
|
55
|
66
|
};
|
56
|
67
|
}
|
|
@@ -59,20 +70,22 @@ export default class AutoHeightWebView extends PureComponent {
|
59
|
70
|
this.setState({ script: getScript(nextProps, baseScript, iframeBaseScript) });
|
60
|
71
|
}
|
61
|
72
|
|
62
|
|
- handleNavigationStateChange = navState => {
|
63
|
|
- const height = Number(navState.title);
|
64
|
|
- const { enableAnimation, animationDuration } = this.props;
|
65
|
|
- if (height && height !== this.state.height) {
|
66
|
|
- enableAnimation && this.opacityAnimatedValue.setValue(0);
|
67
|
|
- this.setState({ height }, () => {
|
68
|
|
- enableAnimation
|
69
|
|
- ? Animated.timing(this.opacityAnimatedValue, {
|
70
|
|
- toValue: 1,
|
71
|
|
- duration: animationDuration
|
72
|
|
- }).start(() => onHeightUpdated(height, this.props))
|
73
|
|
- : onHeightUpdated(height, this.props);
|
74
|
|
- });
|
75
|
|
- }
|
|
73
|
+ handleNavigationStateChange = navState => {
|
|
74
|
+ var [width, height] = navState.title.split(',');
|
|
75
|
+ width = Number(width);
|
|
76
|
+ height = Number(height);
|
|
77
|
+ const { enableAnimation, animationDuration } = this.props;
|
|
78
|
+ if (height && height !== this.state.height) { // ??? add to logic ??? width && width !== this.state.width
|
|
79
|
+ enableAnimation && this.opacityAnimatedValue.setValue(0);
|
|
80
|
+ this.setState({ height, width }, () => {
|
|
81
|
+ enableAnimation
|
|
82
|
+ ? Animated.timing(this.opacityAnimatedValue, {
|
|
83
|
+ toValue: 1,
|
|
84
|
+ duration: animationDuration
|
|
85
|
+ }).start(() => onHeightWidthUpdated(height, width, this.props))
|
|
86
|
+ : onHeightWidthUpdated(height, width, this.props);
|
|
87
|
+ });
|
|
88
|
+ }
|
76
|
89
|
};
|
77
|
90
|
|
78
|
91
|
getWebView = webView => (this.webView = webView);
|
|
@@ -82,7 +95,7 @@ export default class AutoHeightWebView extends PureComponent {
|
82
|
95
|
}
|
83
|
96
|
|
84
|
97
|
render() {
|
85
|
|
- const { height, script } = this.state;
|
|
98
|
+ const { height, width, script } = this.state;
|
86
|
99
|
const {
|
87
|
100
|
onError,
|
88
|
101
|
onLoad,
|
|
@@ -93,6 +106,7 @@ export default class AutoHeightWebView extends PureComponent {
|
93
|
106
|
enableAnimation,
|
94
|
107
|
source,
|
95
|
108
|
heightOffset,
|
|
109
|
+ widthOffset,
|
96
|
110
|
customScript,
|
97
|
111
|
style
|
98
|
112
|
} = this.props;
|
|
@@ -103,7 +117,8 @@ export default class AutoHeightWebView extends PureComponent {
|
103
|
117
|
styles.container,
|
104
|
118
|
{
|
105
|
119
|
opacity: enableAnimation ? this.opacityAnimatedValue : 1,
|
106
|
|
- height: height + heightOffset
|
|
120
|
+ height: height + heightOffset,
|
|
121
|
+ width: width + widthOffset
|
107
|
122
|
},
|
108
|
123
|
style
|
109
|
124
|
]}
|
|
@@ -127,11 +142,8 @@ export default class AutoHeightWebView extends PureComponent {
|
127
|
142
|
}
|
128
|
143
|
}
|
129
|
144
|
|
130
|
|
-const screenWidth = Dimensions.get('window').width;
|
131
|
|
-
|
132
|
145
|
const styles = StyleSheet.create({
|
133
|
146
|
container: {
|
134
|
|
- width: screenWidth,
|
135
|
147
|
backgroundColor: 'transparent'
|
136
|
148
|
},
|
137
|
149
|
webView: {
|
|
@@ -141,36 +153,46 @@ const styles = StyleSheet.create({
|
141
|
153
|
});
|
142
|
154
|
|
143
|
155
|
const commonScript = `
|
144
|
|
- updateHeight();
|
145
|
|
- window.addEventListener('load', updateHeight);
|
146
|
|
- window.addEventListener('resize', updateHeight);
|
|
156
|
+ updateSize();
|
|
157
|
+ window.addEventListener('load', updateSize);
|
|
158
|
+ window.addEventListener('resize', updateSize);
|
147
|
159
|
`;
|
148
|
160
|
|
149
|
|
-const getHeight = `
|
|
161
|
+const _getter = `
|
150
|
162
|
function getHeight(height) {
|
151
|
163
|
if(height < 1) {
|
152
|
164
|
return document.body.offsetHeight;
|
153
|
165
|
}
|
154
|
166
|
return height;
|
155
|
167
|
}
|
|
168
|
+ function getWidth(width) {
|
|
169
|
+ if(width < 1) {
|
|
170
|
+ return document.body.clientWidth; // maybe should be .offsetWidth ??
|
|
171
|
+ }
|
|
172
|
+ return width;
|
|
173
|
+ }
|
156
|
174
|
`;
|
157
|
|
-
|
158
|
175
|
const baseScript = `
|
159
|
176
|
;
|
160
|
|
- ${getHeight}
|
|
177
|
+ ${_getter}
|
161
|
178
|
(function () {
|
162
|
179
|
var i = 0;
|
163
|
180
|
var height = 0;
|
|
181
|
+ var width = ${screenWidth};
|
164
|
182
|
var wrapper = document.createElement('div');
|
165
|
183
|
wrapper.id = 'height-wrapper';
|
166
|
184
|
while (document.body.firstChild instanceof Node) {
|
167
|
185
|
wrapper.appendChild(document.body.firstChild);
|
168
|
186
|
}
|
169
|
187
|
document.body.appendChild(wrapper);
|
170
|
|
- function updateHeight() {
|
171
|
|
- if(document.body.offsetHeight !== height) {
|
172
|
|
- height = getHeight(wrapper.clientHeight);
|
173
|
|
- document.title = height;
|
|
188
|
+ function updateSize() {
|
|
189
|
+ var rect = document.body.firstElementChild.getBoundingClientRect().toJSON();
|
|
190
|
+ var newWidth = Math.round(rect.width);
|
|
191
|
+ var newHeight = Math.round(rect.height);
|
|
192
|
+ if(newHeight !== height) {
|
|
193
|
+ //height = getHeight(wrapper.clientHeight);
|
|
194
|
+ //width = getWidth(wrapper.clientWidth);
|
|
195
|
+ document.title = newWidth + ',' + newHeight;
|
174
|
196
|
window.location.hash = ++i;
|
175
|
197
|
}
|
176
|
198
|
}
|
|
@@ -181,14 +203,19 @@ const baseScript = `
|
181
|
203
|
|
182
|
204
|
const iframeBaseScript = `
|
183
|
205
|
;
|
184
|
|
- ${getHeight}
|
|
206
|
+ ${_getter}
|
185
|
207
|
(function () {
|
186
|
208
|
var i = 0;
|
187
|
209
|
var height = 0;
|
188
|
|
- function updateHeight() {
|
189
|
|
- if(document.body.offsetHeight !== height) {
|
190
|
|
- height = getHeight(document.body.firstChild.clientHeight);
|
191
|
|
- document.title = height;
|
|
210
|
+ var width = ${screenWidth};
|
|
211
|
+ function updateSize() {
|
|
212
|
+ var rect = document.body.firstElementChild.getBoundingClientRect().toJSON();
|
|
213
|
+ var newWidth = Math.round(rect.width);
|
|
214
|
+ var newHeight = Math.round(rect.height);
|
|
215
|
+ if(newHeight !== height) {
|
|
216
|
+ //height = getHeight(document.body.firstChild.clientHeight);
|
|
217
|
+ //width = getWidth(document.body.firstChild.clientHeight);
|
|
218
|
+ document.title = newWidth + ',' + newHeight;
|
192
|
219
|
window.location.hash = ++i;
|
193
|
220
|
}
|
194
|
221
|
}
|