|
@@ -17,7 +17,8 @@ import {
|
17
|
17
|
isSizeChanged,
|
18
|
18
|
handleSizeUpdated,
|
19
|
19
|
domMutationObserveScript,
|
20
|
|
- getCurrentSize
|
|
20
|
+ getStateFromProps,
|
|
21
|
+ updateSizeWithMessage
|
21
|
22
|
} from './common.js';
|
22
|
23
|
|
23
|
24
|
import momoize from './momoize';
|
|
@@ -61,17 +62,7 @@ export default class AutoHeightWebView extends PureComponent {
|
61
|
62
|
getUpdatedState = momoize(setState, isEqual);
|
62
|
63
|
|
63
|
64
|
static getDerivedStateFromProps(props, state) {
|
64
|
|
- const { height: oldHeight, width: oldWidth } = state;
|
65
|
|
- const height = props.style ? props.style.height : null;
|
66
|
|
- const width = props.style ? props.style.width : null;
|
67
|
|
- if (isSizeChanged(height, oldHeight, width, oldWidth)) {
|
68
|
|
- return {
|
69
|
|
- height: height || oldHeight,
|
70
|
|
- width: width || oldWidth,
|
71
|
|
- isSizeChanged: true
|
72
|
|
- };
|
73
|
|
- }
|
74
|
|
- return null;
|
|
65
|
+ return getStateFromProps(props, state);
|
75
|
66
|
}
|
76
|
67
|
|
77
|
68
|
componentDidUpdate() {
|
|
@@ -92,16 +83,19 @@ export default class AutoHeightWebView extends PureComponent {
|
92
|
83
|
}
|
93
|
84
|
}
|
94
|
85
|
|
95
|
|
- handleNavigationStateChange = navState => {
|
96
|
|
- const { title } = navState;
|
97
|
|
- const { onNavigationStateChange } = this.props;
|
98
|
|
- if (!title) {
|
99
|
|
- onNavigationStateChange && onNavigationStateChange(navState);
|
|
86
|
+ onMessage = event => {
|
|
87
|
+ if (!event.nativeEvent) {
|
100
|
88
|
return;
|
101
|
89
|
}
|
102
|
|
- const [heightValue, widthValue] = title.split(',');
|
103
|
|
- const width = Number(widthValue);
|
104
|
|
- const height = Number(heightValue);
|
|
90
|
+ let data = {};
|
|
91
|
+ // Sometimes the message is invalid JSON, so we ignore that case
|
|
92
|
+ try {
|
|
93
|
+ data = JSON.parse(event.nativeEvent.data);
|
|
94
|
+ } catch (error) {
|
|
95
|
+ console.error(error);
|
|
96
|
+ return;
|
|
97
|
+ }
|
|
98
|
+ const { height, width } = data;
|
105
|
99
|
const { height: oldHeight, width: oldWidth } = this.state;
|
106
|
100
|
if (isSizeChanged(height, oldHeight, width, oldWidth)) {
|
107
|
101
|
this.props.enableAnimation && this.opacityAnimatedValue.setValue(0);
|
|
@@ -111,7 +105,8 @@ export default class AutoHeightWebView extends PureComponent {
|
111
|
105
|
width
|
112
|
106
|
});
|
113
|
107
|
}
|
114
|
|
- onNavigationStateChange && onNavigationStateChange(navState);
|
|
108
|
+ const { onMessage } = this.props;
|
|
109
|
+ onMessage && onMessage(event);
|
115
|
110
|
};
|
116
|
111
|
|
117
|
112
|
stopLoading() {
|
|
@@ -128,7 +123,7 @@ export default class AutoHeightWebView extends PureComponent {
|
128
|
123
|
decelerationRate,
|
129
|
124
|
allowsInlineMediaPlayback,
|
130
|
125
|
dataDetectorTypes,
|
131
|
|
- onMessage,
|
|
126
|
+ onNavigationStateChange,
|
132
|
127
|
onError,
|
133
|
128
|
onLoad,
|
134
|
129
|
onLoadStart,
|
|
@@ -161,7 +156,7 @@ export default class AutoHeightWebView extends PureComponent {
|
161
|
156
|
dataDetectorTypes={dataDetectorTypes}
|
162
|
157
|
originWhitelist={originWhitelist || ['*']}
|
163
|
158
|
ref={this.webView}
|
164
|
|
- onMessage={onMessage}
|
|
159
|
+ onMessage={this.onMessage}
|
165
|
160
|
onError={onError}
|
166
|
161
|
onLoad={onLoad}
|
167
|
162
|
onLoadStart={onLoadStart}
|
|
@@ -171,7 +166,7 @@ export default class AutoHeightWebView extends PureComponent {
|
171
|
166
|
scrollEnabled={!!scrollEnabled}
|
172
|
167
|
injectedJavaScript={script}
|
173
|
168
|
source={source}
|
174
|
|
- onNavigationStateChange={this.handleNavigationStateChange}
|
|
169
|
+ onNavigationStateChange={onNavigationStateChange}
|
175
|
170
|
/>
|
176
|
171
|
</Animated.View>
|
177
|
172
|
);
|
|
@@ -190,60 +185,40 @@ const styles = StyleSheet.create({
|
190
|
185
|
|
191
|
186
|
// add viewport setting to meta for WKWebView
|
192
|
187
|
const commonScript = `
|
193
|
|
- var meta = document.createElement('meta');
|
194
|
|
- meta.setAttribute('name', 'viewport');
|
195
|
|
- meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);
|
196
|
|
- updateSize();
|
197
|
|
- window.addEventListener('load', updateSize);
|
198
|
|
- window.addEventListener('resize', updateSize);
|
199
|
|
- `;
|
|
188
|
+var meta = document.createElement('meta');
|
|
189
|
+meta.setAttribute('name', 'viewport');
|
|
190
|
+meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);
|
|
191
|
+updateSize();
|
|
192
|
+window.addEventListener('load', updateSize);
|
|
193
|
+window.addEventListener('resize', updateSize);
|
|
194
|
+`;
|
200
|
195
|
|
201
|
196
|
function getBaseScript(style) {
|
202
|
197
|
return `
|
203
|
|
- ;
|
204
|
|
- ${getCurrentSize}
|
205
|
|
- (function () {
|
206
|
|
- if (!document.getElementById("rnahw-wrapper")) {
|
207
|
|
- var height = 0;
|
208
|
|
- var width = ${getWidth(style)};
|
209
|
|
- var wrapper = document.createElement('div');
|
210
|
|
- wrapper.id = 'rnahw-wrapper';
|
211
|
|
- while (document.body.firstChild instanceof Node) {
|
212
|
|
- wrapper.appendChild(document.body.firstChild);
|
213
|
|
- }
|
214
|
|
- document.body.appendChild(wrapper);
|
215
|
|
- function updateSize() {
|
216
|
|
- if (document.body.offsetHeight !== height || document.body.offsetWidth !== width) {
|
217
|
|
- var size = getSize(wrapper);
|
218
|
|
- height = size.height;
|
219
|
|
- width = size.width;
|
220
|
|
- document.title = height.toString() + ',' + width.toString();
|
221
|
|
- }
|
222
|
|
- }
|
223
|
|
- ${commonScript}
|
224
|
|
- ${domMutationObserveScript}
|
225
|
|
- }
|
226
|
|
- } ());
|
227
|
|
- `;
|
|
198
|
+ ;
|
|
199
|
+ (function () {
|
|
200
|
+ if (!document.getElementById("rnahw-wrapper")) {
|
|
201
|
+ var wrapper = document.createElement('div');
|
|
202
|
+ wrapper.id = 'rnahw-wrapper';
|
|
203
|
+ wrapper.appendChild(document.body.firstChild);
|
|
204
|
+ document.body.appendChild(wrapper);
|
|
205
|
+ }
|
|
206
|
+ var width = ${getWidth(style)};
|
|
207
|
+ ${updateSizeWithMessage('wrapper')}
|
|
208
|
+ ${commonScript}
|
|
209
|
+ ${domMutationObserveScript}
|
|
210
|
+ } ());
|
|
211
|
+ `;
|
228
|
212
|
}
|
229
|
213
|
|
230
|
214
|
function getIframeBaseScript(style) {
|
231
|
215
|
return `
|
232
|
|
- ;
|
233
|
|
- ${getCurrentSize}
|
234
|
|
- (function () {
|
235
|
|
- var height = 0;
|
236
|
|
- var width = ${getWidth(style)};
|
237
|
|
- function updateSize() {
|
238
|
|
- if(document.body.offsetHeight !== height || document.body.offsetWidth !== width) {
|
239
|
|
- var size = getSize(document.body.firstChild);
|
240
|
|
- height = size.height;
|
241
|
|
- width = size.width;
|
242
|
|
- document.title = height.toString() + ',' + width.toString();
|
243
|
|
- }
|
244
|
|
- }
|
245
|
|
- ${commonScript}
|
246
|
|
- ${domMutationObserveScript}
|
247
|
|
- } ());
|
248
|
|
- `;
|
|
216
|
+ ;
|
|
217
|
+ (function () {
|
|
218
|
+ var width = ${getWidth(style)};
|
|
219
|
+ ${updateSizeWithMessage('document.body.firstChild')}
|
|
220
|
+ ${commonScript}
|
|
221
|
+ ${domMutationObserveScript}
|
|
222
|
+ } ());
|
|
223
|
+ `;
|
249
|
224
|
}
|