Açıklama Yok

index.android.js 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. 'use strict'
  2. import React, { PureComponent } from 'react';
  3. import {
  4. findNodeHandle,
  5. requireNativeComponent,
  6. Animated,
  7. DeviceEventEmitter,
  8. Dimensions,
  9. StyleSheet,
  10. Platform,
  11. UIManager,
  12. View,
  13. ViewPropTypes,
  14. WebView
  15. } from 'react-native';
  16. import PropTypes from 'prop-types';
  17. import Immutable from 'immutable';
  18. const RCTAutoHeightWebView = requireNativeComponent('RCTAutoHeightWebView', AutoHeightWebView, { nativeOnly: { messagingEnabled: PropTypes.bool } });
  19. export default class AutoHeightWebView extends PureComponent {
  20. constructor(props) {
  21. super(props);
  22. this.onMessage = this.onMessage.bind(this);
  23. if (this.props.enableAnimation) {
  24. this.opacityAnimatedValue = new Animated.Value(0);
  25. }
  26. if (IsBelowKitKat) {
  27. this.listenWebViewBridgeMessage = this.listenWebViewBridgeMessage.bind(this);
  28. }
  29. const initialScript = props.files ? this.appendFilesToHead(props.files, BaseScript) : BaseScript;
  30. this.state = {
  31. isChangingSource: false,
  32. height: 0,
  33. heightOffset: 0,
  34. script: initialScript
  35. };
  36. }
  37. componentWillMount() {
  38. if (IsBelowKitKat) {
  39. DeviceEventEmitter.addListener("webViewBridgeMessage", this.listenWebViewBridgeMessage);
  40. }
  41. }
  42. componentDidMount() {
  43. this.startInterval();
  44. }
  45. componentWillReceiveProps(nextProps) {
  46. // injectedJavaScript only works when webview reload (source changed)
  47. if (Immutable.is(Immutable.fromJS(this.props.source), Immutable.fromJS(nextProps.source))) {
  48. return;
  49. }
  50. else {
  51. this.setState({
  52. isChangingSource: true,
  53. height: 0,
  54. heightOffset: 0
  55. }, () => {
  56. this.startInterval();
  57. this.setState({ isChangingSource: false });
  58. });
  59. }
  60. let currentScript = BaseScript;
  61. if (nextProps.files) {
  62. currentScript = this.appendFilesToHead(nextProps.files, BaseScript);
  63. }
  64. this.setState({ script: currentScript });
  65. }
  66. componentWillUnmount() {
  67. this.stopInterval();
  68. if (IsBelowKitKat) {
  69. DeviceEventEmitter.removeListener("webViewBridgeMessage", this.listenWebViewBridgeMessage);
  70. }
  71. }
  72. // below kitkat
  73. listenWebViewBridgeMessage(body) {
  74. this.onMessage(body.message);
  75. }
  76. // below kitkat
  77. sendToWebView(message) {
  78. UIManager.dispatchViewManagerCommand(
  79. findNodeHandle(this.webview),
  80. UIManager.RCTAutoHeightWebView.Commands.sendToWebView,
  81. [String(message)]
  82. );
  83. }
  84. postMessage(data) {
  85. UIManager.dispatchViewManagerCommand(
  86. findNodeHandle(this.webview),
  87. UIManager.RCTAutoHeightWebView.Commands.postMessage,
  88. [String(data)]
  89. );
  90. };
  91. startInterval() {
  92. this.finishInterval = false;
  93. this.interval = setInterval(() => {
  94. if (!this.finishInterval) {
  95. IsBelowKitKat ? this.sendToWebView('getBodyHeight') : this.postMessage('getBodyHeight');
  96. }
  97. }, 205);
  98. }
  99. stopInterval() {
  100. this.finishInterval = true;
  101. clearInterval(this.interval);
  102. }
  103. onHeightUpdated(height) {
  104. if (this.props.onHeightUpdated) {
  105. this.props.onHeightUpdated(height);
  106. }
  107. }
  108. onMessage(e) {
  109. const height = parseInt(IsBelowKitKat ? e.nativeEvent.message : e.nativeEvent.data);
  110. if (height) {
  111. if (this.props.enableAnimation) {
  112. this.opacityAnimatedValue.setValue(0);
  113. }
  114. this.stopInterval();
  115. this.setState({
  116. heightOffset: this.props.heightOffset,
  117. height
  118. }, () => {
  119. if (this.props.enableAnimation) {
  120. Animated.timing(this.opacityAnimatedValue, {
  121. toValue: 1,
  122. duration: this.props.animationDuration
  123. }).start(() => this.onHeightUpdated(height));
  124. }
  125. else {
  126. this.onHeightUpdated(height);
  127. }
  128. });
  129. }
  130. }
  131. appendFilesToHead(files, script) {
  132. if (!files) {
  133. return script;
  134. }
  135. for (let file of files) {
  136. script =
  137. `
  138. var link = document.createElement('link');
  139. link.rel = '` + file.rel + `';
  140. link.type = '` + file.type + `';
  141. link.href = '` + file.href + `';
  142. document.head.appendChild(link);
  143. `+ script;
  144. }
  145. return script;
  146. }
  147. render() {
  148. const { height, script, isChangingSource, heightOffset } = this.state;
  149. const { enableAnimation, source, customScript, style, enableBaseUrl } = this.props;
  150. let webViewSource = source;
  151. if (enableBaseUrl) {
  152. webViewSource = Object.assign({}, source, { baseUrl: 'file:///android_asset/web/' });
  153. }
  154. return (
  155. <Animated.View style={[Styles.container, {
  156. opacity: enableAnimation ? this.opacityAnimatedValue : 1,
  157. height: height + heightOffset,
  158. }, style]}>
  159. {
  160. isChangingSource ? null :
  161. <RCTAutoHeightWebView
  162. ref={webview => this.webview = webview}
  163. style={Styles.webView}
  164. javaScriptEnabled={true}
  165. injectedJavaScript={script + customScript}
  166. scrollEnabled={false}
  167. source={webViewSource}
  168. onMessage={this.onMessage}
  169. messagingEnabled={true}
  170. // below kitkat
  171. onChange={this.onMessage} />
  172. }
  173. </Animated.View>
  174. );
  175. }
  176. }
  177. AutoHeightWebView.propTypes = {
  178. source: WebView.propTypes.source,
  179. onHeightUpdated: PropTypes.func,
  180. customScript: PropTypes.string,
  181. enableAnimation: PropTypes.bool,
  182. // only works on enable animation
  183. animationDuration: PropTypes.number,
  184. // offset of rn webview margin
  185. heightOffset: PropTypes.number,
  186. // baseUrl not work in android 4.3 or below version
  187. enableBaseUrl: PropTypes.bool,
  188. style: ViewPropTypes.style,
  189. // works if set enableBaseUrl to true; add web/files... to android/app/src/assets/
  190. files: PropTypes.arrayOf(PropTypes.shape({
  191. href: PropTypes.string,
  192. type: PropTypes.string,
  193. rel: PropTypes.string
  194. }))
  195. }
  196. AutoHeightWebView.defaultProps = {
  197. enableAnimation: true,
  198. animationDuration: 555,
  199. enableBaseUrl: false,
  200. heightOffset: 20
  201. }
  202. const ScreenWidth = Dimensions.get('window').width;
  203. const IsBelowKitKat = Platform.Version < 19;
  204. const Styles = StyleSheet.create({
  205. container: {
  206. width: ScreenWidth,
  207. backgroundColor: 'transparent'
  208. },
  209. webView: {
  210. flex: 1,
  211. backgroundColor: 'transparent'
  212. }
  213. });
  214. const BaseScript =
  215. IsBelowKitKat ?
  216. `
  217. ; (function () {
  218. AutoHeightWebView.onMessage = function (message) {
  219. AutoHeightWebView.send(String(document.body.offsetHeight));
  220. };
  221. } ());
  222. ` :
  223. `
  224. ; (function () {
  225. document.addEventListener('message', function (e) {
  226. window.postMessage(String(document.body.offsetHeight));
  227. });
  228. } ());
  229. `;