|
@@ -2,14 +2,7 @@
|
2
|
2
|
|
3
|
3
|
import React, { Component } from 'react';
|
4
|
4
|
|
5
|
|
-import {
|
6
|
|
- ScrollView,
|
7
|
|
- StyleSheet,
|
8
|
|
- Text,
|
9
|
|
- TouchableOpacity,
|
10
|
|
- Platform,
|
11
|
|
- Linking
|
12
|
|
-} from 'react-native';
|
|
5
|
+import { ScrollView, StyleSheet, Text, TouchableOpacity, Platform, Linking } from 'react-native';
|
13
|
6
|
|
14
|
7
|
import AutoHeightWebView from 'react-native-autoheight-webview';
|
15
|
8
|
|
|
@@ -20,7 +13,6 @@ import {
|
20
|
13
|
autoWidthHtml0,
|
21
|
14
|
autoWidthHtml1,
|
22
|
15
|
autoWidthScript,
|
23
|
|
- autoDetectLinkHtml,
|
24
|
16
|
autoDetectLinkScript,
|
25
|
17
|
style0,
|
26
|
18
|
inlineBodyStyle
|
|
@@ -31,7 +23,7 @@ export default class Explorer extends Component {
|
31
|
23
|
super(props);
|
32
|
24
|
this.state = {
|
33
|
25
|
heightHtml: autoHeightHtml0,
|
34
|
|
- heightScript: null,
|
|
26
|
+ heightScript: autoDetectLinkScript,
|
35
|
27
|
heightStyle: null,
|
36
|
28
|
heightSize: {
|
37
|
29
|
height: 0,
|
|
@@ -49,42 +41,23 @@ export default class Explorer extends Component {
|
49
|
41
|
|
50
|
42
|
changeSource = () => {
|
51
|
43
|
this.setState(prevState => ({
|
52
|
|
- widthHtml:
|
53
|
|
- prevState.widthHtml === autoWidthHtml0
|
54
|
|
- ? autoWidthHtml1
|
55
|
|
- : autoWidthHtml0,
|
56
|
|
- heightHtml:
|
57
|
|
- prevState.heightHtml === autoHeightHtml0
|
58
|
|
- ? autoHeightHtml1
|
59
|
|
- : autoHeightHtml0
|
|
44
|
+ widthHtml: prevState.widthHtml === autoWidthHtml0 ? autoWidthHtml1 : autoWidthHtml0,
|
|
45
|
+ heightHtml: prevState.heightHtml === autoHeightHtml0 ? autoHeightHtml1 : autoHeightHtml0
|
60
|
46
|
}));
|
61
|
47
|
};
|
62
|
48
|
|
63
|
49
|
changeStyle = () => {
|
64
|
50
|
this.setState(prevState => ({
|
65
|
|
- widthStyle:
|
66
|
|
- prevState.widthStyle == inlineBodyStyle
|
67
|
|
- ? style0 + inlineBodyStyle
|
68
|
|
- : inlineBodyStyle,
|
|
51
|
+ widthStyle: prevState.widthStyle == inlineBodyStyle ? style0 + inlineBodyStyle : inlineBodyStyle,
|
69
|
52
|
heightStyle: prevState.heightStyle == null ? style0 : null
|
70
|
53
|
}));
|
71
|
54
|
};
|
72
|
55
|
|
73
|
56
|
changeScript = () => {
|
74
|
57
|
this.setState(prevState => ({
|
75
|
|
- widthScript:
|
76
|
|
- prevState.widthScript !== autoWidthScript ? autoWidthScript : null,
|
|
58
|
+ widthScript: prevState.widthScript !== autoWidthScript ? autoWidthScript : null,
|
77
|
59
|
heightScript:
|
78
|
|
- prevState.heightScript !== autoHeightScript ? autoHeightScript : null
|
79
|
|
- }));
|
80
|
|
- };
|
81
|
|
-
|
82
|
|
- changeTryBrowser = () => {
|
83
|
|
- this.setState(_ => ({
|
84
|
|
- widthHtml: autoWidthHtml0,
|
85
|
|
- heightHtml: autoDetectLinkHtml,
|
86
|
|
- widthScript: autoDetectLinkScript,
|
87
|
|
- heightScript: autoDetectLinkScript
|
|
60
|
+ prevState.heightScript !== autoDetectLinkScript ? autoDetectLinkScript : autoHeightScript + autoDetectLinkScript
|
88
|
61
|
}));
|
89
|
62
|
};
|
90
|
63
|
|
|
@@ -108,7 +81,8 @@ export default class Explorer extends Component {
|
108
|
81
|
contentContainerStyle={{
|
109
|
82
|
justifyContent: 'center',
|
110
|
83
|
alignItems: 'center'
|
111
|
|
- }}>
|
|
84
|
+ }}
|
|
85
|
+ >
|
112
|
86
|
<AutoHeightWebView
|
113
|
87
|
customStyle={heightStyle}
|
114
|
88
|
onError={() => console.log('height on error')}
|
|
@@ -124,22 +98,19 @@ export default class Explorer extends Component {
|
124
|
98
|
customScript={heightScript}
|
125
|
99
|
onMessage={event => {
|
126
|
100
|
console.log('onMessage', event.nativeEvent.data);
|
127
|
|
- let { data } = event.nativeEvent;
|
128
|
|
-
|
|
101
|
+ const { data } = event.nativeEvent;
|
|
102
|
+ let messageData;
|
129
|
103
|
// maybe parse stringified JSON
|
130
|
104
|
try {
|
131
|
|
- data = JSON.parse(data);
|
|
105
|
+ messageData = JSON.parse(data);
|
132
|
106
|
} catch (e) {
|
133
|
107
|
console.log(e.message);
|
134
|
108
|
}
|
135
|
|
-
|
136
|
|
- if (typeof data === 'object') {
|
137
|
|
- const { url } = data;
|
|
109
|
+ if (typeof messageData === 'object') {
|
|
110
|
+ const { url } = messageData;
|
138
|
111
|
// check if this message concerns us
|
139
|
112
|
if (url && url.startsWith('http')) {
|
140
|
|
- Linking.openURL(url).catch(err =>
|
141
|
|
- console.error('An error occurred', err)
|
142
|
|
- );
|
|
113
|
+ Linking.openURL(url).catch(error => console.error('An error occurred', error));
|
143
|
114
|
}
|
144
|
115
|
}
|
145
|
116
|
}}
|
|
@@ -148,11 +119,7 @@ export default class Explorer extends Component {
|
148
|
119
|
height: {heightSize.height}, width: {heightSize.width}
|
149
|
120
|
</Text>
|
150
|
121
|
<AutoHeightWebView
|
151
|
|
- baseUrl={
|
152
|
|
- Platform.OS === 'android'
|
153
|
|
- ? 'file:///android_asset/webAssets/'
|
154
|
|
- : 'webAssets/'
|
155
|
|
- }
|
|
122
|
+ baseUrl={Platform.OS === 'android' ? 'file:///android_asset/webAssets/' : 'webAssets/'}
|
156
|
123
|
style={{
|
157
|
124
|
marginTop: 15
|
158
|
125
|
}}
|
|
@@ -186,12 +153,7 @@ export default class Explorer extends Component {
|
186
|
153
|
<TouchableOpacity onPress={this.changeStyle} style={styles.button}>
|
187
|
154
|
<Text>change style</Text>
|
188
|
155
|
</TouchableOpacity>
|
189
|
|
- <TouchableOpacity onPress={this.changeTryBrowser} style={styles.button}>
|
190
|
|
- <Text>try browser links</Text>
|
191
|
|
- </TouchableOpacity>
|
192
|
|
- <TouchableOpacity
|
193
|
|
- onPress={this.changeScript}
|
194
|
|
- style={[styles.button, { marginBottom: 100 }]}>
|
|
156
|
+ <TouchableOpacity onPress={this.changeScript} style={[styles.button, { marginBottom: 100 }]}>
|
195
|
157
|
<Text>change script</Text>
|
196
|
158
|
</TouchableOpacity>
|
197
|
159
|
</ScrollView>
|