From 33c4c1c5d2bd19abda159c4b507b93ee1745f15a Mon Sep 17 00:00:00 2001 From: Jeroen Hermans Date: Sun, 15 Nov 2020 22:08:13 +0100 Subject: [PATCH] Batch print DOM updates --- js/controllers/print.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/js/controllers/print.js b/js/controllers/print.js index c895654f..2ceda1ce 100644 --- a/js/controllers/print.js +++ b/js/controllers/print.js @@ -55,6 +55,9 @@ export default class Print { slideScrollHeights.push( slide.scrollHeight ); }); + const pages = []; + const pageContainer = slides[0].parentNode; + // Slide and slide background layout slides.forEach( function( slide, index ) { @@ -79,9 +82,10 @@ export default class Print { // Wrap the slide in a page element and hide its overflow // so that no page ever flows onto another const page = document.createElement( 'div' ); + pages.push( page ); + page.className = 'pdf-page'; page.style.height = ( ( pageHeight + config.pdfPageHeightOffset ) * numberOfPages ) + 'px'; - slide.parentNode.insertBefore( page, slide ); page.appendChild( slide ); // Position the slide inside of the page @@ -109,7 +113,7 @@ export default class Print { notesElement.innerHTML = notes; if( notesLayout === 'separate-page' ) { - page.parentNode.insertBefore( notesElement, page.nextSibling ); + pages.push( notesElement ); } else { notesElement.style.left = notesSpacing + 'px'; @@ -141,7 +145,6 @@ export default class Print { const fragmentGroups = this.Reveal.fragments.sort( page.querySelectorAll( '.fragment' ), true ); let previousFragmentStep; - let previousPage; fragmentGroups.forEach( function( fragments ) { @@ -159,10 +162,9 @@ export default class Print { // Create a separate page for the current fragment state let clonedPage = page.cloneNode( true ); - page.parentNode.insertBefore( clonedPage, ( previousPage || page ).nextSibling ); + pages.push( clonedPage ); previousFragmentStep = fragments; - previousPage = clonedPage; }, this ); @@ -185,6 +187,11 @@ export default class Print { }, this ); + await new Promise(requestAnimationFrame); + pages.forEach( function( page ) { + pageContainer.appendChild( page ); + }) + // Notify subscribers that the PDF layout is good to go this.Reveal.dispatchEvent({ type: 'pdf-ready' }); @@ -199,4 +206,4 @@ export default class Print { } -} \ No newline at end of file +}