You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SharedDocument.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // @flow
  2. import React, { PureComponent } from 'react';
  3. import { SafeAreaView, View } from 'react-native';
  4. import { WebView } from 'react-native-webview';
  5. import type { Dispatch } from 'redux';
  6. import { ColorSchemeRegistry } from '../../../base/color-scheme';
  7. import { translate } from '../../../base/i18n';
  8. import { HeaderWithNavigation, LoadingIndicator, SlidingView } from '../../../base/react';
  9. import { connect } from '../../../base/redux';
  10. import { toggleDocument } from '../../actions';
  11. import { getSharedDocumentUrl } from '../../functions';
  12. import styles, { INDICATOR_COLOR } from './styles';
  13. /**
  14. * The type of the React {@code Component} props of {@code ShareDocument}.
  15. */
  16. type Props = {
  17. /**
  18. * URL for the shared document.
  19. */
  20. _documentUrl: string,
  21. /**
  22. * Color schemed style of the header component.
  23. */
  24. _headerStyles: Object,
  25. /**
  26. * True if the chat window should be rendered.
  27. */
  28. _isOpen: boolean,
  29. /**
  30. * The Redux dispatch function.
  31. */
  32. dispatch: Dispatch<any>,
  33. /**
  34. * Function to be used to translate i18n labels.
  35. */
  36. t: Function
  37. };
  38. /**
  39. * Implements a React native component that renders the shared document window.
  40. */
  41. class SharedDocument extends PureComponent<Props> {
  42. /**
  43. * Instantiates a new instance.
  44. *
  45. * @inheritdoc
  46. */
  47. constructor(props: Props) {
  48. super(props);
  49. this._onClose = this._onClose.bind(this);
  50. this._onError = this._onError.bind(this);
  51. this._renderLoading = this._renderLoading.bind(this);
  52. }
  53. /**
  54. * Implements React's {@link Component#render()}.
  55. *
  56. * @inheritdoc
  57. */
  58. render() {
  59. const { _documentUrl, _isOpen } = this.props;
  60. const webViewStyles = this._getWebViewStyles();
  61. return (
  62. <SlidingView
  63. onHide = { this._onClose }
  64. position = 'bottom'
  65. show = { _isOpen } >
  66. <View style = { styles.webViewWrapper }>
  67. <HeaderWithNavigation
  68. headerLabelKey = 'documentSharing.title'
  69. onPressBack = { this._onClose } />
  70. <SafeAreaView style = { webViewStyles }>
  71. <WebView
  72. onError = { this._onError }
  73. renderLoading = { this._renderLoading }
  74. source = {{ uri: _documentUrl }}
  75. startInLoadingState = { true } />
  76. </SafeAreaView>
  77. </View>
  78. </SlidingView>
  79. );
  80. }
  81. /**
  82. * Computes the styles required for the WebView component.
  83. *
  84. * @returns {Object}
  85. */
  86. _getWebViewStyles() {
  87. return {
  88. ...styles.webView,
  89. backgroundColor: this.props._headerStyles.screenHeader.backgroundColor
  90. };
  91. }
  92. _onClose: () => boolean
  93. /**
  94. * Closes the window.
  95. *
  96. * @returns {boolean}
  97. */
  98. _onClose() {
  99. const { _isOpen, dispatch } = this.props;
  100. if (_isOpen) {
  101. dispatch(toggleDocument());
  102. return true;
  103. }
  104. return false;
  105. }
  106. _onError: () => void;
  107. /**
  108. * Callback to handle the error if the page fails to load.
  109. *
  110. * @returns {void}
  111. */
  112. _onError() {
  113. const { _isOpen, dispatch } = this.props;
  114. if (_isOpen) {
  115. dispatch(toggleDocument());
  116. }
  117. }
  118. _renderLoading: () => React$Component<any>;
  119. /**
  120. * Renders the loading indicator.
  121. *
  122. * @returns {React$Component<any>}
  123. */
  124. _renderLoading() {
  125. return (
  126. <View style = { styles.indicatorWrapper }>
  127. <LoadingIndicator
  128. color = { INDICATOR_COLOR }
  129. size = 'large' />
  130. </View>
  131. );
  132. }
  133. }
  134. /**
  135. * Maps (parts of) the redux state to {@link SharedDocument} React {@code Component} props.
  136. *
  137. * @param {Object} state - The redux store/state.
  138. * @private
  139. * @returns {Object}
  140. */
  141. export function _mapStateToProps(state: Object) {
  142. const { editing } = state['features/etherpad'];
  143. const documentUrl = getSharedDocumentUrl(state);
  144. return {
  145. _documentUrl: documentUrl,
  146. _headerStyles: ColorSchemeRegistry.get(state, 'Header'),
  147. _isOpen: editing
  148. };
  149. }
  150. export default translate(connect(_mapStateToProps)(SharedDocument));