Removed new state pdfDocs

- update reverse-pages function
- updated test-case
This commit is contained in:
divy-11
2025-10-20 16:39:45 +05:30
parent a605e56834
commit 4e6cc77a28
4 changed files with 35 additions and 32 deletions

View File

@@ -25,9 +25,9 @@ async function handleSinglePdfUpload(toolId, file) {
showLoader('Loading PDF...');
try {
const pdfBytes = await readFileAsArrayBuffer(file);
const pdfDoc = await PDFLibDocument.load(pdfBytes as ArrayBuffer, { ignoreEncryption: true });
state.pdfDocs = [pdfDoc];
state.pdfDoc = pdfDoc;
state.pdfDoc = await PDFLibDocument.load(pdfBytes as ArrayBuffer, {
ignoreEncryption: true
});
hideLoader();
if (
@@ -324,7 +324,6 @@ async function handleSinglePdfUpload(toolId, file) {
}
async function handleMultiFileUpload(toolId) {
console.log(toolId);
if (toolId === 'merge' || toolId === 'alternate-merge' || toolId === 'reverse-pages') {
const pdfFilesUnloaded: File[] = [];
@@ -347,7 +346,7 @@ async function handleMultiFileUpload(toolId) {
};
})
);
state.pdfDocs = pdfFilesLoaded.map(p => p.pdfDoc);
const foundEncryptedPDFs = pdfFilesLoaded.filter(
(pdf) => pdf.pdfDoc.isEncrypted
);

View File

@@ -6,7 +6,7 @@ import { PDFDocument as PDFLibDocument } from 'pdf-lib';
import JSZip from 'jszip';
export async function reversePages() {
const pdfDocs = Array.isArray(state.pdfDocs) ? state.pdfDocs : state.pdfDoc ? [state.pdfDoc] : [];
const pdfDocs = state.files.filter((file: File) => file.type === 'application/pdf');
if (!pdfDocs.length) {
showAlert('Error', 'PDF not loaded.');
return;
@@ -15,7 +15,9 @@ export async function reversePages() {
try {
const zip = new JSZip();
for (let j = 0; j < pdfDocs.length; j++) {
const pdfDoc = pdfDocs[j];
const file = pdfDocs[j];
const arrayBuffer = await file.arrayBuffer();
const pdfDoc = await PDFLibDocument.load(arrayBuffer);
const newPdf = await PDFLibDocument.create();
const pageCount = pdfDoc.getPageCount();
const reversedIndices = Array.from(
@@ -27,7 +29,8 @@ export async function reversePages() {
copiedPages.forEach((page: any) => newPdf.addPage(page));
const newPdfBytes = await newPdf.save();
const fileName = pdfDocs.length > 1 ? `reversed_${j + 1}.pdf` : 'reversed.pdf';
const originalName = file.name.replace(/\.pdf$/i, '');
const fileName = `${originalName}_reversed.pdf`;
zip.file(fileName, newPdfBytes);
}
const zipBlob = await zip.generateAsync({ type: 'blob' });

View File

@@ -1,7 +1,6 @@
export const state = {
activeTool: null,
files: [],
pdfDocs: [],
pdfDoc: null,
pdfPages: [],
currentPdfUrl: null,
@@ -11,7 +10,6 @@ export const state = {
export function resetState() {
state.activeTool = null;
state.files = [];
state.pdfDocs = [];
state.pdfDoc = null;
state.pdfPages = [];
state.currentPdfUrl = null;