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.
 
 
 
 

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