No Description

index.android.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. export default class AutoHeightWebView extends Component {
  12. constructor(props) {
  13. super(props);
  14. this.handleNavigationStateChange = this.handleNavigationStateChange.bind(this);
  15. const initialScript = props.files ? this.appendFilesToHead(props.files, BaseScript) : BaseScript;
  16. this.state = {
  17. height: 0,
  18. script: initialScript
  19. };
  20. }
  21. componentWillReceiveProps(nextProps) {
  22. let currentScript = BaseScript;
  23. if ((nextProps.files && !this.props.files) || (nextProps.files && this.props.files && JSON.stringify(nextProps.files) !== JSON.stringify(this.props.files))) {
  24. currentScript = this.appendFilesToHead(nextProps.files, BaseScript);
  25. }
  26. this.setState({ script: currentScript });
  27. }
  28. appendFilesToHead(files, script) {
  29. if (!files) {
  30. return script;
  31. }
  32. for (let file of files) {
  33. script =
  34. `
  35. var link = document.createElement('link');
  36. link.rel = '` + file.rel + `';
  37. link.type = '` + file.type + `';
  38. link.href = '` + file.href + `';
  39. document.head.appendChild(link);
  40. `+ script;
  41. }
  42. return script;
  43. }
  44. handleNavigationStateChange(navState) {
  45. const height = Number(navState.title);
  46. this.setState({ height });
  47. if (this.props.onHeightUpdated) {
  48. this.props.onHeightUpdated(height);
  49. }
  50. }
  51. render() {
  52. const source = this.props.enableBaseUrl ? {
  53. html: this.props.html,
  54. baseUrl: 'file:///android_asset/web/'
  55. } : { html: this.props.html };
  56. return (
  57. <View style={[{
  58. height: this.state.height + this.props.heightOffset
  59. }, this.props.style]}>
  60. <RCTAutoHeightWebView
  61. injectedJavaScript={this.state.script + this.props.customScript}
  62. scrollEnabled={false}
  63. source={source}
  64. onNavigationStateChange={this.handleNavigationStateChange} />
  65. </View>
  66. );
  67. }
  68. }
  69. AutoHeightWebView.propTypes = {
  70. html: PropTypes.string,
  71. onHeightUpdated: PropTypes.func,
  72. customScript: PropTypes.string,
  73. style: View.propTypes.style,
  74. // offset rn webview margin
  75. heightOffset: PropTypes.number,
  76. // baseUrl not work in android 4.3 or below version
  77. enableBaseUrl: PropTypes.bool,
  78. // add web/files... to android/app/src/assets/
  79. files: PropTypes.arrayOf(PropTypes.shape({
  80. href: PropTypes.string,
  81. type: PropTypes.string,
  82. rel: PropTypes.string
  83. }))
  84. }
  85. const RCTAutoHeightWebView = requireNativeComponent('RCTAutoHeightWebView', AutoHeightWebView);
  86. const BaseScript =
  87. `
  88. ; (function () {
  89. var wrapper = document.createElement('div');
  90. wrapper.id = 'height-wrapper';
  91. while (document.body.firstChild) {
  92. wrapper.appendChild(document.body.firstChild);
  93. }
  94. document.body.appendChild(wrapper);
  95. var i = 0;
  96. function updateHeight() {
  97. document.title = wrapper.clientHeight;
  98. window.location.hash = ++i;
  99. }
  100. updateHeight();
  101. window.addEventListener('load', function () {
  102. updateHeight();
  103. });
  104. window.addEventListener('resize', updateHeight);
  105. } ());
  106. `;