暫無描述

index.android.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. console.log(this.state.height + this.props.heightOffset);
  71. return (
  72. <View style={[{
  73. height: this.state.height + this.props.heightOffset
  74. }, this.props.style]}>
  75. <RCTAutoHeightWebView
  76. style={{ flex: 1 }}
  77. javaScriptEnabled={true}
  78. injectedJavaScript={this.state.script + this.props.customScript}
  79. scrollEnabled={false}
  80. source={source}
  81. onLoadingStart={this.onLoadingStart}
  82. onLoadingFinish={this.onLoadingFinish} />
  83. </View>
  84. );
  85. }
  86. }
  87. AutoHeightWebView.propTypes = {
  88. ...WebView.propTypes,
  89. html: PropTypes.string,
  90. onHeightUpdated: PropTypes.func,
  91. customScript: PropTypes.string,
  92. // offset rn webview margin
  93. heightOffset: PropTypes.number,
  94. // baseUrl not work in android 4.3 or below version
  95. enableBaseUrl: PropTypes.bool,
  96. // works if set enableBaseUrl to true; add web/files... to android/app/src/assets/
  97. files: PropTypes.arrayOf(PropTypes.shape({
  98. href: PropTypes.string,
  99. type: PropTypes.string,
  100. rel: PropTypes.string
  101. }))
  102. }
  103. AutoHeightWebView.defaultProps = {
  104. enableBaseUrl: false,
  105. heightOffset: 25
  106. }
  107. const BaseScript =
  108. `
  109. ; (function () {
  110. var wrapper = document.createElement('div');
  111. wrapper.id = 'height-wrapper';
  112. while (document.body.firstChild) {
  113. wrapper.appendChild(document.body.firstChild);
  114. }
  115. document.body.appendChild(wrapper);
  116. var i = 0;
  117. function updateHeight() {
  118. document.title = wrapper.clientHeight;
  119. window.location.hash = ++i;
  120. }
  121. updateHeight();
  122. window.addEventListener('load', function () {
  123. updateHeight();
  124. });
  125. window.addEventListener('resize', updateHeight);
  126. } ());
  127. `;