Nessuna descrizione

index.android.js 5.3KB

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