闭社主体 forked from https://github.com/tootsuite/mastodon
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.

157 lines
4.1 KiB

  1. import React from 'react';
  2. import ColumnsArea from './components/columns_area';
  3. import NotificationsContainer from './containers/notifications_container';
  4. import PropTypes from 'prop-types';
  5. import LoadingBarContainer from './containers/loading_bar_container';
  6. import HomeTimeline from '../home_timeline';
  7. import Compose from '../compose';
  8. import TabsBar from './components/tabs_bar';
  9. import ModalContainer from './containers/modal_container';
  10. import Notifications from '../notifications';
  11. import { connect } from 'react-redux';
  12. import { isMobile } from '../../is_mobile';
  13. import { debounce } from 'lodash';
  14. import { uploadCompose } from '../../actions/compose';
  15. import { refreshTimeline } from '../../actions/timelines';
  16. import { refreshNotifications } from '../../actions/notifications';
  17. import UploadArea from './components/upload_area';
  18. const noOp = () => false;
  19. class UI extends React.PureComponent {
  20. static propTypes = {
  21. dispatch: PropTypes.func.isRequired,
  22. children: PropTypes.node,
  23. };
  24. state = {
  25. width: window.innerWidth,
  26. draggingOver: false,
  27. };
  28. handleResize = () => {
  29. this.setState({ width: window.innerWidth });
  30. }
  31. handleDragEnter = (e) => {
  32. e.preventDefault();
  33. if (!this.dragTargets) {
  34. this.dragTargets = [];
  35. }
  36. if (this.dragTargets.indexOf(e.target) === -1) {
  37. this.dragTargets.push(e.target);
  38. }
  39. if (e.dataTransfer && e.dataTransfer.types.includes('Files')) {
  40. this.setState({ draggingOver: true });
  41. }
  42. }
  43. handleDragOver = (e) => {
  44. e.preventDefault();
  45. e.stopPropagation();
  46. try {
  47. e.dataTransfer.dropEffect = 'copy';
  48. } catch (err) {
  49. }
  50. return false;
  51. }
  52. handleDrop = (e) => {
  53. e.preventDefault();
  54. this.setState({ draggingOver: false });
  55. if (e.dataTransfer && e.dataTransfer.files.length === 1) {
  56. this.props.dispatch(uploadCompose(e.dataTransfer.files));
  57. }
  58. }
  59. handleDragLeave = (e) => {
  60. e.preventDefault();
  61. e.stopPropagation();
  62. this.dragTargets = this.dragTargets.filter(el => el !== e.target && this.node.contains(el));
  63. if (this.dragTargets.length > 0) {
  64. return;
  65. }
  66. this.setState({ draggingOver: false });
  67. }
  68. closeUploadModal = () => {
  69. this.setState({ draggingOver: false });
  70. }
  71. componentWillMount () {
  72. window.addEventListener('resize', this.handleResize, { passive: true });
  73. document.addEventListener('dragenter', this.handleDragEnter, false);
  74. document.addEventListener('dragover', this.handleDragOver, false);
  75. document.addEventListener('drop', this.handleDrop, false);
  76. document.addEventListener('dragleave', this.handleDragLeave, false);
  77. document.addEventListener('dragend', this.handleDragEnd, false);
  78. this.props.dispatch(refreshTimeline('home'));
  79. this.props.dispatch(refreshNotifications());
  80. }
  81. componentWillUnmount () {
  82. window.removeEventListener('resize', this.handleResize);
  83. document.removeEventListener('dragenter', this.handleDragEnter);
  84. document.removeEventListener('dragover', this.handleDragOver);
  85. document.removeEventListener('drop', this.handleDrop);
  86. document.removeEventListener('dragleave', this.handleDragLeave);
  87. document.removeEventListener('dragend', this.handleDragEnd);
  88. }
  89. setRef = (c) => {
  90. this.node = c;
  91. }
  92. render () {
  93. const { width, draggingOver } = this.state;
  94. const { children } = this.props;
  95. let mountedColumns;
  96. if (isMobile(width)) {
  97. mountedColumns = (
  98. <ColumnsArea>
  99. {children}
  100. </ColumnsArea>
  101. );
  102. } else {
  103. mountedColumns = (
  104. <ColumnsArea>
  105. <Compose withHeader={true} />
  106. <HomeTimeline shouldUpdateScroll={noOp} />
  107. <Notifications shouldUpdateScroll={noOp} />
  108. <div className="column__wrapper">{children}</div>
  109. </ColumnsArea>
  110. );
  111. }
  112. return (
  113. <div className='ui' ref={this.setRef}>
  114. <TabsBar />
  115. {mountedColumns}
  116. <NotificationsContainer />
  117. <LoadingBarContainer className="loading-bar" />
  118. <ModalContainer />
  119. <UploadArea active={draggingOver} onClose={this.closeUploadModal} />
  120. </div>
  121. );
  122. }
  123. }
  124. export default connect()(UI);