forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChartEditorPage.tsx
378 lines (349 loc) · 13.4 KB
/
ChartEditorPage.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
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
import * as React from "react"
import { observer } from "mobx-react"
import {
observable,
computed,
runInAction,
autorun,
action,
reaction,
IReactionDisposer,
} from "mobx"
import { Prompt, Redirect } from "react-router-dom"
import { Bounds } from "../clientUtils/Bounds"
import { capitalize } from "../clientUtils/Util"
import { Grapher } from "../grapher/core/Grapher"
import { Admin } from "./Admin"
import {
ChartEditor,
EditorDatabase,
Log,
PostReference,
ChartRedirect,
ChartEditorManager,
} from "./ChartEditor"
import { EditorBasicTab } from "./EditorBasicTab"
import { EditorDataTab } from "./EditorDataTab"
import { EditorTextTab } from "./EditorTextTab"
import { EditorCustomizeTab } from "./EditorCustomizeTab"
import { EditorScatterTab } from "./EditorScatterTab"
import { EditorMapTab } from "./EditorMapTab"
import { EditorHistoryTab } from "./EditorHistoryTab"
import { EditorReferencesTab } from "./EditorReferencesTab"
import { SaveButtons } from "./SaveButtons"
import { LoadingBlocker } from "./Forms"
import { AdminLayout } from "./AdminLayout"
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
import { faMobile } from "@fortawesome/free-solid-svg-icons/faMobile"
import { faDesktop } from "@fortawesome/free-solid-svg-icons/faDesktop"
import {
VisionDeficiency,
VisionDeficiencySvgFilters,
VisionDeficiencyDropdown,
VisionDeficiencyEntity,
} from "./VisionDeficiencies"
@observer
class TabBinder extends React.Component<{ editor: ChartEditor }> {
dispose!: IReactionDisposer
componentDidMount(): void {
//window.addEventListener("hashchange", this.onHashChange)
this.onHashChange()
this.dispose = autorun(() => {
//setTimeout(() => window.location.hash = `#${tab}-tab`, 100)
})
}
componentWillUnmount(): void {
//window.removeEventListener("hashchange", this.onHashChange)
this.dispose()
}
render(): null {
return null
}
@action.bound onHashChange(): void {
const match = window.location.hash.match(/#(.+?)-tab/)
if (match) {
const tab = match[1]
if (
this.props.editor.grapher &&
this.props.editor.availableTabs.includes(tab)
)
this.props.editor.tab = tab
}
}
}
@observer
export class ChartEditorPage
extends React.Component<{
grapherId?: number
newGrapherIndex?: number
grapherConfig?: any
}>
implements ChartEditorManager {
@observable.ref grapher = new Grapher()
@observable.ref database = new EditorDatabase({})
@observable logs: Log[] = []
@observable references: PostReference[] = []
@observable redirects: ChartRedirect[] = []
@observable.ref grapherElement?: JSX.Element
static contextType = AdminAppContext
context!: AdminAppContextType
@observable simulateVisionDeficiency?: VisionDeficiency
async fetchGrapher(): Promise<void> {
const { grapherId, grapherConfig } = this.props
const { admin } = this.context
const json =
grapherId === undefined
? grapherConfig
: await admin.getJSON(`/api/charts/${grapherId}.config.json`)
this.loadGrapherJson(json)
}
@observable private _isDbSet = false
@observable private _isGrapherSet = false
@computed get isReady(): boolean {
return this._isDbSet && this._isGrapherSet
}
@action.bound private loadGrapherJson(json: any): void {
this.grapherElement = (
<Grapher
{...{
...json,
bounds:
this.editor?.previewMode === "mobile"
? new Bounds(0, 0, 360, 500)
: new Bounds(0, 0, 800, 600),
getGrapherInstance: (grapher) => {
this.grapher = grapher
},
}}
/>
)
this._isGrapherSet = true
}
@action.bound private setDb(json: any): void {
this.database = new EditorDatabase(json)
this._isDbSet = true
}
async fetchData(): Promise<void> {
const { admin } = this.context
const json = await admin.getJSON(`/api/editorData/namespaces.json`)
this.setDb(json)
}
async fetchLogs(): Promise<void> {
const { grapherId } = this.props
const { admin } = this.context
const json =
grapherId === undefined
? []
: await admin.getJSON(`/api/charts/${grapherId}.logs.json`)
runInAction(() => (this.logs = json.logs))
}
async fetchRefs(): Promise<void> {
const { grapherId } = this.props
const { admin } = this.context
const json =
grapherId === undefined
? []
: await admin.getJSON(
`/api/charts/${grapherId}.references.json`
)
runInAction(() => (this.references = json.references || []))
}
async fetchRedirects(): Promise<void> {
const { grapherId } = this.props
const { admin } = this.context
const json =
grapherId === undefined
? []
: await admin.getJSON(`/api/charts/${grapherId}.redirects.json`)
runInAction(() => (this.redirects = json.redirects))
}
@computed get admin(): Admin {
return this.context.admin
}
@computed get editor(): ChartEditor | undefined {
if (!this.isReady) return undefined
return new ChartEditor({ manager: this })
}
@action.bound refresh(): void {
this.fetchGrapher()
this.fetchData()
this.fetchLogs()
this.fetchRefs()
this.fetchRedirects()
}
dispose!: IReactionDisposer
componentDidMount(): void {
this.refresh()
this.dispose = reaction(
() => this.editor && this.editor.previewMode,
() => {
if (this.editor) {
localStorage.setItem(
"editorPreviewMode",
this.editor.previewMode
)
}
}
)
}
// This funny construction allows the "new chart" link to work by forcing an update
// even if the props don't change
UNSAFE_componentWillReceiveProps(): void {
setTimeout(() => this.refresh(), 0)
}
componentWillUnmount(): void {
this.dispose()
}
render(): JSX.Element {
return (
<AdminLayout noSidebar>
<main className="ChartEditorPage">
{(this.editor === undefined ||
this.editor.currentRequest) && <LoadingBlocker />}
{this.editor !== undefined && this.renderReady(this.editor)}
</main>
</AdminLayout>
)
}
renderReady(editor: ChartEditor): JSX.Element {
const { grapher, availableTabs, previewMode } = editor
return (
<React.Fragment>
{!editor.newChartId && (
<Prompt
when={editor.isModified}
message="Are you sure you want to leave? Unsaved changes will be lost."
/>
)}
{editor.newChartId && (
<Redirect to={`/charts/${editor.newChartId}/edit`} />
)}
<TabBinder editor={editor} />
<div className="chart-editor-settings">
<div className="p-2">
<ul className="nav nav-tabs">
{availableTabs.map((tab) => (
<li key={tab} className="nav-item">
<a
className={
"nav-link" +
(tab === editor.tab
? " active"
: "")
}
onClick={() => (editor.tab = tab)}
>
{capitalize(tab)}
{tab === "refs" &&
this.references.length
? ` (${this.references.length})`
: ""}
</a>
</li>
))}
</ul>
</div>
<div className="innerForm container">
{editor.tab === "basic" && (
<EditorBasicTab editor={editor} />
)}
{editor.tab === "text" && (
<EditorTextTab editor={editor} />
)}
{editor.tab === "data" && (
<EditorDataTab editor={editor} />
)}
{editor.tab === "customize" && (
<EditorCustomizeTab editor={editor} />
)}
{editor.tab === "scatter" && (
<EditorScatterTab grapher={grapher} />
)}
{editor.tab === "map" && (
<EditorMapTab editor={editor} />
)}
{editor.tab === "revisions" && (
<EditorHistoryTab editor={editor} />
)}
{editor.tab === "refs" && (
<EditorReferencesTab editor={editor} />
)}
</div>
<SaveButtons editor={editor} />
</div>
<div className="chart-editor-view">
<figure
data-grapher-src
style={{
filter:
this.simulateVisionDeficiency &&
`url(#${this.simulateVisionDeficiency.id})`,
}}
>
{this.grapherElement}
</figure>
<div>
<div
className="btn-group"
data-toggle="buttons"
style={{ whiteSpace: "nowrap" }}
>
<label
className={
"btn btn-light" +
(previewMode === "mobile" ? " active" : "")
}
title="Mobile preview"
>
<input
type="radio"
onChange={action(
() => (editor.previewMode = "mobile")
)}
name="previewSize"
id="mobile"
checked={previewMode === "mobile"}
/>{" "}
<FontAwesomeIcon icon={faMobile} />
</label>
<label
className={
"btn btn-light" +
(previewMode === "desktop" ? " active" : "")
}
title="Desktop preview"
>
<input
onChange={action(
() => (editor.previewMode = "desktop")
)}
type="radio"
name="previewSize"
id="desktop"
checked={previewMode === "desktop"}
/>{" "}
<FontAwesomeIcon icon={faDesktop} />
</label>
</div>
<div
className="form-group d-inline-block"
style={{ width: 250, marginLeft: 15 }}
>
Emulate vision deficiency:{" "}
<VisionDeficiencyDropdown
onChange={action(
(option: VisionDeficiencyEntity) =>
(this.simulateVisionDeficiency =
option.deficiency)
)}
/>
</div>
</div>
{/* Include svg filters necessary for vision deficiency emulation */}
<VisionDeficiencySvgFilters />
</div>
</React.Fragment>
)
}
}