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