The code powering m.abunchtell.com https://m.abunchtell.com
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

333 satır
11 KiB

  1. import {
  2. COMPOSE_MOUNT,
  3. COMPOSE_UNMOUNT,
  4. COMPOSE_CHANGE,
  5. COMPOSE_REPLY,
  6. COMPOSE_REPLY_CANCEL,
  7. COMPOSE_DIRECT,
  8. COMPOSE_MENTION,
  9. COMPOSE_SUBMIT_REQUEST,
  10. COMPOSE_SUBMIT_SUCCESS,
  11. COMPOSE_SUBMIT_FAIL,
  12. COMPOSE_UPLOAD_REQUEST,
  13. COMPOSE_UPLOAD_SUCCESS,
  14. COMPOSE_UPLOAD_FAIL,
  15. COMPOSE_UPLOAD_UNDO,
  16. COMPOSE_UPLOAD_PROGRESS,
  17. COMPOSE_SUGGESTIONS_CLEAR,
  18. COMPOSE_SUGGESTIONS_READY,
  19. COMPOSE_SUGGESTION_SELECT,
  20. COMPOSE_SUGGESTION_TAGS_UPDATE,
  21. COMPOSE_TAG_HISTORY_UPDATE,
  22. COMPOSE_SENSITIVITY_CHANGE,
  23. COMPOSE_SPOILERNESS_CHANGE,
  24. COMPOSE_SPOILER_TEXT_CHANGE,
  25. COMPOSE_VISIBILITY_CHANGE,
  26. COMPOSE_COMPOSING_CHANGE,
  27. COMPOSE_EMOJI_INSERT,
  28. COMPOSE_UPLOAD_CHANGE_REQUEST,
  29. COMPOSE_UPLOAD_CHANGE_SUCCESS,
  30. COMPOSE_UPLOAD_CHANGE_FAIL,
  31. COMPOSE_RESET,
  32. } from '../actions/compose';
  33. import { TIMELINE_DELETE } from '../actions/timelines';
  34. import { STORE_HYDRATE } from '../actions/store';
  35. import { REDRAFT } from '../actions/statuses';
  36. import { Map as ImmutableMap, List as ImmutableList, OrderedSet as ImmutableOrderedSet, fromJS } from 'immutable';
  37. import uuid from '../uuid';
  38. import { me } from '../initial_state';
  39. import { unescapeHTML } from '../utils/html';
  40. const initialState = ImmutableMap({
  41. mounted: 0,
  42. sensitive: false,
  43. spoiler: false,
  44. spoiler_text: '',
  45. privacy: null,
  46. text: '',
  47. focusDate: null,
  48. caretPosition: null,
  49. preselectDate: null,
  50. in_reply_to: null,
  51. is_composing: false,
  52. is_submitting: false,
  53. is_uploading: false,
  54. progress: 0,
  55. media_attachments: ImmutableList(),
  56. suggestion_token: null,
  57. suggestions: ImmutableList(),
  58. default_privacy: 'public',
  59. default_sensitive: false,
  60. resetFileKey: Math.floor((Math.random() * 0x10000)),
  61. idempotencyKey: null,
  62. tagHistory: ImmutableList(),
  63. });
  64. function statusToTextMentions(state, status) {
  65. let set = ImmutableOrderedSet([]);
  66. if (status.getIn(['account', 'id']) !== me) {
  67. set = set.add(`@${status.getIn(['account', 'acct'])} `);
  68. }
  69. return set.union(status.get('mentions').filterNot(mention => mention.get('id') === me).map(mention => `@${mention.get('acct')} `)).join('');
  70. };
  71. function clearAll(state) {
  72. return state.withMutations(map => {
  73. map.set('text', '');
  74. map.set('spoiler', false);
  75. map.set('spoiler_text', '');
  76. map.set('is_submitting', false);
  77. map.set('in_reply_to', null);
  78. map.set('privacy', state.get('default_privacy'));
  79. map.set('sensitive', false);
  80. map.update('media_attachments', list => list.clear());
  81. map.set('idempotencyKey', uuid());
  82. });
  83. };
  84. function appendMedia(state, media) {
  85. const prevSize = state.get('media_attachments').size;
  86. return state.withMutations(map => {
  87. map.update('media_attachments', list => list.push(media));
  88. map.set('is_uploading', false);
  89. map.set('resetFileKey', Math.floor((Math.random() * 0x10000)));
  90. map.set('idempotencyKey', uuid());
  91. if (prevSize === 0 && (state.get('default_sensitive') || state.get('spoiler'))) {
  92. map.set('sensitive', true);
  93. }
  94. });
  95. };
  96. function removeMedia(state, mediaId) {
  97. const prevSize = state.get('media_attachments').size;
  98. return state.withMutations(map => {
  99. map.update('media_attachments', list => list.filterNot(item => item.get('id') === mediaId));
  100. map.set('idempotencyKey', uuid());
  101. if (prevSize === 1) {
  102. map.set('sensitive', false);
  103. }
  104. });
  105. };
  106. const insertSuggestion = (state, position, token, completion) => {
  107. return state.withMutations(map => {
  108. map.update('text', oldText => `${oldText.slice(0, position)}${completion} ${oldText.slice(position + token.length)}`);
  109. map.set('suggestion_token', null);
  110. map.update('suggestions', ImmutableList(), list => list.clear());
  111. map.set('focusDate', new Date());
  112. map.set('caretPosition', position + completion.length + 1);
  113. map.set('idempotencyKey', uuid());
  114. });
  115. };
  116. const updateSuggestionTags = (state, token) => {
  117. const prefix = token.slice(1);
  118. return state.merge({
  119. suggestions: state.get('tagHistory')
  120. .filter(tag => tag.toLowerCase().startsWith(prefix.toLowerCase()))
  121. .slice(0, 4)
  122. .map(tag => '#' + tag),
  123. suggestion_token: token,
  124. });
  125. };
  126. const insertEmoji = (state, position, emojiData, needsSpace) => {
  127. const oldText = state.get('text');
  128. const emoji = needsSpace ? ' ' + emojiData.native : emojiData.native;
  129. return state.merge({
  130. text: `${oldText.slice(0, position)}${emoji} ${oldText.slice(position)}`,
  131. focusDate: new Date(),
  132. caretPosition: position + emoji.length + 1,
  133. idempotencyKey: uuid(),
  134. });
  135. };
  136. const privacyPreference = (a, b) => {
  137. const order = ['public', 'unlisted', 'private', 'direct'];
  138. return order[Math.max(order.indexOf(a), order.indexOf(b), 0)];
  139. };
  140. const hydrate = (state, hydratedState) => {
  141. state = clearAll(state.merge(hydratedState));
  142. if (hydratedState.has('text')) {
  143. state = state.set('text', hydratedState.get('text'));
  144. }
  145. return state;
  146. };
  147. const domParser = new DOMParser();
  148. const expandMentions = status => {
  149. const fragment = domParser.parseFromString(status.get('content'), 'text/html').documentElement;
  150. status.get('mentions').forEach(mention => {
  151. fragment.querySelector(`a[href="${mention.get('url')}"]`).textContent = `@${mention.get('acct')}`;
  152. });
  153. return fragment.innerHTML;
  154. };
  155. export default function compose(state = initialState, action) {
  156. switch(action.type) {
  157. case STORE_HYDRATE:
  158. return hydrate(state, action.state.get('compose'));
  159. case COMPOSE_MOUNT:
  160. return state.set('mounted', state.get('mounted') + 1);
  161. case COMPOSE_UNMOUNT:
  162. return state
  163. .set('mounted', Math.max(state.get('mounted') - 1, 0))
  164. .set('is_composing', false);
  165. case COMPOSE_SENSITIVITY_CHANGE:
  166. return state.withMutations(map => {
  167. if (!state.get('spoiler')) {
  168. map.set('sensitive', !state.get('sensitive'));
  169. }
  170. map.set('idempotencyKey', uuid());
  171. });
  172. case COMPOSE_SPOILERNESS_CHANGE:
  173. return state.withMutations(map => {
  174. map.set('spoiler_text', '');
  175. map.set('spoiler', !state.get('spoiler'));
  176. map.set('idempotencyKey', uuid());
  177. if (!state.get('sensitive') && state.get('media_attachments').size >= 1) {
  178. map.set('sensitive', true);
  179. }
  180. });
  181. case COMPOSE_SPOILER_TEXT_CHANGE:
  182. return state
  183. .set('spoiler_text', action.text)
  184. .set('idempotencyKey', uuid());
  185. case COMPOSE_VISIBILITY_CHANGE:
  186. return state
  187. .set('privacy', action.value)
  188. .set('idempotencyKey', uuid());
  189. case COMPOSE_CHANGE:
  190. return state
  191. .set('text', action.text)
  192. .set('idempotencyKey', uuid());
  193. case COMPOSE_COMPOSING_CHANGE:
  194. return state.set('is_composing', action.value);
  195. case COMPOSE_REPLY:
  196. return state.withMutations(map => {
  197. map.set('in_reply_to', action.status.get('id'));
  198. map.set('text', statusToTextMentions(state, action.status));
  199. map.set('privacy', privacyPreference(action.status.get('visibility'), state.get('default_privacy')));
  200. map.set('focusDate', new Date());
  201. map.set('caretPosition', null);
  202. map.set('preselectDate', new Date());
  203. map.set('idempotencyKey', uuid());
  204. if (action.status.get('spoiler_text').length > 0) {
  205. map.set('spoiler', true);
  206. map.set('spoiler_text', action.status.get('spoiler_text'));
  207. } else {
  208. map.set('spoiler', false);
  209. map.set('spoiler_text', '');
  210. }
  211. });
  212. case COMPOSE_REPLY_CANCEL:
  213. case COMPOSE_RESET:
  214. return state.withMutations(map => {
  215. map.set('in_reply_to', null);
  216. map.set('text', '');
  217. map.set('spoiler', false);
  218. map.set('spoiler_text', '');
  219. map.set('privacy', state.get('default_privacy'));
  220. map.set('idempotencyKey', uuid());
  221. });
  222. case COMPOSE_SUBMIT_REQUEST:
  223. case COMPOSE_UPLOAD_CHANGE_REQUEST:
  224. return state.set('is_submitting', true);
  225. case COMPOSE_SUBMIT_SUCCESS:
  226. return clearAll(state);
  227. case COMPOSE_SUBMIT_FAIL:
  228. case COMPOSE_UPLOAD_CHANGE_FAIL:
  229. return state.set('is_submitting', false);
  230. case COMPOSE_UPLOAD_REQUEST:
  231. return state.set('is_uploading', true);
  232. case COMPOSE_UPLOAD_SUCCESS:
  233. return appendMedia(state, fromJS(action.media));
  234. case COMPOSE_UPLOAD_FAIL:
  235. return state.set('is_uploading', false);
  236. case COMPOSE_UPLOAD_UNDO:
  237. return removeMedia(state, action.media_id);
  238. case COMPOSE_UPLOAD_PROGRESS:
  239. return state.set('progress', Math.round((action.loaded / action.total) * 100));
  240. case COMPOSE_MENTION:
  241. return state.withMutations(map => {
  242. map.update('text', text => [text.trim(), `@${action.account.get('acct')} `].filter((str) => str.length !== 0).join(' '));
  243. map.set('focusDate', new Date());
  244. map.set('caretPosition', null);
  245. map.set('idempotencyKey', uuid());
  246. });
  247. case COMPOSE_DIRECT:
  248. return state.withMutations(map => {
  249. map.update('text', text => [text.trim(), `@${action.account.get('acct')} `].filter((str) => str.length !== 0).join(' '));
  250. map.set('privacy', 'direct');
  251. map.set('focusDate', new Date());
  252. map.set('caretPosition', null);
  253. map.set('idempotencyKey', uuid());
  254. });
  255. case COMPOSE_SUGGESTIONS_CLEAR:
  256. return state.update('suggestions', ImmutableList(), list => list.clear()).set('suggestion_token', null);
  257. case COMPOSE_SUGGESTIONS_READY:
  258. return state.set('suggestions', ImmutableList(action.accounts ? action.accounts.map(item => item.id) : action.emojis)).set('suggestion_token', action.token);
  259. case COMPOSE_SUGGESTION_SELECT:
  260. return insertSuggestion(state, action.position, action.token, action.completion);
  261. case COMPOSE_SUGGESTION_TAGS_UPDATE:
  262. return updateSuggestionTags(state, action.token);
  263. case COMPOSE_TAG_HISTORY_UPDATE:
  264. return state.set('tagHistory', fromJS(action.tags));
  265. case TIMELINE_DELETE:
  266. if (action.id === state.get('in_reply_to')) {
  267. return state.set('in_reply_to', null);
  268. } else {
  269. return state;
  270. }
  271. case COMPOSE_EMOJI_INSERT:
  272. return insertEmoji(state, action.position, action.emoji, action.needsSpace);
  273. case COMPOSE_UPLOAD_CHANGE_SUCCESS:
  274. return state
  275. .set('is_submitting', false)
  276. .update('media_attachments', list => list.map(item => {
  277. if (item.get('id') === action.media.id) {
  278. return fromJS(action.media);
  279. }
  280. return item;
  281. }));
  282. case REDRAFT:
  283. return state.withMutations(map => {
  284. map.set('text', unescapeHTML(expandMentions(action.status)));
  285. map.set('in_reply_to', action.status.get('in_reply_to_id'));
  286. map.set('privacy', action.status.get('visibility'));
  287. map.set('media_attachments', action.status.get('media_attachments'));
  288. map.set('focusDate', new Date());
  289. map.set('caretPosition', null);
  290. map.set('idempotencyKey', uuid());
  291. if (action.status.get('spoiler_text').length > 0) {
  292. map.set('spoiler', true);
  293. map.set('spoiler_text', action.status.get('spoiler_text'));
  294. } else {
  295. map.set('spoiler', false);
  296. map.set('spoiler_text', '');
  297. }
  298. });
  299. default:
  300. return state;
  301. }
  302. };