Няма описание

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. 'use strict';
  2. import React, { PureComponent } from 'react';
  3. import { Animated, Easing, StyleSheet, ViewPropTypes, WebView } from 'react-native';
  4. import PropTypes from 'prop-types';
  5. import {
  6. isEqual,
  7. setState,
  8. getWidth,
  9. isSizeChanged,
  10. handleSizeUpdated,
  11. domMutationObserveScript,
  12. getCurrentSize,
  13. getRenderSize
  14. } from './common.js';
  15. import momoize from './momoize';
  16. export default class AutoHeightWebView extends PureComponent {
  17. static propTypes = {
  18. hasIframe: PropTypes.bool,
  19. onNavigationStateChange: PropTypes.func,
  20. onMessage: PropTypes.func,
  21. source: WebView.propTypes.source,
  22. customScript: PropTypes.string,
  23. customStyle: PropTypes.string,
  24. enableAnimation: PropTypes.bool,
  25. style: ViewPropTypes.style,
  26. scrollEnabled: PropTypes.bool,
  27. // either height or width updated will trigger this
  28. onSizeUpdated: PropTypes.func,
  29. // if set to true may cause some layout issues (smaller font size)
  30. scalesPageToFit: PropTypes.bool,
  31. // only works on enable animation
  32. animationDuration: PropTypes.number,
  33. // offset of rn webview margin
  34. heightOffset: PropTypes.number,
  35. // rn WebView callback
  36. onError: PropTypes.func,
  37. onLoad: PropTypes.func,
  38. onLoadStart: PropTypes.func,
  39. onLoadEnd: PropTypes.func,
  40. onShouldStartLoadWithRequest: PropTypes.func,
  41. // add web/files... to project root
  42. files: PropTypes.arrayOf(
  43. PropTypes.shape({
  44. href: PropTypes.string,
  45. type: PropTypes.string,
  46. rel: PropTypes.string
  47. })
  48. )
  49. };
  50. static defaultProps = {
  51. scalesPageToFit: false,
  52. enableAnimation: true,
  53. animationDuration: 255,
  54. heightOffset: 12
  55. };
  56. constructor(props) {
  57. super(props);
  58. const { enableAnimation, style } = props;
  59. enableAnimation && (this.opacityAnimatedValue = new Animated.Value(0));
  60. this.webView = React.createRef();
  61. this.state = {
  62. isSizeChanged: false,
  63. width: getWidth(style),
  64. height: style && style.height ? style.height : 0
  65. };
  66. }
  67. getUpdatedState = momoize(setState, isEqual);
  68. static getDerivedStateFromProps(props, state) {
  69. const { height: oldHeight, width: oldWidth } = state;
  70. const height = props.style ? props.style.height : null;
  71. const width = props.style ? props.style.width : null;
  72. if (isSizeChanged(height, oldHeight, width, oldWidth)) {
  73. return {
  74. height,
  75. width,
  76. isSizeChanged: true
  77. };
  78. }
  79. return null;
  80. }
  81. componentDidUpdate() {
  82. const { height, width, isSizeChanged } = this.state;
  83. if (isSizeChanged) {
  84. const { enableAnimation, animationDuration, onSizeUpdated } = this.props;
  85. if (enableAnimation) {
  86. Animated.timing(this.opacityAnimatedValue, {
  87. toValue: 1,
  88. duration: animationDuration
  89. }).start(() => {
  90. handleSizeUpdated(height, width, onSizeUpdated);
  91. });
  92. } else {
  93. handleSizeUpdated(height, width, onSizeUpdated);
  94. }
  95. this.setState({ isSizeChanged: false });
  96. }
  97. }
  98. handleNavigationStateChange = navState => {
  99. const { title } = navState;
  100. const { onNavigationStateChange } = this.props;
  101. if (!title) {
  102. onNavigationStateChange && onNavigationStateChange();
  103. return;
  104. }
  105. const [heightValue, widthValue] = title.split(',');
  106. const width = Number(widthValue);
  107. const height = Number(heightValue);
  108. const { height: oldHeight, width: oldWidth } = this.state;
  109. if (isSizeChanged(height, oldHeight, width, oldWidth)) {
  110. this.props.enableAnimation && this.opacityAnimatedValue.setValue(0);
  111. this.setState({
  112. isSizeChanged: true,
  113. height,
  114. width
  115. });
  116. }
  117. onNavigationStateChange && onNavigationStateChange();
  118. };
  119. stopLoading() {
  120. this.webView.stopLoading();
  121. }
  122. render() {
  123. const { height, width } = this.state;
  124. const {
  125. onMessage,
  126. onError,
  127. onLoad,
  128. onLoadStart,
  129. onLoadEnd,
  130. onShouldStartLoadWithRequest,
  131. scalesPageToFit,
  132. enableAnimation,
  133. heightOffset,
  134. style,
  135. scrollEnabled
  136. } = this.props;
  137. const { source, script } = this.getUpdatedState(this.props, 'web/', getBaseScript, getIframeBaseScript);
  138. return (
  139. <Animated.View
  140. style={[
  141. styles.container,
  142. {
  143. opacity: enableAnimation ? this.opacityAnimatedValue : 1,
  144. width,
  145. height: height + heightOffset
  146. },
  147. style
  148. ]}
  149. >
  150. <WebView
  151. originWhitelist={['*']}
  152. ref={this.webView}
  153. onMessage={onMessage}
  154. onError={onError}
  155. onLoad={onLoad}
  156. onLoadStart={onLoadStart}
  157. onLoadEnd={onLoadEnd}
  158. onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
  159. style={styles.webView}
  160. scrollEnabled={!!scrollEnabled}
  161. scalesPageToFit={scalesPageToFit}
  162. injectedJavaScript={script}
  163. source={source}
  164. onNavigationStateChange={this.handleNavigationStateChange}
  165. />
  166. </Animated.View>
  167. );
  168. }
  169. }
  170. const styles = StyleSheet.create({
  171. container: {
  172. backgroundColor: 'transparent'
  173. },
  174. webView: {
  175. flex: 1,
  176. backgroundColor: 'transparent'
  177. }
  178. });
  179. const commonScript = `
  180. updateSize();
  181. window.addEventListener('load', updateSize);
  182. window.addEventListener('resize', updateSize);
  183. `;
  184. function getBaseScript(style) {
  185. return `
  186. ;
  187. ${getCurrentSize}
  188. (function () {
  189. var height = 0;
  190. var width = ${getWidth(style)};
  191. var wrapper = document.createElement('div');
  192. wrapper.id = 'rnahw-wrapper';
  193. while (document.body.firstChild instanceof Node) {
  194. wrapper.appendChild(document.body.firstChild);
  195. }
  196. document.body.appendChild(wrapper);
  197. function updateSize() {
  198. if(document.body.offsetHeight !== height || document.body.offsetWidth !== width) {
  199. var size = getSize(wrapper);
  200. height = size.height;
  201. width = size.width;
  202. document.title = height.toString() + ',' + width.toString();
  203. }
  204. }
  205. ${commonScript}
  206. ${domMutationObserveScript}
  207. } ());
  208. `;
  209. }
  210. function getIframeBaseScript(style) {
  211. return `
  212. ;
  213. ${getCurrentSize}
  214. (function () {
  215. var height = 0;
  216. var width = ${getWidth(style)};
  217. function updateSize() {
  218. if(document.body.offsetHeight !== height || document.body.offsetWidth !== width) {
  219. var size = getSize(document.body.firstChild);
  220. height = size.height;
  221. width = size.width;
  222. document.title = height.toString() + ',' + width.toString();
  223. }
  224. }
  225. ${commonScript}
  226. ${domMutationObserveScript}
  227. } ());
  228. `;
  229. }