闭社主体 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.

133 lines
3.6 KiB

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