-
Notifications
You must be signed in to change notification settings - Fork 51
/
index.js
442 lines (387 loc) · 13.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import updateObject from 'immutability-helper'
import phraseFilterReducer, { defaultState as defaultFilterState }
from './phrase-filter-reducer'
import { composeReducers, subReducer } from 'redux-sac'
import {
CLAMP_PAGE,
UPDATE_PAGE
} from '../../actions/controls-header-actions'
import { COPY_GLOSSARY_TERM } from '../../actions/glossary-action-types'
import {
CANCEL_EDIT,
COPY_FROM_ALIGNED_SOURCE,
COPY_FROM_SOURCE,
PHRASE_DETAIL_REQUEST,
PHRASE_LIST_REQUEST,
PHRASE_LIST_SUCCESS,
PHRASE_LIST_FAILURE,
PENDING_SAVE_INITIATED,
PHRASE_DETAIL_SUCCESS,
PHRASE_TEXT_SELECTION_RANGE,
QUEUE_SAVE,
SAVE_FINISHED,
SAVE_INITIATED,
SELECT_PHRASE,
SELECT_PHRASE_SPECIFIC_PLURAL,
TRANSLATION_TEXT_INPUT_CHANGED,
UNDO_EDIT
} from '../../actions/phrases-action-types'
import { COPY_SUGGESTION } from '../../actions/suggestions-action-types'
import {
getFilteredPhrases,
getHasAdvancedFilter,
getMaxPageIndex
} from '../../selectors'
import { hasAdvancedFilter } from '../../utils/filter-util'
import { replaceRange } from '../../utils/string-utils'
import { SET_SAVE_AS_MODE } from '../../actions/key-shortcuts-actions'
import { MOVE_NEXT, MOVE_PREVIOUS
} from '../../actions/phrase-navigation-actions'
import { findIndex } from 'lodash'
// FIXME this reducer is too big. See if it can be split up.
// TODO use lodash when upgraded
// clamps a number within the inclusive lower and upper bounds
function clamp (number, lower, upper) {
return Math.max(lower, Math.min(number, upper))
}
const defaultState = {
fetchingList: false,
fetchingFilteredList: false,
filteredListTimestamp: new Date(0),
fetchingDetail: false,
saveAsMode: false,
// expected shape: { [docId1]: [{ id, resId, status }, ...], [docId2]: [...] }
inDoc: {},
inDocFiltered: {},
// expected shape: { [phraseId1]: phrase-object, [phraseId2]: ..., ...}
detail: {},
selectedPhraseId: undefined,
/* Cursor/selection position within the currently editing translation, used
* for inserting terms from glossary etc. */
selectedTextRange: {
start: 0,
end: 0
},
paging: {
countPerPage: 20,
pageIndex: 0
},
filter: defaultFilterState
}
export const phraseReducer = (state = defaultState, action) => {
switch (action.type) {
case CLAMP_PAGE:
return update({
paging: {
pageIndex: {$set: clamp(state.paging.pageIndex, 0,
getMaxPageIndex(action.getState()))}
}
})
case UPDATE_PAGE:
return updatePageIndex(action.payload)
case CANCEL_EDIT:
// Discard any newTranslations that were entered.
const currentTrans = state.detail[state.selectedPhraseId].translations
return update({
selectedPhraseId: {$set: undefined},
detail: {
[state.selectedPhraseId]: {
newTranslations: {$set: currentTrans}
}
}
})
case COPY_FROM_ALIGNED_SOURCE:
return updatePhrase(state.selectedPhraseId, {$apply: (phrase) => {
return copyFromSource(phrase, phrase.selectedPluralIndex)
}})
case COPY_FROM_SOURCE:
const { phraseId, sourceIndex } = action
return updatePhrase(phraseId, {$apply: (phrase) => {
return copyFromSource(phrase, sourceIndex)
}})
case COPY_GLOSSARY_TERM:
return updatePhrase(state.selectedPhraseId, {$apply: phrase => {
return insertTextAtRange(phrase, action.payload,
state.selectedTextRange)
}})
case COPY_SUGGESTION:
const { suggestion } = action
return updatePhrase(state.selectedPhraseId, {$apply: phrase => {
return copyFromSuggestion(phrase, suggestion)
}})
case PHRASE_DETAIL_REQUEST:
return update({
fetchingDetail: {$set: true}
})
case PHRASE_LIST_REQUEST:
if (action.meta.filter) {
return update({
fetchingFilteredList: {$set: true}
})
} else {
return update({
fetchingList: {$set: true}
})
}
case PENDING_SAVE_INITIATED:
return updatePhrase(action.phraseId, {
pendingSave: {$set: undefined}
})
case PHRASE_LIST_SUCCESS:
if (action.meta.filter) {
if (action.meta.timestamp > state.filteredListTimestamp) {
return update({
fetchingFilteredList: {$set: false},
filteredListTimestamp: {$set: action.meta.timestamp},
inDocFiltered: {
[action.payload.docId]: {$set: action.payload.phraseList}
}
})
} else {
// stale search, ignore results
return state
}
} else {
const showingFiltered = getHasAdvancedFilter({ phrases: state })
const selectedPhraseId = showingFiltered
// this list not visible, keep same value
? state.selectedPhraseId
// list is showing, select a visible phrase
: decideSelectedPhrase(state, action.payload.phraseList)
return update({
fetchingList: {$set: false},
inDoc: {[action.payload.docId]: {$set: action.payload.phraseList}},
selectedPhraseId: {$set: selectedPhraseId}
})
}
case PHRASE_LIST_FAILURE:
if (action.meta.filter) {
return update({
fetchingFilteredList: {$set: false}
})
} else {
return update({
fetchingList: {$set: false}
})
}
case PHRASE_DETAIL_SUCCESS:
// TODO this shallow merge will lose data from other locales
// ideally replace source and locale that was looked up, leaving
// others unchanged (depending on caching policy)
return update({
fetchingDetail: {$set: false},
detail: {$merge: action.payload}
})
case PHRASE_TEXT_SELECTION_RANGE:
return update({
selectedTextRange: {$set: action.payload}
})
case QUEUE_SAVE:
return updatePhrase(action.phraseId, {
pendingSave: {$set: action.saveInfo}
})
case SAVE_FINISHED:
const phrase = state.detail[action.phraseId]
const { newTranslations } = phrase
return updatePhrase(action.phraseId, {
inProgressSave: {$set: undefined},
// FIXME check whether this should be action.translations instead
translations: {$set: newTranslations},
// TODO same as inProgressSave.status unless the server adjusted it
status: {$set: action.status},
revision: {$set: action.revision}
})
case SAVE_INITIATED:
return updatePhrase(action.phraseId, {
inProgressSave: {$set: action.saveInfo}
})
case SELECT_PHRASE:
return selectPhrase(state, action.phraseId)
case SELECT_PHRASE_SPECIFIC_PLURAL:
const withNewPluralIndex = updatePhrase(action.phraseId, {
selectedPluralIndex: {$set: action.index}
})
return selectPhrase(withNewPluralIndex, action.phraseId)
case SET_SAVE_AS_MODE:
return update({
saveAsMode: {$set: action.payload}
})
case TRANSLATION_TEXT_INPUT_CHANGED:
return update({
detail: {
[action.id]: {
newTranslations: {
[action.index]: {$set: action.text}
}
}
}
})
case UNDO_EDIT:
return updatePhrase(state.selectedPhraseId, {$apply: (phrase) => {
return updateObject(phrase, {
newTranslations: {$set: [...phrase.translations]}
})
}})
case MOVE_NEXT:
return changeSelectedIndex(index => index + 1)
case MOVE_PREVIOUS:
return changeSelectedIndex(index => index - 1)
}
return state
/**
* Apply the given commands to state.
*
* Just a shortcut to avoid having to pass state to update over and over.
*/
function update (commands) {
// FIXME update to version that does not lose reference equality when
// setting an identical object
// see: https://github.com/facebook/react/pull/4968
return updateObject(state, commands)
}
/**
* Apply commands to the indicated phrase detail.
*
* Returns state with just the indicated phrase changed.
*/
function updatePhrase (phraseId, commands) {
return update({
detail: {
[phraseId]: {$apply: (phrase) => {
return updateObject(phrase, commands)
}}
}
})
}
function updatePageIndex (newPageIndex) {
const oldPageIndex = state.paging.pageIndex
return oldPageIndex === newPageIndex
? state
: update({ paging: { pageIndex: {$set: newPageIndex} } })
}
/**
* Generate a state with a different phrase selected.
*
* @param state
* @param action
* @param indexUpdateCallback produces new index based on previous index
* @returns {*}
*/
function changeSelectedIndex (indexUpdateCallback) {
const { docId } = action.getState().context
const { inDoc, inDocFiltered, filter, selectedPhraseId } = state
const phrases = hasAdvancedFilter(filter.advanced)
? inDocFiltered[docId] : inDoc[docId]
const currentIndex = phrases.findIndex(x => x.id === selectedPhraseId)
const newIndex = indexUpdateCallback(currentIndex)
const indexOutOfBounds = newIndex < 0 || newIndex >= phrases.length
if (!indexOutOfBounds && newIndex !== currentIndex) {
const moveToId = phrases[newIndex].id
return selectPhrase(state, moveToId)
}
return state
}
/**
* Select a given phrase and ensure the correct page is showing.
*
* CAUTION: this calculates max page index from the previous state,
* so do not use this after updating page index.
*/
function selectPhrase (state, phraseId) {
const { countPerPage, pageIndex } = state.paging
const phrases = getFilteredPhrases(action.getState())
const phraseIndex = phrases.findIndex(x => x.id === phraseId)
const desiredPageIndex = phraseIndex === -1
// Just go to valid page nearest current page when selected phrase is
// invisible. Ideal would be page that shows the phrase nearest the
// selected one in the unfiltered document, but that is complicated.
? clamp(pageIndex, 0, getMaxPageIndex(action.getState()))
: Math.floor(phraseIndex / countPerPage)
return updateObject(state, {
selectedPhraseId: {$set: phraseId},
paging: {
pageIndex: {$set: desiredPageIndex}
}
})
}
}
function copyFromSource (phrase, sourceIndex) {
const { selectedPluralIndex, sources, shouldGainFocus } = phrase
const focusedTranslationIndex = selectedPluralIndex || 0
// FIXME use clamp from lodash (when lodash >= 4.0)
const sourceIndexToCopy =
sourceIndex < sources.length
? sourceIndex
: sources.length - 1
const sourceToCopy = sources[sourceIndexToCopy]
const focusId = (shouldGainFocus || 0) + 1
return updateObject(phrase, {
shouldGainFocus: {$set: focusId},
newTranslations: {
// $splice represents an array of calls to Array.prototype.splice
// with an array of params for each call
$splice: [[focusedTranslationIndex, 1, sourceToCopy]]
}
})
}
function copyFromSuggestion (phrase, suggestion) {
const { selectedPluralIndex, shouldGainFocus } = phrase
var targets = suggestion.targetContents
// TODO ensure selectedPluralIndex is always set (in phrase reducer),
// so the default of 0 is only sepecified in one place.
const focusedTranslationIndex = selectedPluralIndex || 0
const targetIndexToCopy = focusedTranslationIndex < targets.length
? focusedTranslationIndex : targets.length - 1
const focusId = (shouldGainFocus || 0) + 1
return updateObject(phrase, {
shouldGainFocus: {$set: focusId},
newTranslations: {
// $splice represents an array of calls to Array.prototype.splice
// with an array of params for each call
$splice: [[focusedTranslationIndex, 1, targets[targetIndexToCopy]]]
}
})
}
function insertTextAtRange (phrase, text, {start, end}) {
const { newTranslations, selectedPluralIndex, shouldGainFocus } = phrase
const focusedTranslationIndex = selectedPluralIndex || 0
const original = newTranslations[focusedTranslationIndex]
const updated = replaceRange(original, text, start, end)
// TODO set selected index as end of inserted range
// (likely needs to use refs on the component)
// const cursorPositionAfter = start + text.length
// The textarea is focused when shouldGainFocus has a changed truthy value
// so incrementing will lead to focus being gained.
const focusId = (shouldGainFocus || 0) + 1
return updateObject(phrase, {
shouldGainFocus: {$set: focusId},
newTranslations: {
// $splice represents an array of calls to Array.prototype.splice
// with an array of params for each call
$splice: [[focusedTranslationIndex, 1, updated]]
}
})
}
/* Decide which phrase should be selected out of a given phrase list
*
* Considers which phrase was selected and which page is showing.
* Does not filter the list, so pass in filtered phrase list if that is showing.
*/
function decideSelectedPhrase (state, phraseList) {
const { selectedPhraseId, paging: { countPerPage, pageIndex } } = state
const selectedPhraseIndex =
findIndex(phraseList, ({ id }) => id === selectedPhraseId)
if (selectedPhraseIndex > -1) {
// A present phrase is already selected, just may be wrong page number
// TODO set page number? Would need to return full state
return selectedPhraseId
}
const maxPageIndex = Math.ceil(phraseList.length / countPerPage) - 1
const clampedPageIndex = Math.min(pageIndex, maxPageIndex)
const firstIndex = clampedPageIndex * countPerPage
return phraseList[firstIndex].id
}
export default composeReducers(
phraseReducer,
subReducer('filter', phraseFilterReducer)
)