Ingen beskrivning

App.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. 'use strict';
  2. import React, { Component } from 'react';
  3. import {
  4. ScrollView,
  5. StyleSheet,
  6. Text,
  7. TouchableOpacity,
  8. Platform,
  9. Linking
  10. } from 'react-native';
  11. import AutoHeightWebView from 'react-native-autoheight-webview';
  12. import {
  13. autoHeightHtml0,
  14. autoHeightHtml1,
  15. autoHeightScript,
  16. autoWidthHtml0,
  17. autoWidthHtml1,
  18. autoWidthScript,
  19. autoDetectLinkHtml,
  20. autoDetectLinkScript,
  21. style0,
  22. inlineBodyStyle
  23. } from './config';
  24. export default class Explorer extends Component {
  25. constructor(props) {
  26. super(props);
  27. this.state = {
  28. heightHtml: autoHeightHtml0,
  29. heightScript: null,
  30. heightStyle: null,
  31. heightSize: {
  32. height: 0,
  33. width: 0
  34. },
  35. widthHtml: autoWidthHtml0,
  36. widthScript: null,
  37. widthStyle: inlineBodyStyle,
  38. widthSize: {
  39. height: 0,
  40. width: 0
  41. }
  42. };
  43. }
  44. changeSource = () => {
  45. this.setState(prevState => ({
  46. widthHtml:
  47. prevState.widthHtml === autoWidthHtml0
  48. ? autoWidthHtml1
  49. : autoWidthHtml0,
  50. heightHtml:
  51. prevState.heightHtml === autoHeightHtml0
  52. ? autoHeightHtml1
  53. : autoHeightHtml0
  54. }));
  55. };
  56. changeStyle = () => {
  57. this.setState(prevState => ({
  58. widthStyle:
  59. prevState.widthStyle == inlineBodyStyle
  60. ? style0 + inlineBodyStyle
  61. : inlineBodyStyle,
  62. heightStyle: prevState.heightStyle == null ? style0 : null
  63. }));
  64. };
  65. changeScript = () => {
  66. this.setState(prevState => ({
  67. widthScript:
  68. prevState.widthScript !== autoWidthScript ? autoWidthScript : null,
  69. heightScript:
  70. prevState.heightScript !== autoHeightScript ? autoHeightScript : null
  71. }));
  72. };
  73. changeTryBrowser = () => {
  74. this.setState(_ => ({
  75. widthHtml: autoWidthHtml0,
  76. heightHtml: autoDetectLinkHtml,
  77. widthScript: autoDetectLinkScript,
  78. heightScript: autoDetectLinkScript
  79. }));
  80. };
  81. render() {
  82. const {
  83. heightHtml,
  84. heightSize,
  85. heightStyle,
  86. heightScript,
  87. widthHtml,
  88. widthSize,
  89. widthStyle,
  90. widthScript
  91. } = this.state;
  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. customStyle={heightStyle}
  104. onError={() => console.log('height on error')}
  105. onLoad={() => console.log('height on load')}
  106. onLoadStart={() => console.log('height on load start')}
  107. onLoadEnd={() => console.log('height on load end')}
  108. onShouldStartLoadWithRequest={result => {
  109. console.log(result);
  110. return true;
  111. }}
  112. onSizeUpdated={heightSize => this.setState({ heightSize })}
  113. source={{ html: heightHtml }}
  114. customScript={heightScript}
  115. onMessage={event => {
  116. console.log('onMessage', event.nativeEvent.data);
  117. let { data } = event.nativeEvent;
  118. // maybe parse stringified JSON
  119. try {
  120. data = JSON.parse(data);
  121. } catch (e) {
  122. console.log(e.message);
  123. }
  124. if (typeof data === 'object') {
  125. const { url } = data;
  126. // check if this message concerns us
  127. if (url && url.startsWith('http')) {
  128. Linking.openURL(url).catch(err =>
  129. console.error('An error occurred', err)
  130. );
  131. }
  132. }
  133. }}
  134. />
  135. <Text style={{ padding: 5 }}>
  136. height: {heightSize.height}, width: {heightSize.width}
  137. </Text>
  138. <AutoHeightWebView
  139. baseUrl={
  140. Platform.OS === 'android'
  141. ? 'file:///android_asset/webAssets/'
  142. : 'webAssets/'
  143. }
  144. style={{
  145. marginTop: 15
  146. }}
  147. enableBaseUrl
  148. files={[
  149. {
  150. href: 'demo.css',
  151. type: 'text/css',
  152. rel: 'stylesheet'
  153. }
  154. ]}
  155. customStyle={widthStyle}
  156. onError={() => console.log('width on error')}
  157. onLoad={() => console.log('width on load')}
  158. onLoadStart={() => console.log('width on load start')}
  159. onLoadEnd={() => console.log('width on load end')}
  160. onShouldStartLoadWithRequest={result => {
  161. console.log(result);
  162. return true;
  163. }}
  164. onSizeUpdated={widthSize => this.setState({ widthSize })}
  165. source={{ html: widthHtml }}
  166. customScript={widthScript}
  167. />
  168. <Text style={{ padding: 5 }}>
  169. height: {widthSize.height}, width: {widthSize.width}
  170. </Text>
  171. <TouchableOpacity onPress={this.changeSource} style={styles.button}>
  172. <Text>change source</Text>
  173. </TouchableOpacity>
  174. <TouchableOpacity onPress={this.changeStyle} style={styles.button}>
  175. <Text>change style</Text>
  176. </TouchableOpacity>
  177. <TouchableOpacity onPress={this.changeTryBrowser} style={styles.button}>
  178. <Text>try browser links</Text>
  179. </TouchableOpacity>
  180. <TouchableOpacity
  181. onPress={this.changeScript}
  182. style={[styles.button, { marginBottom: 100 }]}>
  183. <Text>change script</Text>
  184. </TouchableOpacity>
  185. </ScrollView>
  186. );
  187. }
  188. }
  189. const styles = StyleSheet.create({
  190. button: {
  191. marginTop: 15,
  192. backgroundColor: 'aliceblue',
  193. borderRadius: 5,
  194. padding: 5
  195. }
  196. });