feat(docs): export data dictionary as PDF
This commit is contained in:
1 parent
2d4ee24660
commit
b140f84ac4
28 files changed
+3108
-3
No files matched your search
@@ -0,0 +1,37 @@
|
||||
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
|
||||
|
||||
export class DictionaryFileExistsError extends Error {
|
||||
constructor() {
|
||||
super("exists");
|
||||
this.name = "DictionaryFileExistsError";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Suggested name is only the save dialog's defaultPath. The write uses the path
|
||||
* `save()` just returned. Tauri's dialog grant covers that exact path, not a
|
||||
* typed path or a timestamp appended after the dialog closes.
|
||||
*/
|
||||
export async function saveDataDictionaryFile(suggestedName: string, content: Uint8Array, options: { overwrite: boolean }): Promise<string | null> {
|
||||
if (isTauriRuntime()) {
|
||||
const [{ save }, fs] = await Promise.all([import("@tauri-apps/plugin-dialog"), import("@tauri-apps/plugin-fs")]);
|
||||
const granted = await save({
|
||||
defaultPath: suggestedName,
|
||||
filters: [{ name: "PDF", extensions: ["pdf"] }],
|
||||
});
|
||||
if (!granted) return null;
|
||||
if (!options.overwrite && (await fs.exists(granted))) throw new DictionaryFileExistsError();
|
||||
await fs.writeFile(granted, content);
|
||||
return granted;
|
||||
}
|
||||
|
||||
const downloadName = suggestedName.split(/[/\\]/).pop() || suggestedName;
|
||||
const blob = new Blob([content.buffer.slice(content.byteOffset, content.byteOffset + content.byteLength) as ArrayBuffer], { type: "application/pdf" });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const anchor = document.createElement("a");
|
||||
anchor.href = url;
|
||||
anchor.download = downloadName;
|
||||
anchor.click();
|
||||
URL.revokeObjectURL(url);
|
||||
return downloadName;
|
||||
}
|
||||
Reference in new issue
Block a user