Açıklama Yok

index.ios.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. 'use strict';
  2. import React, { PureComponent } from 'react';
  3. import { Animated, Dimensions, StyleSheet, 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. enableAnimation && this.opacityAnimatedValue.setValue(0);
  58. this.setState({ height }, () => {
  59. enableAnimation
  60. ? Animated.timing(this.opacityAnimatedValue, {
  61. toValue: 1,
  62. duration: animationDuration
  63. }).start(() => onHeightUpdated(height, this.props))
  64. : onHeightUpdated(height, this.props);
  65. });
  66. }
  67. };
  68. getWebView = webView => (this.webView = webView);
  69. stopLoading() {
  70. this.webView.stopLoading();
  71. }
  72. render() {
  73. const { height, script } = this.state;
  74. const {
  75. onError,
  76. onLoad,
  77. onLoadStart,
  78. onLoadEnd,
  79. onShouldStartLoadWithRequest,
  80. scalesPageToFit,
  81. enableAnimation,
  82. source,
  83. heightOffset,
  84. customScript,
  85. style
  86. } = this.props;
  87. const webViewSource = Object.assign({}, source, { baseUrl: 'web/' });
  88. return (
  89. <Animated.View
  90. style={[
  91. styles.container,
  92. {
  93. opacity: enableAnimation ? this.opacityAnimatedValue : 1,
  94. height: height + heightOffset
  95. },
  96. style
  97. ]}
  98. >
  99. <WebView
  100. ref={this.getWebView}
  101. onError={onError}
  102. onLoad={onLoad}
  103. onLoadStart={onLoadStart}
  104. onLoadEnd={onLoadEnd}
  105. onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
  106. style={styles.webView}
  107. injectedJavaScript={script + customScript}
  108. scrollEnabled={false}
  109. scalesPageToFit={scalesPageToFit}
  110. source={webViewSource}
  111. onNavigationStateChange={this.handleNavigationStateChange}
  112. />
  113. </Animated.View>
  114. );
  115. }
  116. }
  117. const screenWidth = Dimensions.get('window').width;
  118. const styles = StyleSheet.create({
  119. container: {
  120. width: screenWidth,
  121. backgroundColor: 'transparent'
  122. },
  123. webView: {
  124. flex: 1,
  125. backgroundColor: 'transparent'
  126. }
  127. });
  128. const baseScript = `
  129. ;
  130. (function () {
  131. var i = 0;
  132. var height = 0;
  133. var wrapper = document.createElement('div');
  134. wrapper.id = 'height-wrapper';
  135. while (document.body.firstChild) {
  136. wrapper.appendChild(document.body.firstChild);
  137. }
  138. document.body.appendChild(wrapper);
  139. function updateHeight() {
  140. if(document.body.offsetHeight !== height) {
  141. height = wrapper.clientHeight;
  142. document.title = wrapper.clientHeight;
  143. window.location.hash = ++i;
  144. }
  145. }
  146. ${domMutationObserveScript}
  147. } ());
  148. `;
  149. const iframeBaseScript = `
  150. ;
  151. (function () {
  152. var i = 0;
  153. var height = 0;
  154. function updateHeight() {
  155. if(document.body.offsetHeight !== height) {
  156. height = document.body.firstChild.clientHeight;
  157. document.title = document.body.firstChild.clientHeight;
  158. window.location.hash = ++i;
  159. }
  160. }
  161. updateHeight();
  162. window.addEventListener('load', updateHeight);
  163. window.addEventListener('resize', updateHeight);
  164. ${domMutationObserveScript}
  165. } ());
  166. `;