Ei kuvausta

index.ios.js 5.1KB

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