forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditorExportTab.tsx
354 lines (321 loc) · 12 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
import { IReactionDisposer, action, autorun, computed, observable } from "mobx"
import { observer } from "mobx-react"
import React from "react"
import { ChartEditor } from "./ChartEditor.js"
import { Section, Toggle } from "./Forms.js"
import { Grapher } from "@ourworldindata/grapher"
import {
triggerDownloadFromBlob,
GrapherStaticFormat,
} from "@ourworldindata/utils"
type ExportSettings = Required<
Pick<
Grapher,
| "hideTitle"
| "forceHideAnnotationFieldsInTitle"
| "hideSubtitle"
| "hideLogo"
| "hideNote"
| "hideOriginUrl"
| "shouldIncludeDetailsInStaticExport"
>
>
type OriginalGrapher = Pick<
Grapher,
| "currentTitle"
| "shouldAddEntitySuffixToTitle"
| "shouldAddTimeSuffixToTitle"
| "currentSubtitle"
| "hideLogo"
| "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,
hideLogo: false,
hideNote: false,
hideOriginUrl: false,
shouldIncludeDetailsInStaticExport: false,
}
interface EditorExportTabProps {
editor: ChartEditor
}
@observer
export class EditorExportTab extends React.Component<EditorExportTabProps> {
@observable private settings = DEFAULT_SETTINGS
private originalSettings: Partial<ExportSettings> = DEFAULT_SETTINGS
private originalGrapher: OriginalGrapher
private disposers: IReactionDisposer[] = []
constructor(props: EditorExportTabProps) {
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,
hideLogo: this.grapher.hideLogo,
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,
hideLogo: this.grapher.hideLogo,
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`,
GrapherStaticFormat.landscape
)
}
@action.bound private onDownloadDesktopPNG() {
void this.download(
`${this.baseFilename}-desktop.png`,
GrapherStaticFormat.landscape
)
}
@action.bound private onDownloadMobileSVG() {
void this.download(
`${this.baseFilename}-mobile.svg`,
GrapherStaticFormat.square
)
}
@action.bound private onDownloadMobilePNG() {
void this.download(
`${this.baseFilename}-mobile.png`,
GrapherStaticFormat.square
)
}
private async download(
filename: ExportFilename,
format: GrapherStaticFormat
) {
try {
let grapher = this.grapher
if (this.grapher.staticFormat !== format) {
grapher = new Grapher({
...this.grapher,
staticFormat: format,
})
}
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() {
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.hideLogo && (
<Toggle
label="Logo"
value={!this.settings.hideLogo}
onValue={(value) =>
(this.settings.hideLogo = !value)
}
/>
)}
{this.originalGrapher.note && (
<Toggle
label="Note"
value={!this.settings.hideNote}
onValue={(value) =>
(this.settings.hideNote = !value)
}
/>
)}
{this.originalGrapher.originUrl && (
<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}
>
Download Desktop PNG
</button>
<button
className="btn btn-primary"
onClick={this.onDownloadDesktopSVG}
>
Download Desktop SVG
</button>
<button
className="btn btn-primary"
onClick={this.onDownloadMobilePNG}
>
Download Mobile PNG
</button>
<button
className="btn btn-primary"
onClick={this.onDownloadMobileSVG}
>
Download Mobile SVG
</button>
</div>
</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))
}