No Description

index.android.js 7.5KB

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