Ingen beskrivning

App.js 5.3KB

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