Nenhuma descrição

index.android.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. 'use strict'
  2. import React, {
  3. Component,
  4. PropTypes
  5. } from 'react';
  6. import {
  7. requireNativeComponent,
  8. View,
  9. WebView
  10. } from 'react-native';
  11. const RCTAutoHeightWebView = requireNativeComponent('RCTAutoHeightWebView', AutoHeightWebView);
  12. export default class AutoHeightWebView extends Component {
  13. constructor(props) {
  14. super(props);
  15. this.onLoadingStart = this.onLoadingStart.bind(this);
  16. this.onLoadingFinish = this.onLoadingStart.bind(this);
  17. this.handleNavigationStateChanged = this.handleNavigationStateChanged.bind(this);
  18. const initialScript = props.files ? this.appendFilesToHead(props.files, BaseScript) : BaseScript;
  19. this.state = {
  20. height: 0,
  21. script: initialScript
  22. };
  23. }
  24. componentWillReceiveProps(nextProps) {
  25. let currentScript = BaseScript;
  26. if ((nextProps.files && !this.props.files) || (nextProps.files && this.props.files && JSON.stringify(nextProps.files) !== JSON.stringify(this.props.files))) {
  27. currentScript = this.appendFilesToHead(nextProps.files, BaseScript);
  28. }
  29. this.setState({ script: currentScript });
  30. }
  31. onLoadingStart(event) {
  32. this.updateNavigationState(event);
  33. }
  34. onLoadingFinish(event) {
  35. this.updateNavigationState(event);
  36. }
  37. updateNavigationState(event) {
  38. this.handleNavigationStateChanged(event.nativeEvent);
  39. }
  40. appendFilesToHead(files, script) {
  41. if (!files) {
  42. return script;
  43. }
  44. for (let file of files) {
  45. script =
  46. `
  47. var link = document.createElement('link');
  48. link.rel = '` + file.rel + `';
  49. link.type = '` + file.type + `';
  50. link.href = '` + file.href + `';
  51. document.head.appendChild(link);
  52. `+ script;
  53. }
  54. return script;
  55. }
  56. handleNavigationStateChanged(navState) {
  57. const height = Number(navState.title);
  58. if (height) {
  59. this.setState({ height });
  60. if (this.props.onHeightUpdated) {
  61. this.props.onHeightUpdated(height);
  62. }
  63. }
  64. }
  65. render() {
  66. const source = this.props.enableBaseUrl ? {
  67. html: this.props.html,
  68. baseUrl: 'file:///android_asset/web/'
  69. } : { html: this.props.html };
  70. return (
  71. <View style={[{
  72. height: this.state.height + this.props.heightOffset
  73. }, this.props.style]}>
  74. <RCTAutoHeightWebView
  75. style={{ flex: 1 }}
  76. javaScriptEnabled={true}
  77. injectedJavaScript={this.state.script + this.props.customScript}
  78. scrollEnabled={false}
  79. source={source}
  80. onLoadingStart={this.onLoadingStart}
  81. onLoadingFinish={this.onLoadingFinish} />
  82. </View>
  83. );
  84. }
  85. }
  86. AutoHeightWebView.propTypes = {
  87. ...WebView.propTypes,
  88. html: PropTypes.string,
  89. onHeightUpdated: PropTypes.func,
  90. customScript: PropTypes.string,
  91. // offset rn webview margin
  92. heightOffset: PropTypes.number,
  93. // baseUrl not work in android 4.3 or below version
  94. enableBaseUrl: PropTypes.bool,
  95. // works if set enableBaseUrl to true; add web/files... to android/app/src/assets/
  96. files: PropTypes.arrayOf(PropTypes.shape({
  97. href: PropTypes.string,
  98. type: PropTypes.string,
  99. rel: PropTypes.string
  100. }))
  101. }
  102. AutoHeightWebView.defaultProps = {
  103. enableBaseUrl: false,
  104. heightOffset: 25
  105. }
  106. const BaseScript =
  107. `
  108. ; (function () {
  109. var wrapper = document.createElement('div');
  110. wrapper.id = 'height-wrapper';
  111. while (document.body.firstChild) {
  112. wrapper.appendChild(document.body.firstChild);
  113. }
  114. document.body.appendChild(wrapper);
  115. var i = 0;
  116. function updateHeight() {
  117. document.title = wrapper.clientHeight;
  118. window.location.hash = ++i;
  119. }
  120. updateHeight();
  121. window.addEventListener('load', function () {
  122. updateHeight();
  123. });
  124. window.addEventListener('resize', updateHeight);
  125. } ());
  126. `;