설명 없음

App.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. import React, { Component } from 'react';
  3. import { ScrollView, StyleSheet, Text, TouchableOpacity } from 'react-native';
  4. import AutoHeightWebView from 'react-native-autoheight-webview';
  5. export default class Explorer extends Component {
  6. constructor(props) {
  7. super(props);
  8. this.html0 = `<p style="font-weight: 400;font-style: normal;font-size: 21px;line-height: 1.58;letter-spacing: -.003em;">Tags are great for describing the essence of your story in a single word or phrase, but stories are rarely about a single thing. <span style="background-color: transparent !important;background-image: linear-gradient(to bottom, rgba(146, 249, 190, 1), rgba(146, 249, 190, 1));">If I pen a story about moving across the country to start a new job in a car with my husband, two cats, a dog, and a tarantula, I wouldn’t only tag the piece with “moving”. I’d also use the tags “pets”, “marriage”, “career change”, and “travel tips”.</span></p>`;
  9. this.html1 = `Tags are great for describing the essence of your story in a single word or phrase, but stories are rarely about a single thing. If I pen a story about moving across the country to start a new job in a car with my husband, two cats, a dog, and a tarantula, I wouldn’t only tag the piece with “moving”. I’d also use the tags “pets”, “marriage”, “career change”, and “travel tips”.`;
  10. this.testStyle = `
  11. p {
  12. font-size: 25px !important;
  13. }
  14. `;
  15. this.script0 = `
  16. var styleElement = document.createElement('style');
  17. styleElement.innerHTML = '${this.testStyle.replace(/\'/g, "\\'").replace(/\n/g, '\\n')}';
  18. document.head.appendChild(styleElement)
  19. `;
  20. this.script1 = `document.body.style.background = 'cornflowerblue';`;
  21. // this.script1 = null;
  22. this.state = {
  23. html: this.html0,
  24. script: null,
  25. webViewStyle: null,
  26. size: {
  27. height: 0,
  28. width: 0
  29. }
  30. };
  31. }
  32. changeSource = () => {
  33. this.setState(prevState => ({
  34. html: prevState.html === this.html0 ? this.html1 : this.html0
  35. }));
  36. };
  37. changeStyle = () => {
  38. this.setState(prevState => ({
  39. webViewStyle: prevState.webViewStyle == null ? this.testStyle : null
  40. }));
  41. };
  42. changeScript = () => {
  43. // this.changeSource();
  44. this.setState(prevState => ({
  45. script: prevState.script === this.script0 ? this.script1 : this.script0
  46. }));
  47. };
  48. render() {
  49. const { html, size, webViewStyle, script } = this.state;
  50. return (
  51. <ScrollView
  52. style={{
  53. backgroundColor: 'lightyellow'
  54. }}
  55. contentContainerStyle={{
  56. justifyContent: 'center',
  57. alignItems: 'center'
  58. }}
  59. >
  60. <AutoHeightWebView
  61. customStyle={webViewStyle}
  62. onError={() => console.log('on error')}
  63. onLoad={() => console.log('on load')}
  64. onLoadStart={() => console.log('on load start')}
  65. onLoadEnd={() => console.log('on load end')}
  66. onShouldStartLoadWithRequest={result => {
  67. console.log(result);
  68. return true;
  69. }}
  70. onSizeUpdated={size => this.setState({ size })}
  71. source={{ html }}
  72. customScript={script}
  73. />
  74. <TouchableOpacity onPress={this.changeSource} style={styles.button}>
  75. <Text>change source</Text>
  76. </TouchableOpacity>
  77. <TouchableOpacity onPress={this.changeStyle} style={styles.button}>
  78. <Text>change style</Text>
  79. </TouchableOpacity>
  80. <TouchableOpacity onPress={this.changeScript} style={styles.button}>
  81. <Text>change script (have to change source to reload on android)</Text>
  82. </TouchableOpacity>
  83. <Text style={{ padding: 5 }}>
  84. height: {size.height}, width: {size.width}
  85. </Text>
  86. </ScrollView>
  87. );
  88. }
  89. }
  90. const styles = StyleSheet.create({
  91. button: {
  92. marginTop: 15,
  93. backgroundColor: 'aliceblue',
  94. borderRadius: 5,
  95. padding: 5
  96. }
  97. });