Нет описания

index.ios.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict'
  2. import React, {
  3. Component,
  4. PropTypes
  5. } from 'react';
  6. import {
  7. View,
  8. WebView
  9. } from 'react-native';
  10. export default class AutoHeightWebView extends Component {
  11. constructor(props) {
  12. super(props);
  13. this.handleNavigationStateChange = this.handleNavigationStateChange.bind(this);
  14. const initialScript = props.files ? this.appendFilesToHead(props.files, BaseScript) : BaseScript;
  15. this.state = {
  16. height: 0,
  17. script: initialScript
  18. };
  19. }
  20. componentWillReceiveProps(nextProps) {
  21. let currentScript = BaseScript;
  22. if ((nextProps.files && !this.props.files) || (nextProps.files && this.props.files && JSON.stringify(nextProps.files) !== JSON.stringify(this.props.files))) {
  23. currentScript = this.appendFilesToHead(nextProps.files, BaseScript);
  24. }
  25. this.setState({ script: currentScript });
  26. }
  27. appendFilesToHead(files, script) {
  28. if (!files) {
  29. return script;
  30. }
  31. for (let file of files) {
  32. script =
  33. `
  34. var link = document.createElement('link');
  35. link.rel = '` + file.rel + `';
  36. link.type = '` + file.type + `';
  37. link.href = '` + file.href + `';
  38. document.head.appendChild(link);
  39. `+ script;
  40. }
  41. return script;
  42. }
  43. handleNavigationStateChange(navState) {
  44. const height = Number(navState.title);
  45. if (height) {
  46. this.setState({ height });
  47. if (this.props.onHeightUpdated) {
  48. this.props.onHeightUpdated(height);
  49. }
  50. }
  51. }
  52. render() {
  53. return (
  54. <View style={[{
  55. height: this.state.height + this.props.heightOffset,
  56. }, this.props.style]}>
  57. <WebView
  58. injectedJavaScript={this.state.script + this.props.customScript}
  59. scrollEnabled={false}
  60. source={{
  61. html: this.props.html,
  62. baseUrl: 'web/'
  63. }}
  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. // offset rn webview margin
  74. heightOffset: PropTypes.number,
  75. style: View.propTypes.style,
  76. // add web/files... to project root
  77. files: PropTypes.arrayOf(PropTypes.shape({
  78. href: PropTypes.string,
  79. type: PropTypes.string,
  80. rel: PropTypes.string
  81. }))
  82. }
  83. AutoHeightWebView.defaultProps = {
  84. heightOffset: 12
  85. }
  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. `;