Bez popisu

index.ios.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. return `
  89. var styleElement = document.createElement('style');
  90. var styleText = document.createTextNode('${styles}');
  91. styleElement.appendChild(styleText);
  92. document.head.appendChild(styleElement);
  93. ${script}
  94. `
  95. }
  96. onHeightUpdated(height) {
  97. if (this.props.onHeightUpdated) {
  98. this.props.onHeightUpdated(height);
  99. }
  100. }
  101. handleNavigationStateChange(navState) {
  102. const height = Number(navState.title);
  103. if (height && height !== this.state.height) {
  104. if (this.props.enableAnimation) {
  105. this.opacityAnimatedValue.setValue(0);
  106. }
  107. this.setState({ height }, () => {
  108. if (this.props.enableAnimation) {
  109. Animated.timing(this.opacityAnimatedValue, {
  110. toValue: 1,
  111. duration: this.props.animationDuration
  112. }).start(() => this.onHeightUpdated(height));
  113. }
  114. else {
  115. this.onHeightUpdated(height);
  116. }
  117. });
  118. }
  119. }
  120. render() {
  121. const { height, script } = this.state;
  122. const { scalesPageToFit, enableAnimation, source, heightOffset, customScript, style } = this.props;
  123. const webViewSource = Object.assign({}, source, { baseUrl: 'web/' });
  124. return (
  125. <Animated.View style={[Styles.container, {
  126. opacity: enableAnimation ? this.opacityAnimatedValue : 1,
  127. height: height + heightOffset,
  128. }, style]}>
  129. <WebView
  130. style={Styles.webView}
  131. injectedJavaScript={script + customScript}
  132. scrollEnabled={false}
  133. scalesPageToFit={scalesPageToFit}
  134. source={webViewSource}
  135. onNavigationStateChange={this.handleNavigationStateChange} />
  136. </Animated.View>
  137. );
  138. }
  139. }
  140. const ScreenWidth = Dimensions.get('window').width;
  141. const Styles = StyleSheet.create({
  142. container: {
  143. width: ScreenWidth,
  144. backgroundColor: 'transparent'
  145. },
  146. webView: {
  147. flex: 1,
  148. backgroundColor: 'transparent'
  149. }
  150. });
  151. const BaseScript =
  152. `
  153. ;
  154. (function () {
  155. var i = 0;
  156. var height = 0;
  157. var wrapper = document.createElement('div');
  158. wrapper.id = 'height-wrapper';
  159. while (document.body.firstChild) {
  160. wrapper.appendChild(document.body.firstChild);
  161. }
  162. document.body.appendChild(wrapper);
  163. function updateHeight() {
  164. if(document.body.offsetHeight !== height) {
  165. height = wrapper.clientHeight;
  166. document.title = wrapper.clientHeight;
  167. window.location.hash = ++i;
  168. }
  169. }
  170. updateHeight();
  171. window.addEventListener('load', updateHeight);
  172. window.addEventListener('resize', updateHeight);
  173. } ());
  174. `;
  175. const IframeBaseScript =
  176. `
  177. ;
  178. (function () {
  179. var i = 0;
  180. var height = 0;
  181. function updateHeight() {
  182. if(document.body.offsetHeight !== height) {
  183. height = document.body.firstChild.clientHeight;
  184. document.title = document.body.firstChild.clientHeight;
  185. window.location.hash = ++i;
  186. }
  187. }
  188. updateHeight();
  189. window.addEventListener('load', updateHeight);
  190. window.addEventListener('resize', updateHeight);
  191. } ());
  192. `;