forked from keplergl/kepler.gl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport-utils.ts
More file actions
169 lines (140 loc) · 4.75 KB
/
Copy pathexport-utils.ts
File metadata and controls
169 lines (140 loc) · 4.75 KB
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
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project
import {Blob, URL, atob, Uint8Array, ArrayBuffer, document} from 'global/window';
import get from 'lodash/get';
import {ExportImage} from '@kepler.gl/types';
import {generateHashId} from '@kepler.gl/common-utils';
import domtoimage from './dom-to-image';
import {set} from './utils';
import {exportMapToHTML} from './export-map-html';
import {getApplicationConfig} from './application-config';
export function isMSEdge(window: Window): boolean {
// @ts-ignore msSaveOrOpenBlob was a proprietary addition to the Navigator object, added by Microsoft for Internet Explorer.
return Boolean(window.navigator && window.navigator.msSaveOrOpenBlob);
}
export function getScaleFromImageSize(imageW = 0, imageH = 0, mapW = 0, mapH = 0) {
if ([imageW, imageH, mapW, mapH].some(d => d <= 0)) {
return 1;
}
const base = imageW / imageH > 1 ? imageW : imageH;
const mapBase = imageW / imageH > 1 ? mapW : mapH;
return base / mapBase;
}
export function calculateExportImageSize({}: {}) {
return {
scale: 1,
imageW: 0,
imageH: 0
};
}
export function convertToPng(sourceElem: HTMLElement, options) {
return domtoimage.toPng(sourceElem, options);
}
export function dataURItoBlob(dataURI: string): Blob {
const binary = atob(dataURI.split(',')[1]);
// separate out the mime component
const mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to an ArrayBuffer
const ab = new ArrayBuffer(binary.length);
// create a view into the buffer
const ia = new Uint8Array(ab);
for (let i = 0; i < binary.length; i++) {
ia[i] = binary.charCodeAt(i);
}
return new Blob([ab], {type: mimeString});
}
export function downloadFile(fileBlob: Blob, fileName: string) {
if (isMSEdge(window)) {
(window.navigator as any).msSaveOrOpenBlob(fileBlob, fileName);
} else {
const url = URL.createObjectURL(fileBlob);
const link = document.createElement('a');
link.setAttribute('href', url);
link.setAttribute('download', fileName);
document.body.appendChild(link);
// in some cases where maps are embedded, e.g. need to
// create and dispatch an event so that the browser downloads
// the file instead of navigating to the url
const evt = new MouseEvent('click', {
view: window,
bubbles: false,
cancelable: true
});
link.dispatchEvent(evt);
document.body.removeChild(link);
URL.revokeObjectURL(url);
}
}
/**
* Whether color is rgb
* @returns
*/
export function exportImage(
uiStateExportImage: ExportImage,
filename = getApplicationConfig().defaultImageName
) {
const {imageDataUri} = uiStateExportImage;
if (imageDataUri) {
const file = dataURItoBlob(imageDataUri);
downloadFile(file, filename);
}
}
export function exportToJsonString(data) {
try {
return JSON.stringify(data);
} catch (e) {
if (e instanceof TypeError) return e.message;
// Non-Standard Error Object Property
return (e as any).description;
}
}
export function getMapJSON(state, options = getApplicationConfig().defaultExportJsonSettings) {
const {hasData} = options;
const schema = state.visState.schema;
if (!hasData) {
return schema.getConfigToSave(state);
}
let mapToSave = schema.save(state);
// add file name if title is not provided
const title = get(mapToSave, ['info', 'title']);
if (!title || !title.length) {
mapToSave = set(['info', 'title'], `keplergl_${generateHashId(6)}`, mapToSave);
}
return mapToSave;
}
export function exportJson(state, options: any = {}) {
const map = getMapJSON(state, options);
map.info.source = 'kepler.gl';
const fileBlob = new Blob([exportToJsonString(map)], {type: 'application/json'});
const fileName = state.appName ? `${state.appName}.json` : getApplicationConfig().defaultJsonName;
downloadFile(fileBlob, fileName);
}
export function exportHtml(state, options) {
const {userMapboxToken, exportMapboxAccessToken, mode} = options;
const data = {
...getMapJSON(state),
mapboxApiAccessToken:
(userMapboxToken || '') !== '' ? userMapboxToken : exportMapboxAccessToken,
mode
};
const fileBlob = new Blob([exportMapToHTML(data)], {type: 'text/html'});
downloadFile(
fileBlob,
state.appName ? `${state.appName}.html` : getApplicationConfig().defaultHtmlName
);
}
export function exportMap(state, options = getApplicationConfig().defaultExportJsonSettings) {
const {imageDataUri} = state.uiState.exportImage;
const thumbnail: Blob | null = imageDataUri ? dataURItoBlob(imageDataUri) : null;
const mapToSave = getMapJSON(state, options);
return {
map: mapToSave,
thumbnail
};
}
const exporters = {
exportImage,
exportJson,
exportHtml
};
export default exporters;