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.

151 lines
5.0 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. });
  42. function statusToTextMentions(state, status) {
  43. let set = Immutable.OrderedSet([]);
  44. let me = state.get('me');
  45. if (status.getIn(['account', 'id']) !== me) {
  46. set = set.add(`@${status.getIn(['account', 'acct'])} `);
  47. }
  48. return set.union(status.get('mentions').filterNot(mention => mention.get('id') === me).map(mention => `@${mention.get('acct')} `)).join('');
  49. };
  50. function clearAll(state) {
  51. return state.withMutations(map => {
  52. map.set('text', '');
  53. map.set('is_submitting', false);
  54. map.set('in_reply_to', null);
  55. map.update('media_attachments', list => list.clear());
  56. });
  57. };
  58. function appendMedia(state, media) {
  59. return state.withMutations(map => {
  60. map.update('media_attachments', list => list.push(media));
  61. map.set('is_uploading', false);
  62. map.update('text', oldText => `${oldText} ${media.get('text_url')}`.trim());
  63. });
  64. };
  65. function removeMedia(state, mediaId) {
  66. const media = state.get('media_attachments').find(item => item.get('id') === mediaId);
  67. return state.withMutations(map => {
  68. map.update('media_attachments', list => list.filterNot(item => item.get('id') === mediaId));
  69. map.update('text', text => text.replace(media.get('text_url'), '').trim());
  70. });
  71. };
  72. const insertSuggestion = (state, position, token, completion) => {
  73. return state.withMutations(map => {
  74. map.update('text', oldText => `${oldText.slice(0, position)}${completion}${oldText.slice(position + token.length)}`);
  75. map.set('suggestion_token', null);
  76. map.update('suggestions', Immutable.List(), list => list.clear());
  77. });
  78. };
  79. export default function compose(state = initialState, action) {
  80. switch(action.type) {
  81. case STORE_HYDRATE:
  82. return state.merge(action.state.get('compose'));
  83. case COMPOSE_MOUNT:
  84. return state.set('mounted', true);
  85. case COMPOSE_UNMOUNT:
  86. return state.set('mounted', false);
  87. case COMPOSE_SENSITIVITY_CHANGE:
  88. return state.set('sensitive', action.checked);
  89. case COMPOSE_VISIBILITY_CHANGE:
  90. return state.set('private', action.checked);
  91. case COMPOSE_LISTABILITY_CHANGE:
  92. return state.set('unlisted', action.checked);
  93. case COMPOSE_CHANGE:
  94. return state.set('text', action.text);
  95. case COMPOSE_REPLY:
  96. return state.withMutations(map => {
  97. map.set('in_reply_to', action.status.get('id'));
  98. map.set('text', statusToTextMentions(state, action.status));
  99. });
  100. case COMPOSE_REPLY_CANCEL:
  101. return state.withMutations(map => {
  102. map.set('in_reply_to', null);
  103. map.set('text', '');
  104. });
  105. case COMPOSE_SUBMIT_REQUEST:
  106. return state.set('is_submitting', true);
  107. case COMPOSE_SUBMIT_SUCCESS:
  108. return clearAll(state);
  109. case COMPOSE_SUBMIT_FAIL:
  110. return state.set('is_submitting', false);
  111. case COMPOSE_UPLOAD_REQUEST:
  112. return state.withMutations(map => {
  113. map.set('is_uploading', true);
  114. map.set('fileDropDate', new Date());
  115. });
  116. case COMPOSE_UPLOAD_SUCCESS:
  117. return appendMedia(state, Immutable.fromJS(action.media));
  118. case COMPOSE_UPLOAD_FAIL:
  119. return state.set('is_uploading', false);
  120. case COMPOSE_UPLOAD_UNDO:
  121. return removeMedia(state, action.media_id);
  122. case COMPOSE_UPLOAD_PROGRESS:
  123. return state.set('progress', Math.round((action.loaded / action.total) * 100));
  124. case COMPOSE_MENTION:
  125. return state.update('text', text => `${text}@${action.account.get('acct')} `);
  126. case COMPOSE_SUGGESTIONS_CLEAR:
  127. return state.update('suggestions', Immutable.List(), list => list.clear()).set('suggestion_token', null);
  128. case COMPOSE_SUGGESTIONS_READY:
  129. return state.set('suggestions', Immutable.List(action.accounts.map(item => item.id))).set('suggestion_token', action.token);
  130. case COMPOSE_SUGGESTION_SELECT:
  131. return insertSuggestion(state, action.position, action.token, action.completion);
  132. case TIMELINE_DELETE:
  133. if (action.id === state.get('in_reply_to')) {
  134. return state.set('in_reply_to', null);
  135. } else {
  136. return state;
  137. }
  138. default:
  139. return state;
  140. }
  141. };