File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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-
620615document.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
You can’t perform that action at this time.
0 commit comments