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 { pickLabel } from '@/i18n/pickLocaleLabel'
|
||||||
import type { ViewStates } from '@/anatomy/drawing'
|
import type { ViewStates } from '@/anatomy/drawing'
|
||||||
import { exportReportToPdf } from '@/pdf/export'
|
import { exportReportToPdf } from '@/pdf/export'
|
||||||
|
import { downloadBlob } from '@/lib/download'
|
||||||
import type { ExportWarning } from '@faunosteo/pdf'
|
import type { ExportWarning } from '@faunosteo/pdf'
|
||||||
import type { RebaseOutcome } from '@faunosteo/core-wasm/types'
|
import type { RebaseOutcome } from '@faunosteo/core-wasm/types'
|
||||||
import { useT } from '@/i18n/useT'
|
import { useT } from '@/i18n/useT'
|
||||||
@@ -236,13 +237,10 @@ export default function EditorRoute() {
|
|||||||
const { pdf, warnings } = await exportReportToPdf(report, index, {
|
const { pdf, warnings } = await exportReportToPdf(report, index, {
|
||||||
locale,
|
locale,
|
||||||
})
|
})
|
||||||
const blob = new Blob([pdf as BlobPart], { type: 'application/pdf' })
|
downloadBlob(
|
||||||
const url = URL.createObjectURL(blob)
|
new Blob([pdf as BlobPart], { type: 'application/pdf' }),
|
||||||
const a = document.createElement('a')
|
`bilan-${report.animal.name}-${report.date}.pdf`,
|
||||||
a.href = url
|
)
|
||||||
a.download = `bilan-${report.animal.name}-${report.date}.pdf`
|
|
||||||
a.click()
|
|
||||||
URL.revokeObjectURL(url)
|
|
||||||
const missing = missingItemsOf(warnings)
|
const missing = missingItemsOf(warnings)
|
||||||
if (missing.length) {
|
if (missing.length) {
|
||||||
window.alert(
|
window.alert(
|
||||||
|
|||||||
@@ -3,18 +3,28 @@ import { todayIso } from '@/lib/dates'
|
|||||||
const REVOKE_DELAY_MS = 30_000
|
const REVOKE_DELAY_MS = 30_000
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves `raw` as `<stem>-<date>.json`. A lone surrogate in a JSON.stringify output is a
|
* Saves `blob` under `fileName`. The anchor is attached while clicked and the URL revoked
|
||||||
* `\uXXXX` escape, so the UTF-8 Blob is lossless. This copy may be the last one before a
|
* later, since some engines (WebKit) cancel a download whose URL is revoked at once.
|
||||||
* 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.
|
|
||||||
*/
|
*/
|
||||||
export function downloadJson(raw: string, stem: string): void {
|
export function downloadBlob(blob: Blob, fileName: string): void {
|
||||||
const url = URL.createObjectURL(new Blob([raw], { type: 'application/json' }))
|
const url = URL.createObjectURL(blob)
|
||||||
const a = document.createElement('a')
|
const a = document.createElement('a')
|
||||||
a.href = url
|
a.href = url
|
||||||
a.download = `${stem}-${todayIso()}.json`
|
a.download = fileName
|
||||||
document.body.appendChild(a)
|
document.body.appendChild(a)
|
||||||
a.click()
|
a.click()
|
||||||
a.remove()
|
a.remove()
|
||||||
setTimeout(() => URL.revokeObjectURL(url), REVOKE_DELAY_MS)
|
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`,
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -31,6 +31,7 @@ const PDF_BYTES = new Uint8Array([37, 80, 68, 70])
|
|||||||
|
|
||||||
let sheet: SheetIndex
|
let sheet: SheetIndex
|
||||||
const downloads: string[] = []
|
const downloads: string[] = []
|
||||||
|
const clicked: { connectedAtClick: boolean }[] = []
|
||||||
const realCreateObjectURL = URL.createObjectURL
|
const realCreateObjectURL = URL.createObjectURL
|
||||||
const realRevokeObjectURL = URL.revokeObjectURL
|
const realRevokeObjectURL = URL.revokeObjectURL
|
||||||
|
|
||||||
@@ -48,17 +49,20 @@ beforeEach(() => {
|
|||||||
.hydrateImport({ report: minimalReport(sheet), archive: null }, sheet)
|
.hydrateImport({ report: minimalReport(sheet), archive: null }, sheet)
|
||||||
exportMock.mockReset()
|
exportMock.mockReset()
|
||||||
downloads.length = 0
|
downloads.length = 0
|
||||||
|
clicked.length = 0
|
||||||
URL.createObjectURL = vi.fn(() => 'blob:faunosteo-test')
|
URL.createObjectURL = vi.fn(() => 'blob:faunosteo-test')
|
||||||
URL.revokeObjectURL = vi.fn()
|
URL.revokeObjectURL = vi.fn()
|
||||||
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(function (
|
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(function (
|
||||||
this: HTMLAnchorElement,
|
this: HTMLAnchorElement,
|
||||||
) {
|
) {
|
||||||
downloads.push(this.download)
|
downloads.push(this.download)
|
||||||
|
clicked.push({ connectedAtClick: this.isConnected })
|
||||||
})
|
})
|
||||||
vi.stubGlobal('alert', vi.fn())
|
vi.stubGlobal('alert', vi.fn())
|
||||||
})
|
})
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
cleanup()
|
cleanup()
|
||||||
|
vi.useRealTimers()
|
||||||
vi.restoreAllMocks()
|
vi.restoreAllMocks()
|
||||||
vi.unstubAllGlobals()
|
vi.unstubAllGlobals()
|
||||||
URL.createObjectURL = realCreateObjectURL
|
URL.createObjectURL = realCreateObjectURL
|
||||||
@@ -94,7 +98,8 @@ describe('EditorRoute export', () => {
|
|||||||
expect(exportMock.mock.calls[0]![2]).toEqual({ locale: 'en' })
|
expect(exportMock.mock.calls[0]![2]).toEqual({ locale: 'en' })
|
||||||
})
|
})
|
||||||
|
|
||||||
it('downloads bilan-<animal>-<date>.pdf through a blob URL it then revokes', async () => {
|
it('downloads bilan-<animal>-<date>.pdf through a blob URL it revokes after a delay', async () => {
|
||||||
|
vi.useFakeTimers({ shouldAdvanceTime: true })
|
||||||
exportMock.mockResolvedValue({ pdf: PDF_BYTES, warnings: [] })
|
exportMock.mockResolvedValue({ pdf: PDF_BYTES, warnings: [] })
|
||||||
await mountAndExport(fr['editor.export']!)
|
await mountAndExport(fr['editor.export']!)
|
||||||
await waitFor(() => expect(downloads).toHaveLength(1))
|
await waitFor(() => expect(downloads).toHaveLength(1))
|
||||||
@@ -102,6 +107,10 @@ describe('EditorRoute export', () => {
|
|||||||
expect(URL.createObjectURL).toHaveBeenCalledTimes(1)
|
expect(URL.createObjectURL).toHaveBeenCalledTimes(1)
|
||||||
const blob = vi.mocked(URL.createObjectURL).mock.calls[0]![0] as Blob
|
const blob = vi.mocked(URL.createObjectURL).mock.calls[0]![0] as Blob
|
||||||
expect(blob.type).toBe('application/pdf')
|
expect(blob.type).toBe('application/pdf')
|
||||||
|
expect(clicked[0]!.connectedAtClick).toBe(true)
|
||||||
|
expect(document.body.querySelector('a[download]')).toBeNull()
|
||||||
|
expect(URL.revokeObjectURL).not.toHaveBeenCalled()
|
||||||
|
vi.advanceTimersByTime(30_000)
|
||||||
expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:faunosteo-test')
|
expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:faunosteo-test')
|
||||||
expect(window.alert).not.toHaveBeenCalled()
|
expect(window.alert).not.toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in new issue
Block a user