The code powering m.abunchtell.com https://m.abunchtell.com
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

450 Zeilen
14 KiB

  1. import classNames from 'classnames';
  2. import React from 'react';
  3. import NotificationsContainer from './containers/notifications_container';
  4. import PropTypes from 'prop-types';
  5. import LoadingBarContainer from './containers/loading_bar_container';
  6. import TabsBar from './components/tabs_bar';
  7. import ModalContainer from './containers/modal_container';
  8. import { connect } from 'react-redux';
  9. import { Redirect, withRouter } from 'react-router-dom';
  10. import { isMobile } from '../../is_mobile';
  11. import { debounce } from 'lodash';
  12. import { uploadCompose, resetCompose } from '../../actions/compose';
  13. import { expandHomeTimeline } from '../../actions/timelines';
  14. import { expandNotifications } from '../../actions/notifications';
  15. import { clearHeight } from '../../actions/height_cache';
  16. import { WrappedSwitch, WrappedRoute } from './util/react_router_helpers';
  17. import UploadArea from './components/upload_area';
  18. import ColumnsAreaContainer from './containers/columns_area_container';
  19. import {
  20. Compose,
  21. Status,
  22. GettingStarted,
  23. KeyboardShortcuts,
  24. PublicTimeline,
  25. CommunityTimeline,
  26. AccountTimeline,
  27. AccountGallery,
  28. HomeTimeline,
  29. Followers,
  30. Following,
  31. Reblogs,
  32. Favourites,
  33. HashtagTimeline,
  34. Notifications,
  35. FollowRequests,
  36. GenericNotFound,
  37. FavouritedStatuses,
  38. ListTimeline,
  39. Blocks,
  40. Mutes,
  41. PinnedStatuses,
  42. Lists,
  43. } from './util/async-components';
  44. import { HotKeys } from 'react-hotkeys';
  45. import { me } from '../../initial_state';
  46. import { defineMessages, injectIntl } from 'react-intl';
  47. // Dummy import, to make sure that <Status /> ends up in the application bundle.
  48. // Without this it ends up in ~8 very commonly used bundles.
  49. import '../../components/status';
  50. const messages = defineMessages({
  51. beforeUnload: { id: 'ui.beforeunload', defaultMessage: 'Your draft will be lost if you leave Mastodon.' },
  52. });
  53. const mapStateToProps = state => ({
  54. isComposing: state.getIn(['compose', 'is_composing']),
  55. hasComposingText: state.getIn(['compose', 'text']) !== '',
  56. dropdownMenuIsOpen: state.getIn(['dropdown_menu', 'openId']) !== null,
  57. });
  58. const keyMap = {
  59. help: '?',
  60. new: 'n',
  61. search: 's',
  62. forceNew: 'option+n',
  63. focusColumn: ['1', '2', '3', '4', '5', '6', '7', '8', '9'],
  64. reply: 'r',
  65. favourite: 'f',
  66. boost: 'b',
  67. mention: 'm',
  68. open: ['enter', 'o'],
  69. openProfile: 'p',
  70. moveDown: ['down', 'j'],
  71. moveUp: ['up', 'k'],
  72. back: 'backspace',
  73. goToHome: 'g h',
  74. goToNotifications: 'g n',
  75. goToLocal: 'g l',
  76. goToFederated: 'g t',
  77. goToStart: 'g s',
  78. goToFavourites: 'g f',
  79. goToPinned: 'g p',
  80. goToProfile: 'g u',
  81. goToBlocked: 'g b',
  82. goToMuted: 'g m',
  83. };
  84. class SwitchingColumnsArea extends React.PureComponent {
  85. static propTypes = {
  86. children: PropTypes.node,
  87. location: PropTypes.object,
  88. onLayoutChange: PropTypes.func.isRequired,
  89. };
  90. state = {
  91. mobile: isMobile(window.innerWidth),
  92. };
  93. componentWillMount () {
  94. window.addEventListener('resize', this.handleResize, { passive: true });
  95. }
  96. componentDidUpdate (prevProps) {
  97. if (![this.props.location.pathname, '/'].includes(prevProps.location.pathname)) {
  98. this.node.handleChildrenContentChange();
  99. }
  100. }
  101. componentWillUnmount () {
  102. window.removeEventListener('resize', this.handleResize);
  103. }
  104. handleResize = debounce(() => {
  105. // The cached heights are no longer accurate, invalidate
  106. this.props.onLayoutChange();
  107. this.setState({ mobile: isMobile(window.innerWidth) });
  108. }, 500, {
  109. trailing: true,
  110. });
  111. setRef = c => {
  112. this.node = c.getWrappedInstance().getWrappedInstance();
  113. }
  114. render () {
  115. const { children } = this.props;
  116. const { mobile } = this.state;
  117. return (
  118. <ColumnsAreaContainer ref={this.setRef} singleColumn={mobile}>
  119. <WrappedSwitch>
  120. <Redirect from='/' to='/getting-started' exact />
  121. <WrappedRoute path='/getting-started' component={GettingStarted} content={children} />
  122. <WrappedRoute path='/keyboard-shortcuts' component={KeyboardShortcuts} content={children} />
  123. <WrappedRoute path='/timelines/home' component={HomeTimeline} content={children} />
  124. <WrappedRoute path='/timelines/public' exact component={PublicTimeline} content={children} />
  125. <WrappedRoute path='/timelines/public/local' component={CommunityTimeline} content={children} />
  126. <WrappedRoute path='/timelines/tag/:id' component={HashtagTimeline} content={children} />
  127. <WrappedRoute path='/timelines/list/:id' component={ListTimeline} content={children} />
  128. <WrappedRoute path='/notifications' component={Notifications} content={children} />
  129. <WrappedRoute path='/favourites' component={FavouritedStatuses} content={children} />
  130. <WrappedRoute path='/pinned' component={PinnedStatuses} content={children} />
  131. <WrappedRoute path='/statuses/new' component={Compose} content={children} />
  132. <WrappedRoute path='/statuses/:statusId' exact component={Status} content={children} />
  133. <WrappedRoute path='/statuses/:statusId/reblogs' component={Reblogs} content={children} />
  134. <WrappedRoute path='/statuses/:statusId/favourites' component={Favourites} content={children} />
  135. <WrappedRoute path='/accounts/:accountId' exact component={AccountTimeline} content={children} />
  136. <WrappedRoute path='/accounts/:accountId/with_replies' component={AccountTimeline} content={children} componentParams={{ withReplies: true }} />
  137. <WrappedRoute path='/accounts/:accountId/followers' component={Followers} content={children} />
  138. <WrappedRoute path='/accounts/:accountId/following' component={Following} content={children} />
  139. <WrappedRoute path='/accounts/:accountId/media' component={AccountGallery} content={children} />
  140. <WrappedRoute path='/follow_requests' component={FollowRequests} content={children} />
  141. <WrappedRoute path='/blocks' component={Blocks} content={children} />
  142. <WrappedRoute path='/mutes' component={Mutes} content={children} />
  143. <WrappedRoute path='/lists' component={Lists} content={children} />
  144. <WrappedRoute component={GenericNotFound} content={children} />
  145. </WrappedSwitch>
  146. </ColumnsAreaContainer>
  147. );
  148. }
  149. }
  150. @connect(mapStateToProps)
  151. @injectIntl
  152. @withRouter
  153. export default class UI extends React.PureComponent {
  154. static contextTypes = {
  155. router: PropTypes.object.isRequired,
  156. };
  157. static propTypes = {
  158. dispatch: PropTypes.func.isRequired,
  159. children: PropTypes.node,
  160. isComposing: PropTypes.bool,
  161. hasComposingText: PropTypes.bool,
  162. location: PropTypes.object,
  163. intl: PropTypes.object.isRequired,
  164. dropdownMenuIsOpen: PropTypes.bool,
  165. };
  166. state = {
  167. draggingOver: false,
  168. };
  169. handleBeforeUnload = (e) => {
  170. const { intl, isComposing, hasComposingText } = this.props;
  171. if (isComposing && hasComposingText) {
  172. // Setting returnValue to any string causes confirmation dialog.
  173. // Many browsers no longer display this text to users,
  174. // but we set user-friendly message for other browsers, e.g. Edge.
  175. e.returnValue = intl.formatMessage(messages.beforeUnload);
  176. }
  177. }
  178. handleLayoutChange = () => {
  179. // The cached heights are no longer accurate, invalidate
  180. this.props.dispatch(clearHeight());
  181. }
  182. handleDragEnter = (e) => {
  183. e.preventDefault();
  184. if (!this.dragTargets) {
  185. this.dragTargets = [];
  186. }
  187. if (this.dragTargets.indexOf(e.target) === -1) {
  188. this.dragTargets.push(e.target);
  189. }
  190. if (e.dataTransfer && e.dataTransfer.types.includes('Files')) {
  191. this.setState({ draggingOver: true });
  192. }
  193. }
  194. handleDragOver = (e) => {
  195. e.preventDefault();
  196. e.stopPropagation();
  197. try {
  198. e.dataTransfer.dropEffect = 'copy';
  199. } catch (err) {
  200. }
  201. return false;
  202. }
  203. handleDrop = (e) => {
  204. e.preventDefault();
  205. this.setState({ draggingOver: false });
  206. if (e.dataTransfer && e.dataTransfer.files.length === 1) {
  207. this.props.dispatch(uploadCompose(e.dataTransfer.files));
  208. }
  209. }
  210. handleDragLeave = (e) => {
  211. e.preventDefault();
  212. e.stopPropagation();
  213. this.dragTargets = this.dragTargets.filter(el => el !== e.target && this.node.contains(el));
  214. if (this.dragTargets.length > 0) {
  215. return;
  216. }
  217. this.setState({ draggingOver: false });
  218. }
  219. closeUploadModal = () => {
  220. this.setState({ draggingOver: false });
  221. }
  222. handleServiceWorkerPostMessage = ({ data }) => {
  223. if (data.type === 'navigate') {
  224. this.context.router.history.push(data.path);
  225. } else {
  226. console.warn('Unknown message type:', data.type);
  227. }
  228. }
  229. componentWillMount () {
  230. window.addEventListener('beforeunload', this.handleBeforeUnload, false);
  231. document.addEventListener('dragenter', this.handleDragEnter, false);
  232. document.addEventListener('dragover', this.handleDragOver, false);
  233. document.addEventListener('drop', this.handleDrop, false);
  234. document.addEventListener('dragleave', this.handleDragLeave, false);
  235. document.addEventListener('dragend', this.handleDragEnd, false);
  236. if ('serviceWorker' in navigator) {
  237. navigator.serviceWorker.addEventListener('message', this.handleServiceWorkerPostMessage);
  238. }
  239. this.props.dispatch(expandHomeTimeline());
  240. this.props.dispatch(expandNotifications());
  241. }
  242. componentDidMount () {
  243. this.hotkeys.__mousetrap__.stopCallback = (e, element) => {
  244. return ['TEXTAREA', 'SELECT', 'INPUT'].includes(element.tagName);
  245. };
  246. }
  247. componentWillUnmount () {
  248. window.removeEventListener('beforeunload', this.handleBeforeUnload);
  249. document.removeEventListener('dragenter', this.handleDragEnter);
  250. document.removeEventListener('dragover', this.handleDragOver);
  251. document.removeEventListener('drop', this.handleDrop);
  252. document.removeEventListener('dragleave', this.handleDragLeave);
  253. document.removeEventListener('dragend', this.handleDragEnd);
  254. }
  255. setRef = c => {
  256. this.node = c;
  257. }
  258. handleHotkeyNew = e => {
  259. e.preventDefault();
  260. const element = this.node.querySelector('.compose-form__autosuggest-wrapper textarea');
  261. if (element) {
  262. element.focus();
  263. }
  264. }
  265. handleHotkeySearch = e => {
  266. e.preventDefault();
  267. const element = this.node.querySelector('.search__input');
  268. if (element) {
  269. element.focus();
  270. }
  271. }
  272. handleHotkeyForceNew = e => {
  273. this.handleHotkeyNew(e);
  274. this.props.dispatch(resetCompose());
  275. }
  276. handleHotkeyFocusColumn = e => {
  277. const index = (e.key * 1) + 1; // First child is drawer, skip that
  278. const column = this.node.querySelector(`.column:nth-child(${index})`);
  279. if (column) {
  280. const status = column.querySelector('.focusable');
  281. if (status) {
  282. status.focus();
  283. }
  284. }
  285. }
  286. handleHotkeyBack = () => {
  287. if (window.history && window.history.length === 1) {
  288. this.context.router.history.push('/');
  289. } else {
  290. this.context.router.history.goBack();
  291. }
  292. }
  293. setHotkeysRef = c => {
  294. this.hotkeys = c;
  295. }
  296. handleHotkeyToggleHelp = () => {
  297. if (this.props.location.pathname === '/keyboard-shortcuts') {
  298. this.context.router.history.goBack();
  299. } else {
  300. this.context.router.history.push('/keyboard-shortcuts');
  301. }
  302. }
  303. handleHotkeyGoToHome = () => {
  304. this.context.router.history.push('/timelines/home');
  305. }
  306. handleHotkeyGoToNotifications = () => {
  307. this.context.router.history.push('/notifications');
  308. }
  309. handleHotkeyGoToLocal = () => {
  310. this.context.router.history.push('/timelines/public/local');
  311. }
  312. handleHotkeyGoToFederated = () => {
  313. this.context.router.history.push('/timelines/public');
  314. }
  315. handleHotkeyGoToStart = () => {
  316. this.context.router.history.push('/getting-started');
  317. }
  318. handleHotkeyGoToFavourites = () => {
  319. this.context.router.history.push('/favourites');
  320. }
  321. handleHotkeyGoToPinned = () => {
  322. this.context.router.history.push('/pinned');
  323. }
  324. handleHotkeyGoToProfile = () => {
  325. this.context.router.history.push(`/accounts/${me}`);
  326. }
  327. handleHotkeyGoToBlocked = () => {
  328. this.context.router.history.push('/blocks');
  329. }
  330. handleHotkeyGoToMuted = () => {
  331. this.context.router.history.push('/mutes');
  332. }
  333. render () {
  334. const { draggingOver } = this.state;
  335. const { children, isComposing, location, dropdownMenuIsOpen } = this.props;
  336. const handlers = {
  337. help: this.handleHotkeyToggleHelp,
  338. new: this.handleHotkeyNew,
  339. search: this.handleHotkeySearch,
  340. forceNew: this.handleHotkeyForceNew,
  341. focusColumn: this.handleHotkeyFocusColumn,
  342. back: this.handleHotkeyBack,
  343. goToHome: this.handleHotkeyGoToHome,
  344. goToNotifications: this.handleHotkeyGoToNotifications,
  345. goToLocal: this.handleHotkeyGoToLocal,
  346. goToFederated: this.handleHotkeyGoToFederated,
  347. goToStart: this.handleHotkeyGoToStart,
  348. goToFavourites: this.handleHotkeyGoToFavourites,
  349. goToPinned: this.handleHotkeyGoToPinned,
  350. goToProfile: this.handleHotkeyGoToProfile,
  351. goToBlocked: this.handleHotkeyGoToBlocked,
  352. goToMuted: this.handleHotkeyGoToMuted,
  353. };
  354. return (
  355. <HotKeys keyMap={keyMap} handlers={handlers} ref={this.setHotkeysRef}>
  356. <div className={classNames('ui', { 'is-composing': isComposing })} ref={this.setRef} style={{ pointerEvents: dropdownMenuIsOpen ? 'none' : null }}>
  357. <TabsBar />
  358. <SwitchingColumnsArea location={location} onLayoutChange={this.handleLayoutChange}>
  359. {children}
  360. </SwitchingColumnsArea>
  361. <NotificationsContainer />
  362. <LoadingBarContainer className='loading-bar' />
  363. <ModalContainer />
  364. <UploadArea active={draggingOver} onClose={this.closeUploadModal} />
  365. </div>
  366. </HotKeys>
  367. );
  368. }
  369. }