fix(editor): PDF export download keeps its URL alive (WebKit may cancel)
This commit is contained in:
1 parent
ae17097afb
commit
2b8000e5ab
3 files changed
+32
-15
No files matched your search
@@ -33,6 +33,7 @@ import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import { pickLabel } from '@/i18n/pickLocaleLabel'
|
||||
import type { ViewStates } from '@/anatomy/drawing'
|
||||
import { exportReportToPdf } from '@/pdf/export'
|
||||
import { downloadBlob } from '@/lib/download'
|
||||
import type { ExportWarning } from '@faunosteo/pdf'
|
||||
import type { RebaseOutcome } from '@faunosteo/core-wasm/types'
|
||||
import { useT } from '@/i18n/useT'
|
||||
@@ -236,13 +237,10 @@ export default function EditorRoute() {
|
||||
const { pdf, warnings } = await exportReportToPdf(report, index, {
|
||||
locale,
|
||||
})
|
||||
const blob = new Blob([pdf as BlobPart], { type: 'application/pdf' })
|
||||
const url = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = `bilan-${report.animal.name}-${report.date}.pdf`
|
||||
a.click()
|
||||
URL.revokeObjectURL(url)
|
||||
downloadBlob(
|
||||
new Blob([pdf as BlobPart], { type: 'application/pdf' }),
|
||||
`bilan-${report.animal.name}-${report.date}.pdf`,
|
||||
)
|
||||
const missing = missingItemsOf(warnings)
|
||||
if (missing.length) {
|
||||
window.alert(
|
||||
|
||||
@@ -3,18 +3,28 @@ import { todayIso } from '@/lib/dates'
|
||||
const REVOKE_DELAY_MS = 30_000
|
||||
|
||||
/**
|
||||
* Saves `raw` as `<stem>-<date>.json`. A lone surrogate in a JSON.stringify output is a
|
||||
* `\uXXXX` escape, so the UTF-8 Blob is lossless. This copy may be the last one before a
|
||||
* deletion: the anchor is attached while clicked and the URL revoked later, since some
|
||||
* engines (WebKit) cancel a download whose URL is revoked at once.
|
||||
* Saves `blob` under `fileName`. The anchor is attached while clicked and the URL revoked
|
||||
* later, since some engines (WebKit) cancel a download whose URL is revoked at once.
|
||||
*/
|
||||
export function downloadJson(raw: string, stem: string): void {
|
||||
const url = URL.createObjectURL(new Blob([raw], { type: 'application/json' }))
|
||||
export function downloadBlob(blob: Blob, fileName: string): void {
|
||||
const url = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = `${stem}-${todayIso()}.json`
|
||||
a.download = fileName
|
||||
document.body.appendChild(a)
|
||||
a.click()
|
||||
a.remove()
|
||||
setTimeout(() => URL.revokeObjectURL(url), REVOKE_DELAY_MS)
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves `raw` as `<stem>-<date>.json`. A lone surrogate in a JSON.stringify output is a
|
||||
* `\uXXXX` escape, so the UTF-8 Blob is lossless. This copy may be the last one before a
|
||||
* deletion.
|
||||
*/
|
||||
export function downloadJson(raw: string, stem: string): void {
|
||||
downloadBlob(
|
||||
new Blob([raw], { type: 'application/json' }),
|
||||
`${stem}-${todayIso()}.json`,
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user