Per-chart export: copy/download spec and PNG/SVG from the preview header

This commit is contained in:
2026-06-10 18:30:31 +03:00
parent cad19445b0
commit 1791ee9f8d
15 changed files with 921 additions and 33 deletions
+20 -5
View File
@@ -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);
}