mirror of
https://github.com/olehomelchenko/astrolabe.git
synced 2026-08-08 02:02:33 +00:00
Per-chart export: copy/download spec and PNG/SVG from the preview header
This commit is contained in:
@@ -7,10 +7,13 @@
|
||||
* than Blobs and anchors.
|
||||
*/
|
||||
|
||||
/** Trigger a client-side download of `json` text as `filename`. */
|
||||
export function downloadJson(filename: string, json: string): void {
|
||||
const blob = new Blob([json], { type: 'application/json' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
/**
|
||||
* Trigger a download of an already-built object/data `url` as `filename`. A
|
||||
* `blob:` URL is revoked right after the click; a `data:` URL needs no cleanup.
|
||||
* Used by the per-chart export for an image URL produced by the Vega view
|
||||
* (`RenderHandle.toImageURL`).
|
||||
*/
|
||||
export function downloadUrl(filename: string, url: string): void {
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
@@ -18,10 +21,22 @@ export function downloadJson(filename: string, json: string): void {
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
URL.revokeObjectURL(url);
|
||||
if (url.startsWith('blob:')) URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
/** Trigger a client-side download of `json` text as `filename`. */
|
||||
export function downloadJson(filename: string, json: string): void {
|
||||
const url = URL.createObjectURL(new Blob([json], { type: 'application/json' }));
|
||||
downloadUrl(filename, url);
|
||||
}
|
||||
|
||||
/** Read a picked file's text content (rejects on an unreadable file). */
|
||||
export function readTextFile(file: File): Promise<string> {
|
||||
return file.text();
|
||||
}
|
||||
|
||||
/** Copy `text` to the clipboard (rejects when the browser blocks access). The
|
||||
* one place outside a component that touches the clipboard API. */
|
||||
export function copyText(text: string): Promise<void> {
|
||||
return navigator.clipboard.writeText(text);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user