-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathEditorExportTab.tsx
409 lines (372 loc) · 14.5 KB
/
EditorExportTab.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
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
import { IReactionDisposer, action, autorun, computed, observable } from "mobx"
import { observer } from "mobx-react"
import { Component } from "react"
import { Section, Toggle } from "./Forms.js"
import { Grapher } from "@ourworldindata/grapher"
import {
triggerDownloadFromBlob,
GrapherStaticFormat,
} from "@ourworldindata/utils"
import { AbstractChartEditor } from "./AbstractChartEditor.js"
import { ETL_WIZARD_URL } from "../settings/clientSettings.js"
import { faHatWizard, faDownload } from "@fortawesome/free-solid-svg-icons"
import { Button } from "antd"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome/index.js"
import urljoin from "url-join"
type ExportSettings = Required<
Pick<
Grapher,
| "hideTitle"
| "forceHideAnnotationFieldsInTitle"
| "hideSubtitle"
| "hideNote"
| "hideOriginUrl"
| "shouldIncludeDetailsInStaticExport"
>
>
type OriginalGrapher = Pick<
Grapher,
| "currentTitle"
| "shouldAddEntitySuffixToTitle"
| "shouldAddTimeSuffixToTitle"
| "currentSubtitle"
| "note"
| "originUrl"
| "shouldIncludeDetailsInStaticExport"
| "detailsOrderedByReference"
>
type ExportSettingsByChartId = Record<number, ExportSettings>
type Extension = "png" | "svg"
type ExportFilename = `${string}.${Extension}`
const STORAGE_KEY = "chart-export-settings"
const DEFAULT_SETTINGS: ExportSettings = {
hideTitle: false,
forceHideAnnotationFieldsInTitle: {
entity: false,
time: false,
},
hideSubtitle: false,
hideNote: false,
hideOriginUrl: false,
shouldIncludeDetailsInStaticExport: false,
}
interface EditorExportTabProps<Editor> {
editor: Editor
}
@observer
export class EditorExportTab<
Editor extends AbstractChartEditor,
> extends Component<EditorExportTabProps<Editor>> {
@observable private settings = DEFAULT_SETTINGS
private originalSettings: Partial<ExportSettings> = DEFAULT_SETTINGS
private originalGrapher: OriginalGrapher
private disposers: IReactionDisposer[] = []
constructor(props: EditorExportTabProps<Editor>) {
super(props)
this.originalGrapher = this.grabRelevantPropertiesFromGrapher()
}
componentDidMount(): void {
this.saveOriginalSettings()
// needs to run before settings are loaded from session storage
const dispose = autorun(() => this.updateGrapher())
if (sessionStorage) {
this.loadSettingsFromSessionStorage()
}
this.disposers.push(dispose)
}
componentWillUnmount(): void {
this.resetGrapher()
if (sessionStorage) {
this.saveSettingsToSessionStorage()
}
this.disposers.forEach((dispose) => dispose())
}
private loadSettingsFromSessionStorage() {
const settingsByChartId = (loadJSONFromSessionStorage(STORAGE_KEY) ??
{}) as ExportSettingsByChartId
const settings = settingsByChartId[this.chartId]
if (settings) {
this.settings = settings
}
}
private saveSettingsToSessionStorage() {
const settingsByChartId = (loadJSONFromSessionStorage(STORAGE_KEY) ??
{}) as ExportSettingsByChartId
settingsByChartId[this.chartId] = this.settings
saveJSONToSessionStorage(STORAGE_KEY, settingsByChartId)
}
private saveOriginalSettings() {
this.originalSettings = {
hideTitle: this.grapher.hideTitle,
forceHideAnnotationFieldsInTitle:
this.grapher.forceHideAnnotationFieldsInTitle,
hideSubtitle: this.grapher.hideSubtitle,
hideNote: this.grapher.hideNote,
hideOriginUrl: this.grapher.hideOriginUrl,
shouldIncludeDetailsInStaticExport:
this.grapher.shouldIncludeDetailsInStaticExport,
}
}
// a deep clone of Grapher would be simpler and cleaner, but takes too long
private grabRelevantPropertiesFromGrapher(): OriginalGrapher {
return {
currentTitle: this.grapher.currentTitle,
shouldAddEntitySuffixToTitle:
this.grapher.shouldAddEntitySuffixToTitle,
shouldAddTimeSuffixToTitle: this.grapher.shouldAddTimeSuffixToTitle,
currentSubtitle: this.grapher.currentSubtitle,
note: this.grapher.note,
originUrl: this.grapher.originUrl,
shouldIncludeDetailsInStaticExport:
this.grapher.shouldIncludeDetailsInStaticExport,
detailsOrderedByReference: this.grapher.detailsOrderedByReference,
}
}
private resetGrapher() {
Object.assign(this.grapher, this.originalSettings)
}
private updateGrapher() {
Object.assign(this.grapher, this.settings)
}
@computed private get grapher(): Grapher {
return this.props.editor.grapher
}
@computed private get chartId(): number {
// the id is undefined for unsaved charts
return this.grapher.id ?? 0
}
@computed private get baseFilename(): string {
return this.props.editor.grapher.displaySlug
}
@action.bound private onDownloadDesktopSVG() {
void this.download(`${this.baseFilename}-desktop.svg`, {
format: GrapherStaticFormat.landscape,
})
}
@action.bound private onDownloadDesktopPNG() {
void this.download(`${this.baseFilename}-desktop.png`, {
format: GrapherStaticFormat.landscape,
})
}
@action.bound private onDownloadMobileSVG() {
void this.download(`${this.baseFilename}-mobile.svg`, {
format: GrapherStaticFormat.square,
})
}
@action.bound private onDownloadMobilePNG() {
void this.download(`${this.baseFilename}-mobile.png`, {
format: GrapherStaticFormat.square,
})
}
@action.bound private onDownloadMobileSVGForSocialMedia() {
void this.download(`${this.baseFilename}-instagram.svg`, {
format: GrapherStaticFormat.square,
isSocialMediaExport: true,
})
}
private async download(
filename: ExportFilename,
{
format,
isSocialMediaExport = false,
}: {
format: GrapherStaticFormat
isSocialMediaExport?: boolean
}
) {
try {
let grapher = this.grapher
if (
this.grapher.staticFormat !== format ||
this.grapher.isSocialMediaExport !== isSocialMediaExport
) {
grapher = new Grapher({
...this.grapher,
staticFormat: format,
selectedEntityNames:
this.grapher.selection.selectedEntityNames,
focusedSeriesNames: this.grapher.focusedSeriesNames,
isSocialMediaExport,
})
}
const { blob: pngBlob, svgBlob } = await grapher.rasterize()
if (filename.endsWith("svg") && svgBlob) {
triggerDownloadFromBlob(filename, svgBlob)
} else if (filename.endsWith("png") && pngBlob) {
triggerDownloadFromBlob(filename, pngBlob)
}
} catch (err) {
console.error(err)
}
}
render() {
const chartAnimationUrl = new URL(
urljoin(ETL_WIZARD_URL, "chart-animation")
)
if (this.grapher.canonicalUrl)
chartAnimationUrl.searchParams.set(
"animation_chart_url",
this.grapher.canonicalUrl
)
chartAnimationUrl.searchParams.set("animation_skip_button", "True")
// chartAnimationUrl.searchParams.set(
// "animation_chart_tab",
// this.grapher.tab ?? ""
// )
return (
<div className="EditorExportTab">
<Section name="Displayed elements">
{this.originalGrapher.currentTitle && (
<Toggle
label="Title"
value={!this.settings.hideTitle}
onValue={(value) =>
(this.settings.hideTitle = !value)
}
/>
)}
{this.originalGrapher.currentTitle &&
this.originalGrapher.shouldAddEntitySuffixToTitle && (
<Toggle
label="Title suffix: automatic entity"
value={
!this.settings
.forceHideAnnotationFieldsInTitle
?.entity
}
onValue={(value) =>
(this.settings.forceHideAnnotationFieldsInTitle.entity =
!value)
}
/>
)}
{this.originalGrapher.currentTitle &&
this.originalGrapher.shouldAddTimeSuffixToTitle && (
<Toggle
label="Title suffix: automatic time"
value={
!this.settings
.forceHideAnnotationFieldsInTitle?.time
}
onValue={(value) =>
(this.settings.forceHideAnnotationFieldsInTitle.time =
!value)
}
/>
)}
{this.originalGrapher.currentSubtitle && (
<Toggle
label="Subtitle"
value={!this.settings.hideSubtitle}
onValue={(value) =>
(this.settings.hideSubtitle = !value)
}
/>
)}
{this.originalGrapher.note && (
<Toggle
label="Note"
value={!this.settings.hideNote}
onValue={(value) =>
(this.settings.hideNote = !value)
}
/>
)}
{this.originalGrapher.originUrl &&
!this.grapher.isStaticAndSmall && (
<Toggle
label="Origin URL"
value={!this.settings.hideOriginUrl}
onValue={(value) =>
(this.settings.hideOriginUrl = !value)
}
/>
)}
{this.originalGrapher.detailsOrderedByReference.length >
0 && (
<Toggle
label="Details on demand"
value={
this.settings.shouldIncludeDetailsInStaticExport
}
onValue={(value) =>
(this.settings.shouldIncludeDetailsInStaticExport =
value)
}
/>
)}
</Section>
<Section name="Export static chart">
<div className="DownloadButtons">
<button
className="btn btn-primary"
onClick={this.onDownloadDesktopPNG}
>
{<FontAwesomeIcon icon={faDownload} />} Download
Desktop PNG
</button>
<button
className="btn btn-primary"
onClick={this.onDownloadDesktopSVG}
>
{<FontAwesomeIcon icon={faDownload} />} Download
Desktop SVG
</button>
<button
className="btn btn-primary"
onClick={this.onDownloadMobilePNG}
>
{<FontAwesomeIcon icon={faDownload} />} Download
Mobile PNG
</button>
<button
className="btn btn-primary"
onClick={this.onDownloadMobileSVG}
>
{<FontAwesomeIcon icon={faDownload} />} Download
Mobile SVG
</button>
<button
className="btn btn-primary"
onClick={this.onDownloadMobileSVGForSocialMedia}
>
{<FontAwesomeIcon icon={faDownload} />} Download
Mobile SVG for Social Media
</button>
</div>
</Section>
{/* Link to Wizard dataset preview */}
{this.grapher.isPublished && (
<Section name="Animate chart">
<a
href={chartAnimationUrl.toString()}
target="_blank"
className="btn btn-tertiary"
rel="noopener"
>
<Button
type="default"
icon={<FontAwesomeIcon icon={faHatWizard} />}
>
Animate with Wizard
</Button>
</a>
</Section>
)}
</div>
)
}
}
function loadJSONFromSessionStorage(key: string): unknown | undefined {
const rawJSON = sessionStorage.getItem(key)
if (!rawJSON) return undefined
try {
return JSON.parse(rawJSON)
} catch (err) {
console.error(err)
return undefined
}
}
function saveJSONToSessionStorage(key: string, value: any) {
sessionStorage.setItem(key, JSON.stringify(value))
}