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.

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