暂无描述

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. // 500 -> min-width of #doc in newsletter
  121. viewportContent={`initial-scale=${Dimensions.get('window').width / 500}`}
  122. onError={onError}
  123. onLoad={onHeightLoad}
  124. onLoadStart={onHeightLoadStart}
  125. onLoadEnd={onHeightLoadEnd}
  126. onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
  127. onSizeUpdated={setHeightSize}
  128. source={{html: autoHeightHtml1}}
  129. customScript={heightScript}
  130. onMessage={onMessage}
  131. />
  132. <Text style={{padding: 5}}>
  133. height: {heightSize.height}, width: {heightSize.width}
  134. </Text>
  135. <AutoHeightWebView
  136. style={{
  137. marginTop: 15,
  138. }}
  139. enableBaseUrl
  140. customStyle={widthStyle}
  141. onError={onError}
  142. onLoad={onWidthLoad}
  143. onLoadStart={onWidthLoadStart}
  144. onLoadEnd={onWidthLoadEnd}
  145. onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
  146. onSizeUpdated={setWidthSize}
  147. allowFileAccessFromFileURLs={true}
  148. allowUniversalAccessFromFileURLs={true}
  149. source={{
  150. html: widthHtml,
  151. baseUrl:
  152. Platform.OS === 'android' ? 'file:///android_asset/' : 'web/',
  153. }}
  154. customScript={widthScript}
  155. />
  156. <Text style={{padding: 5}}>
  157. height: {widthSize.height}, width: {widthSize.width}
  158. </Text>
  159. <TouchableOpacity onPress={changeSource} style={styles.button}>
  160. <Text>change source</Text>
  161. </TouchableOpacity>
  162. <TouchableOpacity onPress={changeStyle} style={styles.button}>
  163. <Text>change style</Text>
  164. </TouchableOpacity>
  165. <TouchableOpacity
  166. onPress={changeScript}
  167. style={[styles.button, {marginBottom: 100}]}>
  168. <Text>change script</Text>
  169. </TouchableOpacity>
  170. </ScrollView>
  171. );
  172. };
  173. const styles = StyleSheet.create({
  174. button: {
  175. marginTop: 15,
  176. backgroundColor: 'aliceblue',
  177. borderRadius: 5,
  178. padding: 5,
  179. },
  180. });
  181. export default Explorer;