Skip to content

Commit

Permalink
Fix file bloat somewhat, and clean up preview handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Cocoa committed Feb 23, 2024
1 parent b1f43d1 commit 0fa22fa
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
15 changes: 7 additions & 8 deletions src/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 6 additions & 1 deletion src/utils/changeHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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]);
Expand Down
7 changes: 6 additions & 1 deletion src/utils/clickHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(() => {
Expand Down
14 changes: 7 additions & 7 deletions src/utils/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/utils/renderUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
}

0 comments on commit 0fa22fa

Please sign in to comment.