暂无描述

index.android.js 6.3KB

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