From 0fa22fa4a0a633ffe00c906de9f7911099dcb8e1 Mon Sep 17 00:00:00 2001 From: Cocoa Date: Thu, 22 Feb 2024 21:40:00 -0500 Subject: [PATCH] Fix file bloat somewhat, and clean up preview handling --- src/book.js | 15 +++++++-------- src/utils/changeHandlers.js | 7 ++++++- src/utils/clickHandlers.js | 7 ++++++- src/utils/pdf.js | 14 +++++++------- src/utils/renderUtils.js | 6 ++++++ 5 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/book.js b/src/book.js index d597049..15a7be3 100644 --- a/src/book.js +++ b/src/book.js @@ -297,7 +297,6 @@ export class Book { */ async createoutputfiles(isPreview) { const previewFrame = document.getElementById('pdf'); - previewFrame.style.display = 'none'; let previewPdf = null; // create a directory named after the input pdf and fill it with @@ -372,25 +371,25 @@ export class Book { for (const sig of signatures) { // Adding pages to aggregate PDFs has to be done in order, not with promises if (aggregate.front) { - const copiedPages = await aggregate.front.copyPages( + const copiedPages = await aggregate.front.embedPdf( sig.front, sig.front.getPageIndices() ); - copiedPages.forEach((page) => aggregate.front.addPage(page)); + copiedPages.forEach((page) => aggregate.front.addPage().drawPage(page)); } if (aggregate.back) { - const copiedPages = await aggregate.back.copyPages( + const copiedPages = await aggregate.back.embedPdf( sig.back, sig.back.getPageIndices() ); - copiedPages.forEach((page) => aggregate.back.addPage(page)); + copiedPages.forEach((page) => aggregate.back.addPage().drawPage(page)); } if (aggregate.duplex) { - const copiedPages = await aggregate.duplex.copyPages( + const copiedPages = await aggregate.duplex.embedPdf( sig.duplex, sig.duplex.getPageIndices() ); - copiedPages.forEach((page) => aggregate.duplex.addPage(page)); + copiedPages.forEach((page) => aggregate.duplex.addPage().drawPage(page)); } } if (aggregate.front) { @@ -443,11 +442,11 @@ export class Book { viewerPrefs.setCenterWindow(true); viewerPrefs.setDisplayDocTitle(true); + previewFrame.src = pdfDataUri; previewFrame.style.width = `450px`; const height = (this.papersize[1] / this.papersize[0]) * 500; previewFrame.style.height = `${height}px`; previewFrame.style.display = ''; - previewFrame.src = pdfDataUri; } if (!isPreview) return this.saveZip(); diff --git a/src/utils/changeHandlers.js b/src/utils/changeHandlers.js index 8c774a4..536ac14 100644 --- a/src/utils/changeHandlers.js +++ b/src/utils/changeHandlers.js @@ -3,7 +3,11 @@ // file, You can obtain one at https://mozilla.org/MPL/2.0/. import { saveForm, updateRenderedForm } from './formUtils'; -import { updatePaperSelectOptionsUnits, updateAddOrRemoveCustomPaperOption } from './renderUtils'; +import { + updatePaperSelectOptionsUnits, + updateAddOrRemoveCustomPaperOption, + clearPreview, +} from './renderUtils'; export function handleInputChange(book, bookbinderForm) { const formData = new FormData(bookbinderForm); @@ -17,6 +21,7 @@ export function handleInputChange(book, bookbinderForm) { } export function handleFileChange(e, book) { + clearPreview(); const fileList = e.target.files; if (fileList.length > 0) { const updated = book.openpdf(fileList[0]); diff --git a/src/utils/clickHandlers.js b/src/utils/clickHandlers.js index 9964798..6c6e3a0 100644 --- a/src/utils/clickHandlers.js +++ b/src/utils/clickHandlers.js @@ -3,7 +3,11 @@ // file, You can obtain one at https://mozilla.org/MPL/2.0/. import { resetForm } from './formUtils'; -import { updateAddOrRemoveCustomPaperOption, updatePaperSelectOptionsUnits } from './renderUtils'; +import { + clearPreview, + updateAddOrRemoveCustomPaperOption, + updatePaperSelectOptionsUnits, +} from './renderUtils'; export function handleGenerateClick(generateEl, book) { generateEl.setAttribute('disabled', true); @@ -28,6 +32,7 @@ export function handleGenerateClick(generateEl, book) { export function handlePreviewClick(previewEl, book) { previewEl.setAttribute('disabled', true); previewEl.innerText = 'Generating Preview...'; + clearPreview(); const result = book.createoutputfiles(true); result .then(() => { diff --git a/src/utils/pdf.js b/src/utils/pdf.js index 7bc264f..f4c2eba 100644 --- a/src/utils/pdf.js +++ b/src/utils/pdf.js @@ -13,16 +13,16 @@ export async function interleavePages(pdfA, pdfB) { const pageCount = Math.max(pdfA.getPageCount(), pdfB.getPageCount()); const promises = []; - for (let i = 0; i < pageCount; i++) { - const pageAPromise = pdfA.getPage(i); - const pageBPromise = pdfB.getPage(i); + const pagesAPromise = mergedPdf.copyPages(pdfA, pdfA.getPageIndices()); + const pagesBPromise = mergedPdf.copyPages(pdfB, pdfB.getPageIndices()); - promises.push(pageAPromise, pageBPromise); + promises.push(pagesAPromise, pagesBPromise); - const [pageA, pageB] = await Promise.all([pageAPromise, pageBPromise]); + const [pagesA, pagesB] = await Promise.all([pagesAPromise, pagesBPromise]); - if (pageA) mergedPdf.addPage((await mergedPdf.copyPages(pdfA, [i]))[0]); - if (pageB) mergedPdf.addPage((await mergedPdf.copyPages(pdfB, [i]))[0]); + for (let i = 0; i < pageCount; i++) { + if (i < pagesA.length) mergedPdf.addPage(pagesA[i]); + if (i < pagesB.length) mergedPdf.addPage(pagesB[i]); } await Promise.all(promises); // Wait for all page retrieval promises to resolve diff --git a/src/utils/renderUtils.js b/src/utils/renderUtils.js index 54c2e6a..b627be1 100644 --- a/src/utils/renderUtils.js +++ b/src/utils/renderUtils.js @@ -251,3 +251,9 @@ export function renderFormFromSettings(configuration) { example.style.display = example.id === selectedValue ? 'block' : 'none'; }); } + +export function clearPreview() { + const previewFrame = document.getElementById('pdf'); + previewFrame.style.display = 'none'; + previewFrame.src = ''; +}