-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathEditorTextTab.tsx
325 lines (311 loc) · 14.3 KB
/
EditorTextTab.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
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
import { faMinus, faPlus } from "@fortawesome/free-solid-svg-icons"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome/index.js"
import { LogoOption, RelatedQuestionsConfig } from "@ourworldindata/types"
import { getErrorMessageRelatedQuestionUrl } from "@ourworldindata/grapher"
import { slugify } from "@ourworldindata/utils"
import { action, computed } from "mobx"
import { observer } from "mobx-react"
import { Component } from "react"
import { isChartEditorInstance } from "./ChartEditor.js"
import {
AutoTextField,
BindAutoStringExt,
BindString,
Button,
RadioGroup,
Section,
TextField,
Toggle,
} from "./Forms.js"
import { AbstractChartEditor } from "./AbstractChartEditor.js"
import { ErrorMessages } from "./ChartEditorTypes.js"
import { isChartViewEditorInstance } from "./ChartViewEditor.js"
@observer
export class EditorTextTab<
Editor extends AbstractChartEditor,
> extends Component<{ editor: Editor; errorMessages: ErrorMessages }> {
@action.bound onSlug(slug: string) {
this.props.editor.grapher.slug = slugify(slug)
}
@action.bound onChangeLogo(value: string) {
if (value === "none") {
this.props.editor.grapher.hideLogo = true
} else {
this.props.editor.grapher.hideLogo = undefined
this.props.editor.grapher.logo = (value as LogoOption) || undefined
}
}
@action.bound onAddRelatedQuestion() {
const { grapher } = this.props.editor
if (!grapher.relatedQuestions) grapher.relatedQuestions = []
grapher.relatedQuestions.push({
text: "",
url: "",
})
}
@action.bound onRemoveRelatedQuestion(idx: number) {
const { grapher } = this.props.editor
if (!grapher.relatedQuestions) grapher.relatedQuestions = []
grapher.relatedQuestions.splice(idx, 1)
}
@action.bound onToggleTitleAnnotationEntity(value: boolean) {
const { grapher } = this.props.editor
grapher.hideAnnotationFieldsInTitle ??= {}
grapher.hideAnnotationFieldsInTitle.entity = value || undefined
}
@action.bound onToggleTitleAnnotationTime(value: boolean) {
const { grapher } = this.props.editor
grapher.hideAnnotationFieldsInTitle ??= {}
grapher.hideAnnotationFieldsInTitle.time = value || undefined
}
@action.bound onToggleTitleAnnotationChangeInPrefix(value: boolean) {
const { grapher } = this.props.editor
grapher.hideAnnotationFieldsInTitle ??= {}
grapher.hideAnnotationFieldsInTitle.changeInPrefix = value || undefined
}
@computed get errorMessages() {
return this.props.errorMessages
}
@computed get showChartSlug() {
return !isChartViewEditorInstance(this.props.editor)
}
@computed get showAnyAnnotationFieldInTitleToggle() {
const { features } = this.props.editor
return (
features.showEntityAnnotationInTitleToggle ||
features.showTimeAnnotationInTitleToggle ||
features.showChangeInPrefixToggle
)
}
render() {
const { editor } = this.props
const { grapher, features } = editor
const { relatedQuestions = [] } = grapher
return (
<div className="EditorTextTab">
<Section name="Header">
<BindAutoStringExt
label="Title"
readFn={(grapher) => grapher.displayTitle}
writeFn={(grapher, newVal) => (grapher.title = newVal)}
auto={
editor.couldPropertyBeInherited("title")
? editor.activeParentConfig!.title
: undefined
}
isAuto={
editor.isPropertyInherited("title") ||
grapher.title === undefined
}
store={grapher}
softCharacterLimit={100}
/>
{features.showEntityAnnotationInTitleToggle && (
<Toggle
label="Hide automatic entity"
value={
!!grapher.hideAnnotationFieldsInTitle?.entity
}
onValue={this.onToggleTitleAnnotationEntity}
/>
)}
{features.showTimeAnnotationInTitleToggle && (
<Toggle
label="Hide automatic time"
secondaryLabel={
"Grapher makes sure to include the current time in the title if " +
"omitting it would lead to SVG exports with no reference " +
"to the time. In such cases, your preference is ignored."
}
value={!!grapher.hideAnnotationFieldsInTitle?.time}
onValue={this.onToggleTitleAnnotationTime}
/>
)}
{features.showChangeInPrefixToggle && (
<Toggle
label="Don't prepend 'Change in' in relative line charts"
value={
!!grapher.hideAnnotationFieldsInTitle
?.changeInPrefix
}
onValue={this.onToggleTitleAnnotationChangeInPrefix}
/>
)}
{this.showAnyAnnotationFieldInTitleToggle && <hr />}
{this.showChartSlug && (
<AutoTextField
label="/grapher"
value={grapher.displaySlug}
onValue={this.onSlug}
isAuto={grapher.slug === undefined}
onToggleAuto={() =>
(grapher.slug =
grapher.slug === undefined
? grapher.displaySlug
: undefined)
}
helpText="Human-friendly URL for this chart"
/>
)}
<BindAutoStringExt
label="Subtitle"
readFn={(grapher) => grapher.currentSubtitle}
writeFn={(grapher, newVal) =>
(grapher.subtitle = newVal)
}
auto={
editor.couldPropertyBeInherited("subtitle")
? editor.activeParentConfig!.subtitle
: undefined
}
isAuto={
editor.isPropertyInherited("subtitle") ||
grapher.subtitle === undefined
}
store={grapher}
placeholder="Briefly describe the context of the data. It's best to avoid duplicating any information which can be easily inferred from other visual elements of the chart."
textarea
softCharacterLimit={280}
errorMessage={this.errorMessages.subtitle}
/>
<RadioGroup
label="Logo"
options={[
{ label: "OWID", value: "owid" },
{ label: "CORE+OWID", value: "core+owid" },
{ label: "GV+OWID", value: "gv+owid" },
{ label: "No logo", value: "none" },
]}
value={
grapher.hideLogo ? "none" : grapher.logo || "owid"
}
onChange={this.onChangeLogo}
/>
</Section>
<Section name="Footer">
<BindAutoStringExt
label="Source"
readFn={(grapher) => grapher.sourcesLine}
writeFn={(grapher, newVal) =>
(grapher.sourceDesc = newVal)
}
auto={
editor.couldPropertyBeInherited("sourceDesc")
? editor.activeParentConfig!.sourceDesc
: undefined
}
isAuto={
editor.isPropertyInherited("sourceDesc") ||
grapher.sourceDesc === undefined
}
store={grapher}
helpText="Short comma-separated list of source names"
softCharacterLimit={60}
/>
<BindString
label="Origin url"
field="originUrl"
store={grapher}
placeholder={grapher.originUrlWithProtocol}
helpText="The page containing this chart where more context can be found"
/>
{isChartEditorInstance(editor) &&
editor.references &&
(editor.references.postsWordpress?.length ||
editor.references.postsGdocs?.length) && (
<div className="originSuggestions">
<p>Origin url suggestions</p>
<ul>
{[
...(editor.references.postsWordpress ??
[]),
...(editor.references.postsGdocs ?? []),
].map((post) => (
<li key={post.id}>{post.url}</li>
))}
</ul>
</div>
)}
<BindAutoStringExt
label="Footer note"
readFn={(grapher) => grapher.note ?? ""}
writeFn={(grapher, newVal) => (grapher.note = newVal)}
auto={
editor.couldPropertyBeInherited("note")
? editor.activeParentConfig?.note
: undefined
}
isAuto={editor.isPropertyInherited("note")}
store={grapher}
helpText="Any important clarification needed to avoid miscommunication"
softCharacterLimit={140}
errorMessage={this.errorMessages.note}
textarea
/>
</Section>
<Section name="Related">
{relatedQuestions.map(
(question: RelatedQuestionsConfig, idx: number) => (
<div key={idx}>
<TextField
label="Related question"
value={question.text}
onValue={action((value: string) => {
question.text = value
})}
placeholder="e.g. How did countries respond to the pandemic?"
helpText="Short question promoting exploration of related content"
softCharacterLimit={50}
/>
{question.text && (
<TextField
label="URL"
value={question.url}
onValue={action((value: string) => {
question.url = value
})}
placeholder="e.g. https://ourworldindata.org/coronavirus"
helpText="Page or section of a page where the answer to the previous question can be found."
errorMessage={getErrorMessageRelatedQuestionUrl(
question
)}
/>
)}
<Button
onClick={() =>
this.onRemoveRelatedQuestion(idx)
}
>
<FontAwesomeIcon icon={faMinus} /> Remove
related question
</Button>
</div>
)
)}
{!relatedQuestions.length && (
<Button onClick={this.onAddRelatedQuestion}>
<FontAwesomeIcon icon={faPlus} /> Add related
question
</Button>
)}
</Section>
<Section name="Misc">
<BindString
label="Internal author notes"
field="internalNotes"
store={grapher}
placeholder="e.g. WIP, needs review, etc"
textarea
/>
<BindString
label="Variant name"
field="variantName"
store={grapher}
placeholder="e.g. IHME"
helpText="Optional variant name for distinguishing charts with the same title"
/>
</Section>
</div>
)
}
}