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.

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