説明なし

index.ios.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. 'use strict'
  2. import React, { PureComponent } from 'react';
  3. import {
  4. Animated,
  5. Dimensions,
  6. StyleSheet,
  7. View,
  8. ViewPropTypes,
  9. WebView
  10. } from 'react-native';
  11. import PropTypes from 'prop-types';
  12. export default class AutoHeightWebView extends PureComponent {
  13. static propTypes = {
  14. source: WebView.propTypes.source,
  15. onHeightUpdated: PropTypes.func,
  16. customScript: PropTypes.string,
  17. enableAnimation: PropTypes.bool,
  18. // if set to true may cause some layout issues (smaller font size)
  19. scalesPageToFit: PropTypes.bool,
  20. // only works on enable animation
  21. animationDuration: PropTypes.number,
  22. // offset of rn webview margin
  23. heightOffset: PropTypes.number,
  24. style: ViewPropTypes.style,
  25. // add web/files... to project root
  26. files: PropTypes.arrayOf(PropTypes.shape({
  27. href: PropTypes.string,
  28. type: PropTypes.string,
  29. rel: PropTypes.string
  30. }))
  31. }
  32. static defaultProps = {
  33. scalesPageToFit: false,
  34. enableAnimation: true,
  35. animationDuration: 555,
  36. heightOffset: 12
  37. }
  38. constructor(props) {
  39. super(props);
  40. this.handleNavigationStateChange = this.handleNavigationStateChange.bind(this);
  41. if (this.props.enableAnimation) {
  42. this.opacityAnimatedValue = new Animated.Value(0);
  43. }
  44. const initialScript = props.files ? this.appendFilesToHead(props.files, BaseScript) : BaseScript;
  45. this.state = {
  46. height: 0,
  47. script: initialScript
  48. };
  49. }
  50. componentWillReceiveProps(nextProps) {
  51. let currentScript = BaseScript;
  52. if (nextProps.files) {
  53. currentScript = this.appendFilesToHead(nextProps.files, BaseScript);
  54. }
  55. this.setState({ script: currentScript });
  56. }
  57. appendFilesToHead(files, script) {
  58. if (!files) {
  59. return script;
  60. }
  61. for (let file of files) {
  62. script =
  63. `
  64. var link = document.createElement('link');
  65. link.rel = '` + file.rel + `';
  66. link.type = '` + file.type + `';
  67. link.href = '` + file.href + `';
  68. document.head.appendChild(link);
  69. `+ script;
  70. }
  71. return script;
  72. }
  73. onHeightUpdated(height) {
  74. if (this.props.onHeightUpdated) {
  75. this.props.onHeightUpdated(height);
  76. }
  77. }
  78. handleNavigationStateChange(navState) {
  79. const height = Number(navState.title);
  80. if (height && height !== this.state.height) {
  81. if (this.props.enableAnimation) {
  82. this.opacityAnimatedValue.setValue(0);
  83. }
  84. this.setState({ height }, () => {
  85. if (this.props.enableAnimation) {
  86. Animated.timing(this.opacityAnimatedValue, {
  87. toValue: 1,
  88. duration: this.props.animationDuration
  89. }).start(() => this.onHeightUpdated(height));
  90. }
  91. else {
  92. this.onHeightUpdated(height);
  93. }
  94. });
  95. }
  96. }
  97. render() {
  98. const { height, script } = this.state;
  99. const { scalesPageToFit, enableAnimation, source, heightOffset, customScript, style } = this.props;
  100. const webViewSource = Object.assign({}, source, { baseUrl: 'web/' });
  101. return (
  102. <Animated.View style={[Styles.container, {
  103. opacity: enableAnimation ? this.opacityAnimatedValue : 1,
  104. height: height + heightOffset,
  105. }, style]}>
  106. <WebView
  107. style={Styles.webView}
  108. injectedJavaScript={script + customScript}
  109. scrollEnabled={false}
  110. scalesPageToFit={scalesPageToFit}
  111. source={webViewSource}
  112. onNavigationStateChange={this.handleNavigationStateChange} />
  113. </Animated.View>
  114. );
  115. }
  116. }
  117. const ScreenWidth = Dimensions.get('window').width;
  118. const Styles = StyleSheet.create({
  119. container: {
  120. width: ScreenWidth,
  121. backgroundColor: 'transparent'
  122. },
  123. webView: {
  124. flex: 1,
  125. backgroundColor: 'transparent'
  126. }
  127. });
  128. const BaseScript =
  129. `
  130. ;
  131. (function () {
  132. var i = 0;
  133. var height = 0;
  134. var wrapper = document.createElement('div');
  135. wrapper.id = 'height-wrapper';
  136. while (document.body.firstChild) {
  137. wrapper.appendChild(document.body.firstChild);
  138. }
  139. document.body.appendChild(wrapper);
  140. function updateHeight() {
  141. if(document.body.offsetHeight !== height) {
  142. height = wrapper.clientHeight;
  143. document.title = wrapper.clientHeight;
  144. window.location.hash = ++i;
  145. }
  146. }
  147. updateHeight();
  148. window.addEventListener('load', updateHeight);
  149. window.addEventListener('resize', updateHeight);
  150. } ());
  151. `;