Nav apraksta

index.ios.js 6.8KB

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