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