]> cat aescling's git repositories - mastodon.git/blob - app/javascript/mastodon/reducers/compose.js
Add emoji autosuggest (#5053)
[mastodon.git] / app / javascript / mastodon / reducers / compose.js
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_COMPOSING_CHANGE,
24 COMPOSE_EMOJI_INSERT,
25 } from '../actions/compose';
26 import { TIMELINE_DELETE } from '../actions/timelines';
27 import { STORE_HYDRATE } from '../actions/store';
28 import { Map as ImmutableMap, List as ImmutableList, OrderedSet as ImmutableOrderedSet, fromJS } from 'immutable';
29 import uuid from '../uuid';
30
31 const initialState = ImmutableMap({
32 mounted: false,
33 sensitive: false,
34 spoiler: false,
35 spoiler_text: '',
36 privacy: null,
37 text: '',
38 focusDate: null,
39 preselectDate: null,
40 in_reply_to: null,
41 is_composing: false,
42 is_submitting: false,
43 is_uploading: false,
44 progress: 0,
45 media_attachments: ImmutableList(),
46 suggestion_token: null,
47 suggestions: ImmutableList(),
48 me: null,
49 default_privacy: 'public',
50 default_sensitive: false,
51 resetFileKey: Math.floor((Math.random() * 0x10000)),
52 idempotencyKey: null,
53 });
54
55 function statusToTextMentions(state, status) {
56 let set = ImmutableOrderedSet([]);
57 let me = state.get('me');
58
59 if (status.getIn(['account', 'id']) !== me) {
60 set = set.add(`@${status.getIn(['account', 'acct'])} `);
61 }
62
63 return set.union(status.get('mentions').filterNot(mention => mention.get('id') === me).map(mention => `@${mention.get('acct')} `)).join('');
64 };
65
66 function clearAll(state) {
67 return state.withMutations(map => {
68 map.set('text', '');
69 map.set('spoiler', false);
70 map.set('spoiler_text', '');
71 map.set('is_submitting', false);
72 map.set('in_reply_to', null);
73 map.set('privacy', state.get('default_privacy'));
74 map.set('sensitive', false);
75 map.update('media_attachments', list => list.clear());
76 map.set('idempotencyKey', uuid());
77 });
78 };
79
80 function appendMedia(state, media) {
81 const prevSize = state.get('media_attachments').size;
82
83 return state.withMutations(map => {
84 map.update('media_attachments', list => list.push(media));
85 map.set('is_uploading', false);
86 map.set('resetFileKey', Math.floor((Math.random() * 0x10000)));
87 map.update('text', oldText => `${oldText.trim()} ${media.get('text_url')}`);
88 map.set('focusDate', new Date());
89 map.set('idempotencyKey', uuid());
90
91 if (prevSize === 0 && (state.get('default_sensitive') || state.get('spoiler'))) {
92 map.set('sensitive', true);
93 }
94 });
95 };
96
97 function removeMedia(state, mediaId) {
98 const media = state.get('media_attachments').find(item => item.get('id') === mediaId);
99 const prevSize = state.get('media_attachments').size;
100
101 return state.withMutations(map => {
102 map.update('media_attachments', list => list.filterNot(item => item.get('id') === mediaId));
103 map.update('text', text => text.replace(media.get('text_url'), '').trim());
104 map.set('idempotencyKey', uuid());
105
106 if (prevSize === 1) {
107 map.set('sensitive', false);
108 }
109 });
110 };
111
112 const insertSuggestion = (state, position, token, completion) => {
113 return state.withMutations(map => {
114 map.update('text', oldText => `${oldText.slice(0, position)}${completion} ${oldText.slice(position + token.length)}`);
115 map.set('suggestion_token', null);
116 map.update('suggestions', ImmutableList(), list => list.clear());
117 map.set('focusDate', new Date());
118 map.set('idempotencyKey', uuid());
119 });
120 };
121
122 const insertEmoji = (state, position, emojiData) => {
123 const emoji = emojiData.native;
124
125 return state.withMutations(map => {
126 map.update('text', oldText => `${oldText.slice(0, position)}${emoji} ${oldText.slice(position)}`);
127 map.set('focusDate', new Date());
128 map.set('idempotencyKey', uuid());
129 });
130 };
131
132 const privacyPreference = (a, b) => {
133 if (a === 'direct' || b === 'direct') {
134 return 'direct';
135 } else if (a === 'private' || b === 'private') {
136 return 'private';
137 } else if (a === 'unlisted' || b === 'unlisted') {
138 return 'unlisted';
139 } else {
140 return 'public';
141 }
142 };
143
144 const hydrate = (state, hydratedState) => {
145 state = clearAll(state.merge(hydratedState));
146
147 if (hydratedState.has('text')) {
148 state = state.set('text', hydratedState.get('text'));
149 }
150
151 return state;
152 };
153
154 export default function compose(state = initialState, action) {
155 switch(action.type) {
156 case STORE_HYDRATE:
157 return hydrate(state, action.state.get('compose'));
158 case COMPOSE_MOUNT:
159 return state.set('mounted', true);
160 case COMPOSE_UNMOUNT:
161 return state
162 .set('mounted', false)
163 .set('is_composing', false);
164 case COMPOSE_SENSITIVITY_CHANGE:
165 return state.withMutations(map => {
166 if (!state.get('spoiler')) {
167 map.set('sensitive', !state.get('sensitive'));
168 }
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
178 if (!state.get('sensitive') && state.get('media_attachments').size >= 1) {
179 map.set('sensitive', true);
180 }
181 });
182 case COMPOSE_SPOILER_TEXT_CHANGE:
183 return state
184 .set('spoiler_text', action.text)
185 .set('idempotencyKey', uuid());
186 case COMPOSE_VISIBILITY_CHANGE:
187 return state
188 .set('privacy', action.value)
189 .set('idempotencyKey', uuid());
190 case COMPOSE_CHANGE:
191 return state
192 .set('text', action.text)
193 .set('idempotencyKey', uuid());
194 case COMPOSE_COMPOSING_CHANGE:
195 return state.set('is_composing', action.value);
196 case COMPOSE_REPLY:
197 return state.withMutations(map => {
198 map.set('in_reply_to', action.status.get('id'));
199 map.set('text', statusToTextMentions(state, action.status));
200 map.set('privacy', privacyPreference(action.status.get('visibility'), state.get('default_privacy')));
201 map.set('focusDate', new Date());
202 map.set('preselectDate', new Date());
203 map.set('idempotencyKey', uuid());
204
205 if (action.status.get('spoiler_text').length > 0) {
206 map.set('spoiler', true);
207 map.set('spoiler_text', action.status.get('spoiler_text'));
208 } else {
209 map.set('spoiler', false);
210 map.set('spoiler_text', '');
211 }
212 });
213 case COMPOSE_REPLY_CANCEL:
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 return state.set('is_submitting', true);
224 case COMPOSE_SUBMIT_SUCCESS:
225 return clearAll(state);
226 case COMPOSE_SUBMIT_FAIL:
227 return state.set('is_submitting', false);
228 case COMPOSE_UPLOAD_REQUEST:
229 return state.withMutations(map => {
230 map.set('is_uploading', true);
231 });
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
242 .update('text', text => `${text}@${action.account.get('acct')} `)
243 .set('focusDate', new Date())
244 .set('idempotencyKey', uuid());
245 case COMPOSE_SUGGESTIONS_CLEAR:
246 return state.update('suggestions', ImmutableList(), list => list.clear()).set('suggestion_token', null);
247 case COMPOSE_SUGGESTIONS_READY:
248 return state.set('suggestions', ImmutableList(action.accounts ? action.accounts.map(item => item.id) : action.emojis)).set('suggestion_token', action.token);
249 case COMPOSE_SUGGESTION_SELECT:
250 return insertSuggestion(state, action.position, action.token, action.completion);
251 case TIMELINE_DELETE:
252 if (action.id === state.get('in_reply_to')) {
253 return state.set('in_reply_to', null);
254 } else {
255 return state;
256 }
257 case COMPOSE_EMOJI_INSERT:
258 return insertEmoji(state, action.position, action.emoji);
259 default:
260 return state;
261 }
262 };
This page took 0.110838 seconds and 4 git commands to generate.