Sin descripción

index.ios.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. 'use strict'
  2. import React, { PureComponent } from 'react';
  3. import {
  4. Animated,
  5. Dimensions,
  6. StyleSheet,
  7. View,
  8. ViewPropTypes,
  9. WebView
  10. } from 'react-native';
  11. import PropTypes from 'prop-types';
  12. export default class AutoHeightWebView extends PureComponent {
  13. static propTypes = {
  14. hasIframe: PropTypes.bool,
  15. source: WebView.propTypes.source,
  16. onHeightUpdated: PropTypes.func,
  17. customScript: PropTypes.string,
  18. customStyle: PropTypes.string,
  19. enableAnimation: PropTypes.bool,
  20. // if set to true may cause some layout issues (smaller font size)
  21. scalesPageToFit: PropTypes.bool,
  22. // only works on enable animation
  23. animationDuration: PropTypes.number,
  24. // offset of rn webview margin
  25. heightOffset: PropTypes.number,
  26. style: ViewPropTypes.style,
  27. // add web/files... to project root
  28. files: PropTypes.arrayOf(PropTypes.shape({
  29. href: PropTypes.string,
  30. type: PropTypes.string,
  31. rel: PropTypes.string
  32. }))
  33. }
  34. static defaultProps = {
  35. scalesPageToFit: false,
  36. enableAnimation: true,
  37. animationDuration: 555,
  38. heightOffset: 12
  39. }
  40. constructor(props) {
  41. super(props);
  42. this.handleNavigationStateChange = this.handleNavigationStateChange.bind(this);
  43. if (this.props.enableAnimation) {
  44. this.opacityAnimatedValue = new Animated.Value(0);
  45. }
  46. let initialScript = props.hasIframe ? IframeBaseScript : BaseScript;
  47. initialScript = props.files
  48. ? this.appendFilesToHead(props.files, BaseScript)
  49. : BaseScript;
  50. initialScript = props.customStyle
  51. ? this.appendStylesToHead(props.customStyle, initialScript)
  52. : initialScript;
  53. this.state = {
  54. height: 0,
  55. script: initialScript
  56. };
  57. }
  58. componentWillReceiveProps(nextProps) {
  59. let currentScript = nextProps.hasIframe ? IframeBaseScript : BaseScript;
  60. if (nextProps.files) {
  61. currentScript = this.appendFilesToHead(nextProps.files, currentScript);
  62. }
  63. currentScript = nextProps.customStyle
  64. ? this.appendStylesToHead(nextProps.customStyle, currentScript)
  65. : currentScript;
  66. this.setState({ script: currentScript });
  67. }
  68. appendFilesToHead(files, script) {
  69. if (!files) {
  70. return script;
  71. }
  72. for (let file of files) {
  73. script =
  74. `
  75. var link = document.createElement('link');
  76. link.rel = '` + file.rel + `';
  77. link.type = '` + file.type + `';
  78. link.href = '` + file.href + `';
  79. document.head.appendChild(link);
  80. `+ script;
  81. }
  82. return script;
  83. }
  84. appendStylesToHead(styles, script) {
  85. if (!styles) {
  86. return script;
  87. }
  88. // Escape any single quotes or newlines in the CSS with .replace()
  89. const escaped = styles.replace(/\'/g, "\\'").replace(/\n/g, '\\n')
  90. return `
  91. var styleElement = document.createElement('style');
  92. var styleText = document.createTextNode('${escaped}');
  93. styleElement.appendChild(styleText);
  94. document.head.appendChild(styleElement);
  95. ${script}
  96. `
  97. }
  98. onHeightUpdated(height) {
  99. if (this.props.onHeightUpdated) {
  100. this.props.onHeightUpdated(height);
  101. }
  102. }
  103. handleNavigationStateChange(navState) {
  104. const height = Number(navState.title);
  105. if (height && height !== this.state.height) {
  106. if (this.props.enableAnimation) {
  107. this.opacityAnimatedValue.setValue(0);
  108. }
  109. this.setState({ height }, () => {
  110. if (this.props.enableAnimation) {
  111. Animated.timing(this.opacityAnimatedValue, {
  112. toValue: 1,
  113. duration: this.props.animationDuration
  114. }).start(() => this.onHeightUpdated(height));
  115. }
  116. else {
  117. this.onHeightUpdated(height);
  118. }
  119. });
  120. }
  121. }
  122. render() {
  123. const { height, script } = this.state;
  124. const { scalesPageToFit, enableAnimation, source, heightOffset, customScript, style } = this.props;
  125. const webViewSource = Object.assign({}, source, { baseUrl: 'web/' });
  126. return (
  127. <Animated.View style={[Styles.container, {
  128. opacity: enableAnimation ? this.opacityAnimatedValue : 1,
  129. height: height + heightOffset,
  130. }, style]}>
  131. <WebView
  132. style={Styles.webView}
  133. injectedJavaScript={script + customScript}
  134. scrollEnabled={false}
  135. scalesPageToFit={scalesPageToFit}
  136. source={webViewSource}
  137. onNavigationStateChange={this.handleNavigationStateChange} />
  138. </Animated.View>
  139. );
  140. }
  141. }
  142. const ScreenWidth = Dimensions.get('window').width;
  143. const Styles = StyleSheet.create({
  144. container: {
  145. width: ScreenWidth,
  146. backgroundColor: 'transparent'
  147. },
  148. webView: {
  149. flex: 1,
  150. backgroundColor: 'transparent'
  151. }
  152. });
  153. const BaseScript =
  154. `
  155. ;
  156. (function () {
  157. var i = 0;
  158. var height = 0;
  159. var wrapper = document.createElement('div');
  160. wrapper.id = 'height-wrapper';
  161. while (document.body.firstChild) {
  162. wrapper.appendChild(document.body.firstChild);
  163. }
  164. document.body.appendChild(wrapper);
  165. function updateHeight() {
  166. if(document.body.offsetHeight !== height) {
  167. height = wrapper.clientHeight;
  168. document.title = wrapper.clientHeight;
  169. window.location.hash = ++i;
  170. }
  171. }
  172. updateHeight();
  173. window.addEventListener('load', updateHeight);
  174. window.addEventListener('resize', updateHeight);
  175. } ());
  176. `;
  177. const IframeBaseScript =
  178. `
  179. ;
  180. (function () {
  181. var i = 0;
  182. var height = 0;
  183. function updateHeight() {
  184. if(document.body.offsetHeight !== height) {
  185. height = document.body.firstChild.clientHeight;
  186. document.title = document.body.firstChild.clientHeight;
  187. window.location.hash = ++i;
  188. }
  189. }
  190. updateHeight();
  191. window.addEventListener('load', updateHeight);
  192. window.addEventListener('resize', updateHeight);
  193. } ());
  194. `;