/
TransUnitTranslationPanel.js
476 lines (446 loc) · 14.8 KB
/
TransUnitTranslationPanel.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// @ts-nocheck
import React from 'react'
import * as PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { injectIntl, intlShape } from 'react-intl'
import Textarea from 'react-textarea-autosize'
import TransUnitTranslationHeader from './TransUnitTranslationHeader'
import TransUnitTranslationFooter from './TransUnitTranslationFooter'
import { LoaderText } from '../../components'
import { pick } from 'lodash'
import {
phraseTextSelectionRange, validationError
} from '../actions/phrases-actions'
import {
getSyntaxHighlighting,
getValidateHtmlXml,
getValidateNewLine,
getValidateTab,
getValidateJavaVariables,
getValidateXmlEntity,
getValidatePrintfVariables,
getValidatePrintfXsi
} from '../reducers'
import SyntaxHighlighter, { registerLanguage }
from 'react-syntax-highlighter/light'
import Validation, { getValidationMessages } from './Validation/index.tsx'
import xml from 'react-syntax-highlighter/languages/hljs/xml'
import { atelierLakesideLight } from 'react-syntax-highlighter/styles/hljs'
registerLanguage('xml', xml)
const DO_NOT_RENDER = undefined
/**
* Panel to display and edit translations of a phrase.
*/
class TransUnitTranslationPanel extends React.Component {
static propTypes = {
cancelEdit: PropTypes.func.isRequired,
glossaryCount: PropTypes.number.isRequired,
glossaryVisible: PropTypes.bool.isRequired,
intl: intlShape.isRequired,
isRTL: PropTypes.bool.isRequired,
onSelectionChange: PropTypes.func.isRequired,
onValidationErrorChange: PropTypes.func.isRequired,
// the key of the currently open dropdown (may be undefined if none is open)
openDropdown: PropTypes.any,
permissions: PropTypes.shape({
reviewer: PropTypes.bool.isRequired,
translator: PropTypes.bool.isRequired
}).isRequired,
// FIXME use PropTypes.shape and include all used properties
phrase: PropTypes.object.isRequired,
saveAsMode: PropTypes.bool.isRequired,
// the key for the save dropdown for this translation panel. Can be compared
// with openDropdown to see whether this dropdown is open.
saveDropdownKey: PropTypes.any.isRequired,
savePhraseWithStatus: PropTypes.func.isRequired,
selected: PropTypes.bool.isRequired,
selectPhrasePluralIndex: PropTypes.func.isRequired,
showSuggestions: PropTypes.bool.isRequired,
suggestionCount: PropTypes.number.isRequired,
suggestionSearchType: PropTypes.oneOf(['phrase', 'text']).isRequired,
syntaxOn: PropTypes.bool.isRequired,
textChanged: PropTypes.func.isRequired,
toggleDropdown: PropTypes.func.isRequired,
toggleGlossary: PropTypes.func.isRequired,
toggleSuggestionPanel: PropTypes.func.isRequired,
translationLocale: PropTypes.shape({
id: PropTypes.string.isRequired,
name: PropTypes.string.isRequired
}).isRequired,
undoEdit: PropTypes.func.isRequired,
validationOptions: PropTypes.any
}
componentWillMount () {
// will be set by refs, initialize here to avoid need for null checks
this.textareas = []
// indicates when a textarea should be focused but was not rendered yet
// so should focus as soon as the rendered textarea is available
this.shouldFocus = false
}
componentDidMount () {
const { selected, phrase } = this.props
if (selected) {
// this is the selected row, focus if the textarea is available
// (should be available but in practice the ref is not set until later)
const selectedIndex = phrase.selectedPluralIndex || 0
const textarea = this.textareas[selectedIndex]
if (textarea) {
textarea.focus()
} else {
this.shouldFocus = true
}
}
}
componentDidUpdate (prevProps) {
const { phrase } = this.props
const becameSelected = this.props.selected && !prevProps.selected
// protective check to prevent repeatedly gaining focus
// phrase.shouldGainFocus has a different truthy value if it has been set
// again
const shouldGainFocus = phrase.shouldGainFocus &&
phrase.shouldGainFocus !== prevProps.phrase.shouldGainFocus
if (becameSelected || shouldGainFocus || this.shouldFocus) {
const selectedIndex = phrase.selectedPluralIndex || 0
const textarea = this.textareas[selectedIndex]
if (textarea) {
textarea.focus()
this.shouldFocus = false
} else {
this.shouldFocus = true
}
}
}
setTextArea = (index, ref) => {
this.textareas[index] = ref
}
render () {
const {
onSelectionChange,
phrase,
selected,
selectPhrasePluralIndex,
isRTL,
syntaxOn
} = this.props
var header, footer
const isPlural = phrase.plural
const directionClass = isRTL ? 'rtl' : 'ltr'
if (selected) {
const headerProps = pick(this.props, [
'cancelEdit',
'phrase',
'translationLocale',
'undoEdit'
])
header = <TransUnitTranslationHeader {...headerProps} />
const footerProps = pick(this.props, [
'glossaryCount',
'glossaryVisible',
'openDropdown',
'phrase',
'saveAsMode',
'saveDropdownKey',
'savePhraseWithStatus',
'showSuggestions',
'suggestionCount',
'suggestionSearchType',
'toggleDropdown',
'toggleGlossary',
'toggleSuggestionPanel',
'showRejectModal',
'permissions'
])
footer = <TransUnitTranslationFooter {...footerProps} />
}
const {
openDropdown,
onValidationErrorChange,
intl,
saveAsMode,
saveDropdownKey,
translationLocale,
textChanged,
permissions,
validationOptions } = this.props
const dropdownIsOpen = openDropdown === saveDropdownKey || saveAsMode
// TODO use dedicated phrase.isLoading variable when available
const isLoading = !phrase.newTranslations
const selectedPluralIndex = phrase.selectedPluralIndex || 0
let translations
let validationMsgs
if (isLoading) {
translations = <span className="u-textNeutral">
<LoaderText loading />
</span>
} else {
const newTranslations = phrase.newTranslations
? phrase.newTranslations
: ['Loading...']
translations = newTranslations.map(
(translation, index) => {
const validationProps = {
locale: intl.locale,
source: phrase.sources[0],
target: translation,
validationOptions
}
validationMsgs = getValidationMessages(validationProps)
const hasValidationErrors =
validationMsgs && validationMsgs.errorCount > 0
return (
<TranslationItem key={index}
dropdownIsOpen={dropdownIsOpen}
index={index}
isPlural={isPlural}
phrase={phrase}
onSelectionChange={onSelectionChange}
onValidationErrorChange={onValidationErrorChange}
selected={selected}
selectedPluralIndex={selectedPluralIndex}
selectPhrasePluralIndex={selectPhrasePluralIndex}
setTextArea={this.setTextArea}
translationLocale={translationLocale}
textChanged={textChanged}
translation={translation}
directionClass={directionClass}
syntaxOn={syntaxOn}
validationOptions={validationOptions}
validationMessages={validationMsgs}
permissions={permissions}
hasValidationErrors={hasValidationErrors} />
)
})
}
if (selected) {
const headerProps = pick(this.props, [
'cancelEdit',
'phrase',
'translationLocale',
'undoEdit'
])
header = <TransUnitTranslationHeader {...headerProps} />
const footerProps = {...pick(this.props, [
'glossaryCount',
'glossaryVisible',
'openDropdown',
'phrase',
'saveAsMode',
'saveDropdownKey',
'savePhraseWithStatus',
'showSuggestions',
'suggestionCount',
'suggestionSearchType',
'toggleDropdown',
'toggleGlossary',
'toggleSuggestionPanel',
'showRejectModal',
'permissions'
]), validationMessages: validationMsgs}
footer = <TransUnitTranslationFooter {...footerProps} />
}
return (
<div className="TransUnit-panel TransUnit-translation">
{header}
<span className={directionClass}>{translations}</span>
{footer}
</div>
)
}
}
export class TranslationItem extends React.Component {
static propTypes = {
dropdownIsOpen: PropTypes.bool.isRequired,
index: PropTypes.number.isRequired,
hasValidationErrors: PropTypes.bool,
isPlural: PropTypes.bool.isRequired,
onSelectionChange: PropTypes.func.isRequired,
onValidationErrorChange: PropTypes.func.isRequired,
phrase: PropTypes.shape({
id: PropTypes.any.isRequired,
sources: PropTypes.any.isRequired
}).isRequired,
selected: PropTypes.bool.isRequired,
selectedPluralIndex: PropTypes.number.isRequired,
selectPhrasePluralIndex: PropTypes.func.isRequired,
/* Set a reference to the textarea component, for use with focus
* setTextArea(index, ref)
*/
setTextArea: PropTypes.func.isRequired,
translationLocale: PropTypes.shape({
id: PropTypes.string.isRequired,
name: PropTypes.string.isRequired
}).isRequired,
textChanged: PropTypes.func.isRequired,
translation: PropTypes.string,
validationOptions: PropTypes.any.isRequired,
directionClass: PropTypes.string,
syntaxOn: PropTypes.bool.isRequired,
permissions: PropTypes.shape({
reviewer: PropTypes.bool.isRequired,
translator: PropTypes.bool.isRequired
}).isRequired,
validationMessages: PropTypes.any
}
componentDidUpdate (prevProps) {
const { phrase, hasValidationErrors, onValidationErrorChange } = this.props
if (phrase.errors !== hasValidationErrors) {
onValidationErrorChange(phrase.id, hasValidationErrors)
}
}
setTextArea = (ref) => {
this.props.setTextArea(this.props.index, ref)
}
_onChange = (event) => {
const { index, phrase, textChanged } = this.props
textChanged(phrase.id, index, event)
}
setFocusedPlural = () => {
const { index, phrase, selectPhrasePluralIndex } = this.props
selectPhrasePluralIndex(phrase.id, index)
}
render () {
const {
dropdownIsOpen,
index,
isPlural,
onSelectionChange,
selected,
selectedPluralIndex,
translationLocale,
translation,
directionClass,
permissions,
phrase,
validationMessages
} = this.props
// TODO make this translatable
const headerLabel = index === 0
? 'Singular Form'
: 'Plural Form'
const highlightHeader = selected && index === selectedPluralIndex
const headerClass = highlightHeader
? 'u-textMini txt-primary'
: 'u-textNeutral'
const itemHeader = isPlural &&
<div className="TransUnit-itemHeader">
<span className={headerClass}>
{headerLabel}
</span>
</div>
const syntaxStyle = {
padding: '0.5rem',
width: '90%',
whiteSpace: 'pre-wrap',
wordWrap: 'break-word'
}
const langAttr = phrase.transSourceType && phrase.transSourceType === 'MT'
? `${translationLocale.id}-x-mtfrom-und`
: translationLocale.id
const syntaxHighlighter = (this.props.syntaxOn && selected)
? <SyntaxHighlighter
lang={langAttr}
language='html'
style={atelierLakesideLight}
wrapLines
lineStyle={syntaxStyle}>
{translation}
</SyntaxHighlighter>
: DO_NOT_RENDER
const cantEditTranslation = !permissions.translator || dropdownIsOpen
return (
<div className="TransUnit-item" key={index}>
{itemHeader}
{/* TODO check that this does not trim strings */}
{/* TODO translate "Enter a translation..." */}
<Textarea
inputRef={this.setTextArea}
className={directionClass + ' TransUnit-text'}
disabled={cantEditTranslation}
rows={1}
value={translation}
lang={langAttr}
placeholder="Enter a translation…"
onFocus={this.setFocusedPlural}
onChange={this._onChange}
onSelect={onSelectionChange} />
{syntaxHighlighter}
{selected && <Validation {...validationMessages} />}
</div>
)
}
}
function mapStateToProps (state) {
const {ui, context, headerData} = state
const targetLocaleDetails = ui.uiLocales[context.lang]
return {
syntaxOn: getSyntaxHighlighting(state),
validationOptions: [
{
id: 'HTML_XML',
label: 'HTML/XML tags',
active: getValidateHtmlXml(state) === 'Warning',
disabled: getValidateHtmlXml(state) === 'Error'
},
{
id: 'NEW_LINE',
label: 'Leading/trailing newline',
active: getValidateNewLine(state) === 'Warning',
disabled: getValidateNewLine(state) === 'Error'
},
{
id: 'TAB',
label: 'Tab characters',
active: getValidateTab(state) === 'Warning',
disabled: getValidateTab(state) === 'Error'
},
{
id: 'JAVA_VARIABLES',
label: 'Java variables',
active: getValidateJavaVariables(state) === 'Warning',
disabled: getValidateJavaVariables(state) === 'Error'
},
{
id: 'XML_ENTITY',
label: 'XML entity reference',
active: getValidateXmlEntity(state) === 'Warning',
disabled: getValidateXmlEntity(state) === 'Error'
},
{
id: 'PRINTF_VARIABLES',
label: 'Printf variables',
active: getValidatePrintfVariables(state) === 'Warning',
disabled: getValidatePrintfVariables(state) === 'Error'
},
{
id: 'PRINTF_XSI_EXTENSION',
label: 'Positional printf (XSI extension)',
active: getValidatePrintfXsi(state) === 'Warning',
disabled: getValidatePrintfXsi(state) === 'Error'
}
],
isRTL: targetLocaleDetails ? targetLocaleDetails.isRTL || false
: false,
permissions: headerData.permissions
}
}
function mapDispatchToProps (dispatch, _ownProps) {
// TODO put all the branch-specific stuff here for a start
return {
onSelectionChange: (event) => {
const { selectionStart, selectionEnd } = event.target
event.stopPropagation()
// Do not update with empty ranges
if (selectionStart !== selectionEnd) {
dispatch(phraseTextSelectionRange(selectionStart, selectionEnd))
}
},
onValidationErrorChange: (phraseId, hasValidationErrors) => {
dispatch(validationError(phraseId, hasValidationErrors))
}
}
}
TransUnitTranslationPanel.contextTypes = {
intl: PropTypes.object
}
export default connect(
mapStateToProps, mapDispatchToProps)(injectIntl(TransUnitTranslationPanel))