Skip to content

Commit 424909e

Browse files
committed
Update print button event handler
1 parent de7d46f commit 424909e

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

app/app/src/components/RenderDocument.astro

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -612,16 +612,33 @@ document.querySelectorAll('[data-content-control="split"]').forEach((element) =>
612612
})
613613
})
614614

615-
window.addEventListener('beforeprint', () =>
616-
{
617-
splitDocument()
618-
})
619-
620615
document.querySelectorAll('[data-content-control="print"]').forEach((element) =>
621616
{
622-
element.addEventListener('click', () =>
617+
element.addEventListener('click', async (event) =>
623618
{
619+
const target = event.currentTarget as HTMLElement
620+
// Disable the print button to avoid multiple clicks
621+
target.setAttribute('disabled', 'true')
622+
// Split document in pages before printing
623+
splitDocument()
624+
// Wait for all images in all content wrappers to load
625+
const images: HTMLImageElement[] = Array.from(document.querySelectorAll('.content-wrapper img'))
626+
await Promise.all(images.map((img) =>
627+
{
628+
if (img.complete)
629+
{
630+
return Promise.resolve()
631+
}
632+
return new Promise<void>((resolve) =>
633+
{
634+
img.addEventListener('load', () => resolve())
635+
img.addEventListener('error', () => resolve())
636+
})
637+
}))
638+
// Print the document
624639
window.print()
640+
// Re-enable the print button
641+
target.removeAttribute('disabled')
625642
})
626643
})
627644

0 commit comments

Comments
 (0)