9 COMPOSE_SUBMIT_REQUEST
,
10 COMPOSE_SUBMIT_SUCCESS
,
12 COMPOSE_UPLOAD_REQUEST
,
13 COMPOSE_UPLOAD_SUCCESS
,
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
,
28 COMPOSE_UPLOAD_CHANGE_REQUEST
,
29 COMPOSE_UPLOAD_CHANGE_SUCCESS
,
30 COMPOSE_UPLOAD_CHANGE_FAIL
,
32 } from '../actions/compose';
33 import { TIMELINE_DELETE
} from '../actions/timelines';
34 import { STORE_HYDRATE
} from '../actions/store';
35 import { Map as ImmutableMap
, List as ImmutableList
, OrderedSet as ImmutableOrderedSet
, fromJS
} from 'immutable';
36 import uuid
from '../uuid';
37 import { me
} from '../initial_state';
39 const allowedAroundShortCode
= '><\u0085\u0020\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029\u0009\u000a\u000b\u000c\u000d';
41 const initialState
= ImmutableMap({
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)),
62 tagHistory: ImmutableList(),
65 function statusToTextMentions(state
, status
) {
66 let set = ImmutableOrderedSet([]);
68 if (status
.getIn(['account', 'id']) !== me
) {
69 set = set.add(`@${status.getIn(['account', 'acct'])} `);
72 return set.union(status
.get('mentions').filterNot(mention
=> mention
.get('id') === me
).map(mention
=> `@${mention.get('acct')} `)).join('');
75 function clearAll(state
) {
76 return state
.withMutations(map
=> {
78 map
.set('spoiler', false);
79 map
.set('spoiler_text', '');
80 map
.set('is_submitting', false);
81 map
.set('in_reply_to', null);
82 map
.set('privacy', state
.get('default_privacy'));
83 map
.set('sensitive', false);
84 map
.update('media_attachments', list
=> list
.clear());
85 map
.set('idempotencyKey', uuid());
89 function appendMedia(state
, media
) {
90 const prevSize
= state
.get('media_attachments').size
;
92 return state
.withMutations(map
=> {
93 map
.update('media_attachments', list
=> list
.push(media
));
94 map
.set('is_uploading', false);
95 map
.set('resetFileKey', Math
.floor((Math
.random() * 0x10000)));
96 map
.set('focusDate', new Date());
97 map
.set('idempotencyKey', uuid());
99 if (prevSize
=== 0 && (state
.get('default_sensitive') || state
.get('spoiler'))) {
100 map
.set('sensitive', true);
105 function removeMedia(state
, mediaId
) {
106 const prevSize
= state
.get('media_attachments').size
;
108 return state
.withMutations(map
=> {
109 map
.update('media_attachments', list
=> list
.filterNot(item
=> item
.get('id') === mediaId
));
110 map
.set('idempotencyKey', uuid());
112 if (prevSize
=== 1) {
113 map
.set('sensitive', false);
118 const insertSuggestion
= (state
, position
, token
, completion
) => {
119 return state
.withMutations(map
=> {
120 map
.update('text', oldText
=> `${oldText.slice(0, position)}${completion} ${oldText.slice(position + token.length)}`);
121 map
.set('suggestion_token', null);
122 map
.update('suggestions', ImmutableList(), list
=> list
.clear());
123 map
.set('focusDate', new Date());
124 map
.set('idempotencyKey', uuid());
128 const updateSuggestionTags
= (state
, token
) => {
129 const prefix
= token
.slice(1);
132 suggestions: state
.get('tagHistory')
133 .filter(tag
=> tag
.startsWith(prefix
))
135 .map(tag
=> '#' + tag
),
136 suggestion_token: token
,
140 const insertEmoji
= (state
, position
, emojiData
) => {
141 const oldText
= state
.get('text');
142 const needsSpace
= emojiData
.custom
&& position
> 0 && !allowedAroundShortCode
.includes(oldText
[position
- 1]);
143 const emoji
= needsSpace
? ' ' + emojiData
.native : emojiData
.native;
146 text: `${oldText.slice(0, position)}${emoji} ${oldText.slice(position)}`,
147 focusDate: new Date(),
148 idempotencyKey: uuid(),
152 const privacyPreference
= (a
, b
) => {
153 if (a
=== 'direct' || b
=== 'direct') {
155 } else if (a
=== 'private' || b
=== 'private') {
157 } else if (a
=== 'unlisted' || b
=== 'unlisted') {
164 const hydrate
= (state
, hydratedState
) => {
165 state
= clearAll(state
.merge(hydratedState
));
167 if (hydratedState
.has('text')) {
168 state
= state
.set('text', hydratedState
.get('text'));
174 export default function compose(state
= initialState
, action
) {
175 switch(action
.type
) {
177 return hydrate(state
, action
.state
.get('compose'));
179 return state
.set('mounted', state
.get('mounted') + 1);
180 case COMPOSE_UNMOUNT:
182 .set('mounted', Math
.max(state
.get('mounted') - 1, 0))
183 .set('is_composing', false);
184 case COMPOSE_SENSITIVITY_CHANGE:
185 return state
.withMutations(map
=> {
186 if (!state
.get('spoiler')) {
187 map
.set('sensitive', !state
.get('sensitive'));
190 map
.set('idempotencyKey', uuid());
192 case COMPOSE_SPOILERNESS_CHANGE:
193 return state
.withMutations(map
=> {
194 map
.set('spoiler_text', '');
195 map
.set('spoiler', !state
.get('spoiler'));
196 map
.set('idempotencyKey', uuid());
198 if (!state
.get('sensitive') && state
.get('media_attachments').size
>= 1) {
199 map
.set('sensitive', true);
202 case COMPOSE_SPOILER_TEXT_CHANGE:
204 .set('spoiler_text', action
.text
)
205 .set('idempotencyKey', uuid());
206 case COMPOSE_VISIBILITY_CHANGE:
208 .set('privacy', action
.value
)
209 .set('idempotencyKey', uuid());
212 .set('text', action
.text
)
213 .set('idempotencyKey', uuid());
214 case COMPOSE_COMPOSING_CHANGE:
215 return state
.set('is_composing', action
.value
);
217 return state
.withMutations(map
=> {
218 map
.set('in_reply_to', action
.status
.get('id'));
219 map
.set('text', statusToTextMentions(state
, action
.status
));
220 map
.set('privacy', privacyPreference(action
.status
.get('visibility'), state
.get('default_privacy')));
221 map
.set('focusDate', new Date());
222 map
.set('preselectDate', new Date());
223 map
.set('idempotencyKey', uuid());
225 if (action
.status
.get('spoiler_text').length
> 0) {
226 map
.set('spoiler', true);
227 map
.set('spoiler_text', action
.status
.get('spoiler_text'));
229 map
.set('spoiler', false);
230 map
.set('spoiler_text', '');
233 case COMPOSE_REPLY_CANCEL:
235 return state
.withMutations(map
=> {
236 map
.set('in_reply_to', null);
238 map
.set('spoiler', false);
239 map
.set('spoiler_text', '');
240 map
.set('privacy', state
.get('default_privacy'));
241 map
.set('idempotencyKey', uuid());
243 case COMPOSE_SUBMIT_REQUEST:
244 case COMPOSE_UPLOAD_CHANGE_REQUEST:
245 return state
.set('is_submitting', true);
246 case COMPOSE_SUBMIT_SUCCESS:
247 return clearAll(state
);
248 case COMPOSE_SUBMIT_FAIL:
249 case COMPOSE_UPLOAD_CHANGE_FAIL:
250 return state
.set('is_submitting', false);
251 case COMPOSE_UPLOAD_REQUEST:
252 return state
.set('is_uploading', true);
253 case COMPOSE_UPLOAD_SUCCESS:
254 return appendMedia(state
, fromJS(action
.media
));
255 case COMPOSE_UPLOAD_FAIL:
256 return state
.set('is_uploading', false);
257 case COMPOSE_UPLOAD_UNDO:
258 return removeMedia(state
, action
.media_id
);
259 case COMPOSE_UPLOAD_PROGRESS:
260 return state
.set('progress', Math
.round((action
.loaded
/ action
.total
) * 100));
261 case COMPOSE_MENTION:
263 .update('text', text
=> `${text}@${action.account.get('acct')} `)
264 .set('focusDate', new Date())
265 .set('idempotencyKey', uuid());
268 .update('text', text
=> `${text}@${action.account.get('acct')} `)
269 .set('privacy', 'direct')
270 .set('focusDate', new Date())
271 .set('idempotencyKey', uuid());
272 case COMPOSE_SUGGESTIONS_CLEAR:
273 return state
.update('suggestions', ImmutableList(), list
=> list
.clear()).set('suggestion_token', null);
274 case COMPOSE_SUGGESTIONS_READY:
275 return state
.set('suggestions', ImmutableList(action
.accounts
? action
.accounts
.map(item
=> item
.id
) : action
.emojis
)).set('suggestion_token', action
.token
);
276 case COMPOSE_SUGGESTION_SELECT:
277 return insertSuggestion(state
, action
.position
, action
.token
, action
.completion
);
278 case COMPOSE_SUGGESTION_TAGS_UPDATE:
279 return updateSuggestionTags(state
, action
.token
);
280 case COMPOSE_TAG_HISTORY_UPDATE:
281 return state
.set('tagHistory', fromJS(action
.tags
));
282 case TIMELINE_DELETE:
283 if (action
.id
=== state
.get('in_reply_to')) {
284 return state
.set('in_reply_to', null);
288 case COMPOSE_EMOJI_INSERT:
289 return insertEmoji(state
, action
.position
, action
.emoji
);
290 case COMPOSE_UPLOAD_CHANGE_SUCCESS:
292 .set('is_submitting', false)
293 .update('media_attachments', list
=> list
.map(item
=> {
294 if (item
.get('id') === action
.media
.id
) {
295 return fromJS(action
.media
);