Sin descripción

index.android.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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) {
  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. // below kitkat
  73. listenWebViewBridgeMessage(body) {
  74. this.onMessage(body.message);
  75. }
  76. postMessage(data) {
  77. UIManager.dispatchViewManagerCommand(
  78. findNodeHandle(this.webview),
  79. UIManager.RCTAutoHeightWebView.Commands.postMessage,
  80. [String(data)]
  81. );
  82. };
  83. // below kitkat
  84. sendToWebView(message) {
  85. UIManager.dispatchViewManagerCommand(
  86. findNodeHandle(this.webview),
  87. UIManager.RCTAutoHeightWebView.Commands.sendToWebView,
  88. [String(message)]
  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. onMessage(e) {
  104. const height = parseInt(IsBelowKitKat ? e.nativeEvent.message : e.nativeEvent.data);
  105. if (height) {
  106. this.stopInterval();
  107. this.setState({
  108. heightOffset: this.props.heightOffset,
  109. height
  110. });
  111. if (this.props.onHeightUpdated) {
  112. this.props.onHeightUpdated(height);
  113. }
  114. }
  115. }
  116. appendFilesToHead(files, script) {
  117. if (!files) {
  118. return script;
  119. }
  120. for (let file of files) {
  121. script =
  122. `
  123. var link = document.createElement('link');
  124. link.rel = '` + file.rel + `';
  125. link.type = '` + file.type + `';
  126. link.href = '` + file.href + `';
  127. document.head.appendChild(link);
  128. `+ script;
  129. }
  130. return script;
  131. }
  132. render() {
  133. const source = this.props.enableBaseUrl ? {
  134. html: this.props.html,
  135. baseUrl: 'file:///android_asset/web/'
  136. } : { html: this.props.html };
  137. return (
  138. <View style={[{
  139. width: ScreenWidth,
  140. height: this.state.height + this.state.heightOffset
  141. }, this.props.style]}>
  142. {
  143. this.state.isChangingSource ? null :
  144. <RCTAutoHeightWebView
  145. ref={webview => this.webview = webview}
  146. style={{ flex: 1 }}
  147. javaScriptEnabled={true}
  148. injectedJavaScript={this.state.script + this.props.customScript}
  149. scrollEnabled={false}
  150. source={source}
  151. // below kitkat
  152. onChange={this.onMessage}
  153. onMessage={this.onMessage}
  154. messagingEnabled={true} />
  155. }
  156. </View>
  157. );
  158. }
  159. }
  160. AutoHeightWebView.propTypes = {
  161. ...WebView.propTypes,
  162. html: PropTypes.string,
  163. onHeightUpdated: PropTypes.func,
  164. customScript: PropTypes.string,
  165. // offset rn webview margin
  166. heightOffset: PropTypes.number,
  167. // baseUrl not work in android 4.3 or below version
  168. enableBaseUrl: PropTypes.bool,
  169. // works if set enableBaseUrl to true; add web/files... to android/app/src/assets/
  170. files: PropTypes.arrayOf(PropTypes.shape({
  171. href: PropTypes.string,
  172. type: PropTypes.string,
  173. rel: PropTypes.string
  174. }))
  175. }
  176. AutoHeightWebView.defaultProps = {
  177. enableBaseUrl: false,
  178. heightOffset: 20
  179. }
  180. const ScreenWidth = Dimensions.get('window').width;
  181. const IsBelowKitKat = Platform.Version < 19;
  182. const BaseScript =
  183. IsBelowKitKat ?
  184. `
  185. (function () {
  186. AutoHeightWebView.onMessage = function (message) {
  187. AutoHeightWebView.send(String(document.body.offsetHeight));
  188. };
  189. } ());
  190. ` :
  191. `
  192. ; (function () {
  193. document.addEventListener('message', function (e) {
  194. window.postMessage(String(document.body.offsetHeight));
  195. });
  196. } ());
  197. `;