No Description

index.android.js 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. 'use strict'
  2. import React, {
  3. Component,
  4. PropTypes
  5. } from 'react';
  6. import {
  7. findNodeHandle,
  8. requireNativeComponent,
  9. Animated,
  10. DeviceEventEmitter,
  11. Dimensions,
  12. Platform,
  13. UIManager,
  14. View,
  15. WebView
  16. } from 'react-native';
  17. import ImmutableComponent from 'react-immutable-component';
  18. import Immutable from 'immutable';
  19. const RCTAutoHeightWebView = requireNativeComponent('RCTAutoHeightWebView', AutoHeightWebView, { nativeOnly: { messagingEnabled: PropTypes.bool } });
  20. export default class AutoHeightWebView extends ImmutableComponent {
  21. constructor(props) {
  22. super(props);
  23. this.onMessage = this.onMessage.bind(this);
  24. if (this.props.enableAnimation) {
  25. this.opacityAnimatedValue = new Animated.Value(0);
  26. }
  27. if (IsBelowKitKat) {
  28. this.listenWebViewBridgeMessage = this.listenWebViewBridgeMessage.bind(this);
  29. }
  30. const initialScript = props.files ? this.appendFilesToHead(props.files, BaseScript) : BaseScript;
  31. this.state = {
  32. isChangingSource: false,
  33. height: 0,
  34. heightOffset: 0,
  35. script: initialScript
  36. };
  37. }
  38. componentWillMount() {
  39. if (IsBelowKitKat) {
  40. DeviceEventEmitter.addListener("webViewBridgeMessage", this.listenWebViewBridgeMessage);
  41. }
  42. }
  43. componentDidMount() {
  44. this.startInterval();
  45. }
  46. componentWillReceiveProps(nextProps) {
  47. // injectedJavaScript only works when webview reload (source changed)
  48. if (Immutable.is(Immutable.fromJS(this.props.source), Immutable.fromJS(nextProps.source))) {
  49. return;
  50. }
  51. else {
  52. this.setState({
  53. isChangingSource: true,
  54. height: 0,
  55. heightOffset: 0
  56. }, () => {
  57. this.startInterval();
  58. this.setState({ isChangingSource: false });
  59. });
  60. }
  61. let currentScript = BaseScript;
  62. if (nextProps.files) {
  63. currentScript = this.appendFilesToHead(nextProps.files, BaseScript);
  64. }
  65. this.setState({ script: currentScript });
  66. }
  67. componentWillUnmount() {
  68. this.stopInterval();
  69. if (IsBelowKitKat) {
  70. DeviceEventEmitter.removeListener("webViewBridgeMessage", this.listenWebViewBridgeMessage);
  71. }
  72. }
  73. // below kitkat
  74. listenWebViewBridgeMessage(body) {
  75. this.onMessage(body.message);
  76. }
  77. // below kitkat
  78. sendToWebView(message) {
  79. UIManager.dispatchViewManagerCommand(
  80. findNodeHandle(this.webview),
  81. UIManager.RCTAutoHeightWebView.Commands.sendToWebView,
  82. [String(message)]
  83. );
  84. }
  85. postMessage(data) {
  86. UIManager.dispatchViewManagerCommand(
  87. findNodeHandle(this.webview),
  88. UIManager.RCTAutoHeightWebView.Commands.postMessage,
  89. [String(data)]
  90. );
  91. };
  92. startInterval() {
  93. this.finishInterval = false;
  94. this.interval = setInterval(() => {
  95. if (!this.finishInterval) {
  96. IsBelowKitKat ? this.sendToWebView('getBodyHeight') : this.postMessage('getBodyHeight');
  97. }
  98. }, 205);
  99. }
  100. stopInterval() {
  101. this.finishInterval = true;
  102. clearInterval(this.interval);
  103. }
  104. onHeightUpdated(height) {
  105. if (this.props.onHeightUpdated) {
  106. this.props.onHeightUpdated(height);
  107. }
  108. }
  109. onMessage(e) {
  110. const height = parseInt(IsBelowKitKat ? e.nativeEvent.message : e.nativeEvent.data);
  111. if (height) {
  112. if (this.props.enableAnimation) {
  113. this.opacityAnimatedValue.setValue(0);
  114. }
  115. this.stopInterval();
  116. this.setState({
  117. heightOffset: this.props.heightOffset,
  118. height
  119. }, () => {
  120. if (this.props.enableAnimation) {
  121. Animated.timing(this.opacityAnimatedValue, {
  122. toValue: 1,
  123. duration: this.props.animationDuration
  124. }).start(() => this.onHeightUpdated(height));
  125. }
  126. else {
  127. this.onHeightUpdated(height);
  128. }
  129. });
  130. }
  131. }
  132. appendFilesToHead(files, script) {
  133. if (!files) {
  134. return script;
  135. }
  136. for (let file of files) {
  137. script =
  138. `
  139. var link = document.createElement('link');
  140. link.rel = '` + file.rel + `';
  141. link.type = '` + file.type + `';
  142. link.href = '` + file.href + `';
  143. document.head.appendChild(link);
  144. `+ script;
  145. }
  146. return script;
  147. }
  148. render() {
  149. const { height, script, isChangingSource, heightOffset } = this.state;
  150. const { enableAnimation, source, customScript, style, enableBaseUrl } = this.props;
  151. let webViewSource = source;
  152. if (enableBaseUrl) {
  153. webViewSource = Object.assign({}, source, { baseUrl: 'file:///android_asset/web/' });
  154. }
  155. return (
  156. <Animated.View style={[{
  157. opacity: enableAnimation ? this.opacityAnimatedValue : 1,
  158. width: ScreenWidth,
  159. height: height + heightOffset,
  160. backgroundColor: 'transparent'
  161. }, style]}>
  162. {
  163. isChangingSource ? null :
  164. <RCTAutoHeightWebView
  165. ref={webview => this.webview = webview}
  166. style={{
  167. flex: 1,
  168. backgroundColor: 'transparent'
  169. }}
  170. javaScriptEnabled={true}
  171. injectedJavaScript={script + customScript}
  172. scrollEnabled={false}
  173. source={webViewSource}
  174. onMessage={this.onMessage}
  175. messagingEnabled={true}
  176. // below kitkat
  177. onChange={this.onMessage} />
  178. }
  179. </Animated.View>
  180. );
  181. }
  182. }
  183. AutoHeightWebView.propTypes = {
  184. source: WebView.propTypes.source,
  185. onHeightUpdated: PropTypes.func,
  186. customScript: PropTypes.string,
  187. enableAnimation: PropTypes.bool,
  188. // only works on enable animation
  189. animationDuration: PropTypes.number,
  190. // offset of rn webview margin
  191. heightOffset: PropTypes.number,
  192. // baseUrl not work in android 4.3 or below version
  193. enableBaseUrl: PropTypes.bool,
  194. style: View.propTypes.style,
  195. // works if set enableBaseUrl to true; add web/files... to android/app/src/assets/
  196. files: PropTypes.arrayOf(PropTypes.shape({
  197. href: PropTypes.string,
  198. type: PropTypes.string,
  199. rel: PropTypes.string
  200. }))
  201. }
  202. AutoHeightWebView.defaultProps = {
  203. enableAnimation: true,
  204. animationDuration: 555,
  205. enableBaseUrl: false,
  206. heightOffset: 20
  207. }
  208. const ScreenWidth = Dimensions.get('window').width;
  209. const IsBelowKitKat = Platform.Version < 19;
  210. const BaseScript =
  211. IsBelowKitKat ?
  212. `
  213. ; (function () {
  214. AutoHeightWebView.onMessage = function (message) {
  215. AutoHeightWebView.send(String(document.body.offsetHeight));
  216. };
  217. } ());
  218. ` :
  219. `
  220. ; (function () {
  221. document.addEventListener('message', function (e) {
  222. window.postMessage(String(document.body.offsetHeight));
  223. });
  224. } ());
  225. `;