No Description

index.android.js 4.3KB

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