No Description

App.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. // set spacing on left/right
  93. const spacing = 30;
  94. // calculate new width of *real* content
  95. const screenWidth = Math.round(Dimensions.get('window').width) - 2 * spacing;
  96. // For example on iPhone 8, screenWidth will be 315
  97. // I temporarily hard-coded the width into the newsletter.js its viewport tag
  98. return (
  99. <ScrollView
  100. style={{
  101. paddingTop: 45,
  102. backgroundColor: 'lightyellow',
  103. }}
  104. contentContainerStyle={{
  105. justifyContent: 'center',
  106. alignItems: 'center',
  107. }}>
  108. <AutoHeightWebView
  109. scalesPageToFit
  110. scrollEnabled={false}
  111. scrollEnabledWithZoomedin={true}
  112. customStyle={`
  113. ${heightStyle}
  114. #rnahw-wrapper {
  115. padding: 0 ${spacing}px;
  116. width: ${screenWidth};
  117. box-sizing: border-box;
  118. }
  119. `}
  120. onError={onError}
  121. onLoad={onHeightLoad}
  122. onLoadStart={onHeightLoadStart}
  123. onLoadEnd={onHeightLoadEnd}
  124. onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
  125. onSizeUpdated={setHeightSize}
  126. source={{html: heightHtml}}
  127. customScript={heightScript}
  128. onMessage={onMessage}
  129. />
  130. <Text style={{padding: 5}}>
  131. height: {heightSize.height}, width: {heightSize.width}
  132. </Text>
  133. <AutoHeightWebView
  134. style={{
  135. marginTop: 15,
  136. }}
  137. enableBaseUrl
  138. customStyle={widthStyle}
  139. onError={onError}
  140. onLoad={onWidthLoad}
  141. onLoadStart={onWidthLoadStart}
  142. onLoadEnd={onWidthLoadEnd}
  143. onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
  144. onSizeUpdated={setWidthSize}
  145. allowFileAccessFromFileURLs={true}
  146. allowUniversalAccessFromFileURLs={true}
  147. source={{
  148. html: widthHtml,
  149. baseUrl:
  150. Platform.OS === 'android' ? 'file:///android_asset/' : 'web/',
  151. }}
  152. customScript={widthScript}
  153. />
  154. <Text style={{padding: 5}}>
  155. height: {widthSize.height}, width: {widthSize.width}
  156. </Text>
  157. <TouchableOpacity onPress={changeSource} style={styles.button}>
  158. <Text>change source</Text>
  159. </TouchableOpacity>
  160. <TouchableOpacity onPress={changeStyle} style={styles.button}>
  161. <Text>change style</Text>
  162. </TouchableOpacity>
  163. <TouchableOpacity
  164. onPress={changeScript}
  165. style={[styles.button, {marginBottom: 100}]}>
  166. <Text>change script</Text>
  167. </TouchableOpacity>
  168. </ScrollView>
  169. );
  170. };
  171. const styles = StyleSheet.create({
  172. button: {
  173. marginTop: 15,
  174. backgroundColor: 'aliceblue',
  175. borderRadius: 5,
  176. padding: 5,
  177. },
  178. });
  179. export default Explorer;