|
@@ -1,6 +1,13 @@
|
1
|
|
-import React, { useState } from 'react';
|
|
1
|
+import React, {useState} from 'react';
|
2
|
2
|
|
3
|
|
-import { ScrollView, StyleSheet, Text, TouchableOpacity, Platform, Linking } from 'react-native';
|
|
3
|
+import {
|
|
4
|
+ ScrollView,
|
|
5
|
+ StyleSheet,
|
|
6
|
+ Text,
|
|
7
|
+ TouchableOpacity,
|
|
8
|
+ Platform,
|
|
9
|
+ Linking,
|
|
10
|
+} from 'react-native';
|
4
|
11
|
|
5
|
12
|
import AutoHeightWebView from 'react-native-autoheight-webview';
|
6
|
13
|
|
|
@@ -13,7 +20,7 @@ import {
|
13
|
20
|
autoWidthScript,
|
14
|
21
|
autoDetectLinkScript,
|
15
|
22
|
style0,
|
16
|
|
- inlineBodyStyle
|
|
23
|
+ inlineBodyStyle,
|
17
|
24
|
} from './config';
|
18
|
25
|
|
19
|
26
|
const onShouldStartLoadWithRequest = result => {
|
|
@@ -21,10 +28,11 @@ const onShouldStartLoadWithRequest = result => {
|
21
|
28
|
return true;
|
22
|
29
|
};
|
23
|
30
|
|
24
|
|
-const onError = ({ nativeEvent }) => console.error('WebView error: ', nativeEvent);
|
|
31
|
+const onError = ({nativeEvent}) =>
|
|
32
|
+ console.error('WebView error: ', nativeEvent);
|
25
|
33
|
|
26
|
34
|
const onMessage = event => {
|
27
|
|
- const { data } = event.nativeEvent;
|
|
35
|
+ const {data} = event.nativeEvent;
|
28
|
36
|
let messageData;
|
29
|
37
|
// maybe parse stringified JSON
|
30
|
38
|
try {
|
|
@@ -33,10 +41,12 @@ const onMessage = event => {
|
33
|
41
|
console.log(e.message);
|
34
|
42
|
}
|
35
|
43
|
if (typeof messageData === 'object') {
|
36
|
|
- const { url } = messageData;
|
|
44
|
+ const {url} = messageData;
|
37
|
45
|
// check if this message concerns us
|
38
|
46
|
if (url && url.startsWith('http')) {
|
39
|
|
- Linking.openURL(url).catch(error => console.error('An error occurred', error));
|
|
47
|
+ Linking.openURL(url).catch(error =>
|
|
48
|
+ console.error('An error occurred', error),
|
|
49
|
+ );
|
40
|
50
|
}
|
41
|
51
|
}
|
42
|
52
|
};
|
|
@@ -54,51 +64,56 @@ const onWidthLoad = () => console.log('width on load');
|
54
|
64
|
const onWidthLoadEnd = () => console.log('width on load end');
|
55
|
65
|
|
56
|
66
|
const Explorer = () => {
|
57
|
|
- const [{ widthHtml, heightHtml }, setHtml] = useState({
|
|
67
|
+ const [{widthHtml, heightHtml}, setHtml] = useState({
|
58
|
68
|
widthHtml: autoWidthHtml0,
|
59
|
|
- heightHtml: autoHeightHtml0
|
|
69
|
+ heightHtml: autoHeightHtml0,
|
60
|
70
|
});
|
61
|
71
|
const changeSource = () =>
|
62
|
72
|
setHtml({
|
63
|
73
|
widthHtml: widthHtml === autoWidthHtml0 ? autoWidthHtml1 : autoWidthHtml0,
|
64
|
|
- heightHtml: heightHtml === autoHeightHtml0 ? autoHeightHtml1 : autoHeightHtml0
|
|
74
|
+ heightHtml:
|
|
75
|
+ heightHtml === autoHeightHtml0 ? autoHeightHtml1 : autoHeightHtml0,
|
65
|
76
|
});
|
66
|
77
|
|
67
|
|
- const [{ widthStyle, heightStyle }, setStyle] = useState({
|
|
78
|
+ const [{widthStyle, heightStyle}, setStyle] = useState({
|
68
|
79
|
heightStyle: null,
|
69
|
|
- widthStyle: inlineBodyStyle
|
|
80
|
+ widthStyle: inlineBodyStyle,
|
70
|
81
|
});
|
71
|
82
|
const changeStyle = () =>
|
72
|
83
|
setStyle({
|
73
|
|
- widthStyle: widthStyle === inlineBodyStyle ? style0 + inlineBodyStyle : inlineBodyStyle,
|
74
|
|
- heightStyle: heightStyle === null ? style0 : null
|
|
84
|
+ widthStyle:
|
|
85
|
+ widthStyle === inlineBodyStyle
|
|
86
|
+ ? style0 + inlineBodyStyle
|
|
87
|
+ : inlineBodyStyle,
|
|
88
|
+ heightStyle: heightStyle === null ? style0 : null,
|
75
|
89
|
});
|
76
|
90
|
|
77
|
|
- const [{ widthScript, heightScript }, setScript] = useState({
|
|
91
|
+ const [{widthScript, heightScript}, setScript] = useState({
|
78
|
92
|
heightScript: autoDetectLinkScript,
|
79
|
|
- widthScript: null
|
|
93
|
+ widthScript: null,
|
80
|
94
|
});
|
81
|
95
|
const changeScript = () =>
|
82
|
96
|
setScript({
|
83
|
97
|
widthScript: widthScript == autoWidthScript ? autoWidthScript : null,
|
84
|
98
|
heightScript:
|
85
|
|
- heightScript !== autoDetectLinkScript ? autoDetectLinkScript : autoHeightScript + autoDetectLinkScript
|
|
99
|
+ heightScript !== autoDetectLinkScript
|
|
100
|
+ ? autoDetectLinkScript
|
|
101
|
+ : autoHeightScript + autoDetectLinkScript,
|
86
|
102
|
});
|
87
|
103
|
|
88
|
|
- const [heightSize, setHeightSize] = useState({ height: 0, width: 0 });
|
89
|
|
- const [widthSize, setWidthSize] = useState({ height: 0, width: 0 });
|
|
104
|
+ const [heightSize, setHeightSize] = useState({height: 0, width: 0});
|
|
105
|
+ const [widthSize, setWidthSize] = useState({height: 0, width: 0});
|
90
|
106
|
|
91
|
107
|
return (
|
92
|
108
|
<ScrollView
|
93
|
109
|
style={{
|
94
|
110
|
paddingTop: 45,
|
95
|
|
- backgroundColor: 'lightyellow'
|
|
111
|
+ backgroundColor: 'lightyellow',
|
96
|
112
|
}}
|
97
|
113
|
contentContainerStyle={{
|
98
|
114
|
justifyContent: 'center',
|
99
|
|
- alignItems: 'center'
|
100
|
|
- }}
|
101
|
|
- >
|
|
115
|
+ alignItems: 'center',
|
|
116
|
+ }}>
|
102
|
117
|
<AutoHeightWebView
|
103
|
118
|
customStyle={heightStyle}
|
104
|
119
|
onError={onError}
|
|
@@ -107,26 +122,18 @@ const Explorer = () => {
|
107
|
122
|
onLoadEnd={onHeightLoadEnd}
|
108
|
123
|
onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
|
109
|
124
|
onSizeUpdated={setHeightSize}
|
110
|
|
- source={{ html: heightHtml }}
|
|
125
|
+ source={{html: heightHtml}}
|
111
|
126
|
customScript={heightScript}
|
112
|
127
|
onMessage={onMessage}
|
113
|
128
|
/>
|
114
|
|
- <Text style={{ padding: 5 }}>
|
|
129
|
+ <Text style={{padding: 5}}>
|
115
|
130
|
height: {heightSize.height}, width: {heightSize.width}
|
116
|
131
|
</Text>
|
117
|
132
|
<AutoHeightWebView
|
118
|
|
- baseUrl={Platform.OS === 'android' ? 'file:///android_asset/webAssets/' : 'webAssets/'}
|
119
|
133
|
style={{
|
120
|
|
- marginTop: 15
|
|
134
|
+ marginTop: 15,
|
121
|
135
|
}}
|
122
|
136
|
enableBaseUrl
|
123
|
|
- files={[
|
124
|
|
- {
|
125
|
|
- href: 'demo.css',
|
126
|
|
- type: 'text/css',
|
127
|
|
- rel: 'stylesheet'
|
128
|
|
- }
|
129
|
|
- ]}
|
130
|
137
|
customStyle={widthStyle}
|
131
|
138
|
onError={onError}
|
132
|
139
|
onLoad={onWidthLoad}
|
|
@@ -134,10 +141,16 @@ const Explorer = () => {
|
134
|
141
|
onLoadEnd={onWidthLoadEnd}
|
135
|
142
|
onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
|
136
|
143
|
onSizeUpdated={setWidthSize}
|
137
|
|
- source={{ html: widthHtml }}
|
|
144
|
+ allowFileAccessFromFileURLs={true}
|
|
145
|
+ allowUniversalAccessFromFileURLs={true}
|
|
146
|
+ source={{
|
|
147
|
+ html: widthHtml,
|
|
148
|
+ baseUrl:
|
|
149
|
+ Platform.OS === 'android' ? 'file:///android_asset/' : 'web/',
|
|
150
|
+ }}
|
138
|
151
|
customScript={widthScript}
|
139
|
152
|
/>
|
140
|
|
- <Text style={{ padding: 5 }}>
|
|
153
|
+ <Text style={{padding: 5}}>
|
141
|
154
|
height: {widthSize.height}, width: {widthSize.width}
|
142
|
155
|
</Text>
|
143
|
156
|
<TouchableOpacity onPress={changeSource} style={styles.button}>
|
|
@@ -146,7 +159,9 @@ const Explorer = () => {
|
146
|
159
|
<TouchableOpacity onPress={changeStyle} style={styles.button}>
|
147
|
160
|
<Text>change style</Text>
|
148
|
161
|
</TouchableOpacity>
|
149
|
|
- <TouchableOpacity onPress={changeScript} style={[styles.button, { marginBottom: 100 }]}>
|
|
162
|
+ <TouchableOpacity
|
|
163
|
+ onPress={changeScript}
|
|
164
|
+ style={[styles.button, {marginBottom: 100}]}>
|
150
|
165
|
<Text>change script</Text>
|
151
|
166
|
</TouchableOpacity>
|
152
|
167
|
</ScrollView>
|
|
@@ -158,8 +173,8 @@ const styles = StyleSheet.create({
|
158
|
173
|
marginTop: 15,
|
159
|
174
|
backgroundColor: 'aliceblue',
|
160
|
175
|
borderRadius: 5,
|
161
|
|
- padding: 5
|
162
|
|
- }
|
|
176
|
+ padding: 5,
|
|
177
|
+ },
|
163
|
178
|
});
|
164
|
179
|
|
165
|
180
|
export default Explorer;
|