설명 없음

index.android.js 6.4KB

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