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
@@ -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