Geen omschrijving

index.ios.js 7.0KB

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