8 COMPOSE_SUBMIT_REQUEST
,
9 COMPOSE_SUBMIT_SUCCESS
,
11 COMPOSE_UPLOAD_REQUEST
,
12 COMPOSE_UPLOAD_SUCCESS
,
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
,
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';
31 const initialState
= ImmutableMap({
45 media_attachments: ImmutableList(),
46 suggestion_token: null,
47 suggestions: ImmutableList(),
49 default_privacy: 'public',
50 default_sensitive: false,
51 resetFileKey: Math
.floor((Math
.random() * 0x10000)),
55 function statusToTextMentions(state
, status
) {
56 let set = ImmutableOrderedSet([]);
57 let me
= state
.get('me');
59 if (status
.getIn(['account', 'id']) !== me
) {
60 set = set.add(`@${status.getIn(['account', 'acct'])} `);
63 return set.union(status
.get('mentions').filterNot(mention
=> mention
.get('id') === me
).map(mention
=> `@${mention.get('acct')} `)).join('');
66 function clearAll(state
) {
67 return state
.withMutations(map
=> {
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());
80 function appendMedia(state
, media
) {
81 const prevSize
= state
.get('media_attachments').size
;
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());
91 if (prevSize
=== 0 && (state
.get('default_sensitive') || state
.get('spoiler'))) {
92 map
.set('sensitive', true);
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
;
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());
106 if (prevSize
=== 1) {
107 map
.set('sensitive', false);
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());
122 const insertEmoji
= (state
, position
, emojiData
) => {
123 const emoji
= emojiData
.native;
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());
132 const privacyPreference
= (a
, b
) => {
133 if (a
=== 'direct' || b
=== 'direct') {
135 } else if (a
=== 'private' || b
=== 'private') {
137 } else if (a
=== 'unlisted' || b
=== 'unlisted') {
144 const hydrate
= (state
, hydratedState
) => {
145 state
= clearAll(state
.merge(hydratedState
));
147 if (hydratedState
.has('text')) {
148 state
= state
.set('text', hydratedState
.get('text'));
154 export default function compose(state
= initialState
, action
) {
155 switch(action
.type
) {
157 return hydrate(state
, action
.state
.get('compose'));
159 return state
.set('mounted', true);
160 case COMPOSE_UNMOUNT:
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'));
170 map
.set('idempotencyKey', uuid());
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());
178 if (!state
.get('sensitive') && state
.get('media_attachments').size
>= 1) {
179 map
.set('sensitive', true);
182 case COMPOSE_SPOILER_TEXT_CHANGE:
184 .set('spoiler_text', action
.text
)
185 .set('idempotencyKey', uuid());
186 case COMPOSE_VISIBILITY_CHANGE:
188 .set('privacy', action
.value
)
189 .set('idempotencyKey', uuid());
192 .set('text', action
.text
)
193 .set('idempotencyKey', uuid());
194 case COMPOSE_COMPOSING_CHANGE:
195 return state
.set('is_composing', action
.value
);
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());
205 if (action
.status
.get('spoiler_text').length
> 0) {
206 map
.set('spoiler', true);
207 map
.set('spoiler_text', action
.status
.get('spoiler_text'));
209 map
.set('spoiler', false);
210 map
.set('spoiler_text', '');
213 case COMPOSE_REPLY_CANCEL:
214 return state
.withMutations(map
=> {
215 map
.set('in_reply_to', null);
217 map
.set('spoiler', false);
218 map
.set('spoiler_text', '');
219 map
.set('privacy', state
.get('default_privacy'));
220 map
.set('idempotencyKey', uuid());
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);
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:
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);
257 case COMPOSE_EMOJI_INSERT:
258 return insertEmoji(state
, action
.position
, action
.emoji
);