No Description

index.android.js 5.2KB

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