/
TranslationCard.tsx
238 lines (210 loc) · 8.39 KB
/
TranslationCard.tsx
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
import EditorJS from "@editorjs/editorjs";
import List from "@editorjs/list";
import { Button, Icon, message, Select } from "antd";
import TextArea from "antd/lib/input/TextArea";
import * as React from "react";
import styled from "styled-components";
import { APIUtils } from "../../../api/v1/APIUtils";
import { TranslationsAPI } from "../../../api/v1/TranslationsAPI";
import FlagIcon from "../../../ui/FlagIcons";
import { Styles } from "../../../ui/Styles";
import { Utils } from "../../../ui/Utils";
const EMPTY_EDITOR_HEIGHT = 108;
const EditorWrapper = styled.div`
background: #fff;
border: 1px solid #e8e8e8;
padding: 12px 24px 12px 40px;
border-radius: ${Styles.DEFAULT_BORDER_RADIUS}px;
transition: all 0.3s;
&:hover {
border: 1px solid ${Styles.COLOR_PRIMARY};
}
`;
type IProps = {
projectId: string;
languagesResponse: any;
defaultSelected: any;
keyResponse: any;
hideLanguageSelection?: boolean;
hideSaveButton?: boolean;
exportConfigId?: string;
onChange?(changed: boolean);
onSave?();
};
type IState = {
selectedLanguage: string;
editorContentChanged: boolean;
editorLoaded: boolean;
textareaContentChanged: boolean;
isHTMLKey: boolean;
translationForLanguage: string;
content: string;
};
class TranslationCard extends React.Component<IProps, IState> {
state: IState = {
selectedLanguage: this.props.defaultSelected,
editorContentChanged: false,
editorLoaded: false,
textareaContentChanged: false,
isHTMLKey: true,
translationForLanguage: null,
content: null
};
editor: any;
async componentDidMount() {
await this.loadData();
}
loadData = async () => {
const translationForLanguage = await this.getTranslationForLanguage(this.state.selectedLanguage);
const isHTMLKey = this.props.keyResponse.data.attributes.html_enabled;
if (isHTMLKey) {
let htmlData;
try {
htmlData = JSON.parse(translationForLanguage);
} catch (e) {
//
}
this.editor = new EditorJS({
holder: `codex-editor-${this.state.selectedLanguage}`,
minHeight: 40,
tools: {
list: {
class: List,
inlineToolbar: true
}
},
onChange: () => {
if (this.props.onChange) {
this.props.onChange(true);
}
this.setState({ editorContentChanged: true });
},
data: isHTMLKey ? Utils.escapeEditorContent(htmlData) : undefined
});
await this.editor.isReady;
} else {
this.setState({ content: translationForLanguage });
}
this.setState({
editorLoaded: true,
isHTMLKey: isHTMLKey,
translationForLanguage: translationForLanguage
});
}
getTranslationForLanguage = (languageId: string) => {
let translationForLanguage;
this.props.keyResponse.data.relationships.translations.data.map((translationReference) => {
const translation = APIUtils.getIncludedObject(translationReference, this.props.keyResponse.included);
if (translation.relationships.language.data.id === languageId) {
if (translation.relationships.export_config.data) {
if (translation.relationships.export_config.data.id === this.props.exportConfigId) {
translationForLanguage = translation.attributes.content;
}
} else {
translationForLanguage = translation.attributes.content;
}
}
});
return translationForLanguage;
}
isHTMLKey = () => {
return this.state.isHTMLKey;
}
contentChanged = () => {
return this.state.editorContentChanged || this.state.textareaContentChanged;
}
getEditorLoadingOverlay = () => {
return (
<div style={{ width: "100%", height: "100%", position: "absolute", left: 0, top: 0, display: "flex", justifyContent: "center", alignItems: "center" }}>
<Icon type="loading" style={{ fontSize: 24, margin: "auto" }} spin />
</div>
);
}
saveChanges = async () => {
let promise;
if (this.state.isHTMLKey) {
promise = this.editor.save();
} else {
promise = Promise.resolve(this.state.content);
}
return promise.then(async (content) => {
if (this.state.isHTMLKey) {
content = JSON.stringify(content);
}
await TranslationsAPI.updateTranslation({
projectId: this.props.projectId,
languageId: this.state.selectedLanguage,
keyId: this.props.keyResponse.data.id,
exportConfigId: this.props.exportConfigId,
content: content
});
if (this.props.onSave) {
this.props.onSave();
}
this.setState({
editorContentChanged: false,
textareaContentChanged: false
});
}).catch((error) => {
console.error(error);
message.error("Failed to save changes.");
});
}
render() {
return (
<div style={{ marginBottom: 24, width: "100%" }}>
<div style={{ marginBottom: 8, display: "flex" }}>
{!this.props.hideLanguageSelection && <Select
style={{ flexGrow: 1, maxWidth: 200 }}
onChange={async (selectedValue: string) => {
this.setState({
selectedLanguage: selectedValue
}, this.loadData);
}}
value={this.state.selectedLanguage}
>
{this.props.languagesResponse && this.props.languagesResponse.data.map((language) => {
const countryCode = APIUtils.getIncludedObject(language.relationships.country_code.data, this.props.languagesResponse.included);
return <Select.Option value={language.id} key={language.attributes.name}>
{countryCode && <span style={{ marginRight: 8 }}>
<FlagIcon code={countryCode.attributes.code.toLowerCase()} />
</span>}
{language.attributes.name}
</Select.Option>;
})}
</Select>}
{!this.props.hideSaveButton && <Button
type="primary"
disabled={!this.contentChanged()}
style={{ marginLeft: "auto" }}
onClick={this.saveChanges}
>
Save changes
</Button>}
</div>
{this.isHTMLKey() ?
<div style={{ position: "relative" }}>
<EditorWrapper style={{ opacity: this.state.editorLoaded ? 1 : 0, height: this.state.editorLoaded ? undefined : EMPTY_EDITOR_HEIGHT }}>
<div id={`codex-editor-${this.state.selectedLanguage}`} />
</EditorWrapper>
{!this.state.editorLoaded && this.getEditorLoadingOverlay()}
</div> :
<TextArea
autosize={{ minRows: 4, maxRows: 6 }}
placeholder="Language translation content"
onChange={(event) => {
const changed = event.target.value !== this.state.translationForLanguage;
this.setState({ textareaContentChanged: changed });
if (this.props.onChange) {
this.props.onChange(changed);
}
this.setState({ content: event.target.value });
}}
value={this.state.content}
/>
}
</div>
);
}
}
export { TranslationCard };