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.

153 lines
5.1 KiB

  1. import {
  2. COMPOSE_MOUNT,
  3. COMPOSE_UNMOUNT,
  4. COMPOSE_CHANGE,
  5. COMPOSE_REPLY,
  6. COMPOSE_REPLY_CANCEL,
  7. COMPOSE_MENTION,
  8. COMPOSE_SUBMIT_REQUEST,
  9. COMPOSE_SUBMIT_SUCCESS,
  10. COMPOSE_SUBMIT_FAIL,
  11. COMPOSE_UPLOAD_REQUEST,
  12. COMPOSE_UPLOAD_SUCCESS,
  13. COMPOSE_UPLOAD_FAIL,
  14. COMPOSE_UPLOAD_UNDO,
  15. COMPOSE_UPLOAD_PROGRESS,
  16. COMPOSE_SUGGESTIONS_CLEAR,
  17. COMPOSE_SUGGESTIONS_READY,
  18. COMPOSE_SUGGESTION_SELECT,
  19. COMPOSE_SENSITIVITY_CHANGE,
  20. COMPOSE_VISIBILITY_CHANGE,
  21. COMPOSE_LISTABILITY_CHANGE
  22. } from '../actions/compose';
  23. import { TIMELINE_DELETE } from '../actions/timelines';
  24. import { STORE_HYDRATE } from '../actions/store';
  25. import Immutable from 'immutable';
  26. const initialState = Immutable.Map({
  27. mounted: false,
  28. sensitive: false,
  29. unlisted: false,
  30. private: false,
  31. text: '',
  32. fileDropDate: null,
  33. in_reply_to: null,
  34. is_submitting: false,
  35. is_uploading: false,
  36. progress: 0,
  37. media_attachments: Immutable.List(),
  38. suggestion_token: null,
  39. suggestions: Immutable.List(),
  40. me: null,
  41. resetFileKey: Math.floor((Math.random() * 0x10000))
  42. });
  43. function statusToTextMentions(state, status) {
  44. let set = Immutable.OrderedSet([]);
  45. let me = state.get('me');
  46. if (status.getIn(['account', 'id']) !== me) {
  47. set = set.add(`@${status.getIn(['account', 'acct'])} `);
  48. }
  49. return set.union(status.get('mentions').filterNot(mention => mention.get('id') === me).map(mention => `@${mention.get('acct')} `)).join('');
  50. };
  51. function clearAll(state) {
  52. return state.withMutations(map => {
  53. map.set('text', '');
  54. map.set('is_submitting', false);
  55. map.set('in_reply_to', null);
  56. map.update('media_attachments', list => list.clear());
  57. });
  58. };
  59. function appendMedia(state, media) {
  60. return state.withMutations(map => {
  61. map.update('media_attachments', list => list.push(media));
  62. map.set('is_uploading', false);
  63. map.set('resetFileKey', Math.floor((Math.random() * 0x10000)));
  64. map.update('text', oldText => `${oldText} ${media.get('text_url')}`.trim());
  65. });
  66. };
  67. function removeMedia(state, mediaId) {
  68. const media = state.get('media_attachments').find(item => item.get('id') === mediaId);
  69. return state.withMutations(map => {
  70. map.update('media_attachments', list => list.filterNot(item => item.get('id') === mediaId));
  71. map.update('text', text => text.replace(media.get('text_url'), '').trim());
  72. });
  73. };
  74. const insertSuggestion = (state, position, token, completion) => {
  75. return state.withMutations(map => {
  76. map.update('text', oldText => `${oldText.slice(0, position)}${completion} ${oldText.slice(position + token.length)}`);
  77. map.set('suggestion_token', null);
  78. map.update('suggestions', Immutable.List(), list => list.clear());
  79. });
  80. };
  81. export default function compose(state = initialState, action) {
  82. switch(action.type) {
  83. case STORE_HYDRATE:
  84. return state.merge(action.state.get('compose'));
  85. case COMPOSE_MOUNT:
  86. return state.set('mounted', true);
  87. case COMPOSE_UNMOUNT:
  88. return state.set('mounted', false);
  89. case COMPOSE_SENSITIVITY_CHANGE:
  90. return state.set('sensitive', action.checked);
  91. case COMPOSE_VISIBILITY_CHANGE:
  92. return state.set('private', action.checked);
  93. case COMPOSE_LISTABILITY_CHANGE:
  94. return state.set('unlisted', action.checked);
  95. case COMPOSE_CHANGE:
  96. return state.set('text', action.text);
  97. case COMPOSE_REPLY:
  98. return state.withMutations(map => {
  99. map.set('in_reply_to', action.status.get('id'));
  100. map.set('text', statusToTextMentions(state, action.status));
  101. });
  102. case COMPOSE_REPLY_CANCEL:
  103. return state.withMutations(map => {
  104. map.set('in_reply_to', null);
  105. map.set('text', '');
  106. });
  107. case COMPOSE_SUBMIT_REQUEST:
  108. return state.set('is_submitting', true);
  109. case COMPOSE_SUBMIT_SUCCESS:
  110. return clearAll(state);
  111. case COMPOSE_SUBMIT_FAIL:
  112. return state.set('is_submitting', false);
  113. case COMPOSE_UPLOAD_REQUEST:
  114. return state.withMutations(map => {
  115. map.set('is_uploading', true);
  116. map.set('fileDropDate', new Date());
  117. });
  118. case COMPOSE_UPLOAD_SUCCESS:
  119. return appendMedia(state, Immutable.fromJS(action.media));
  120. case COMPOSE_UPLOAD_FAIL:
  121. return state.set('is_uploading', false);
  122. case COMPOSE_UPLOAD_UNDO:
  123. return removeMedia(state, action.media_id);
  124. case COMPOSE_UPLOAD_PROGRESS:
  125. return state.set('progress', Math.round((action.loaded / action.total) * 100));
  126. case COMPOSE_MENTION:
  127. return state.update('text', text => `${text}@${action.account.get('acct')} `);
  128. case COMPOSE_SUGGESTIONS_CLEAR:
  129. return state.update('suggestions', Immutable.List(), list => list.clear()).set('suggestion_token', null);
  130. case COMPOSE_SUGGESTIONS_READY:
  131. return state.set('suggestions', Immutable.List(action.accounts.map(item => item.id))).set('suggestion_token', action.token);
  132. case COMPOSE_SUGGESTION_SELECT:
  133. return insertSuggestion(state, action.position, action.token, action.completion);
  134. case TIMELINE_DELETE:
  135. if (action.id === state.get('in_reply_to')) {
  136. return state.set('in_reply_to', null);
  137. } else {
  138. return state;
  139. }
  140. default:
  141. return state;
  142. }
  143. };