Fixed up straggling errors
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
|
||||
import { PdfFile, convertAllToPdfLibFile } from '../../wrappers/PdfFile';
|
||||
import { PdfFile } from '../../wrappers/PdfFile';
|
||||
|
||||
export async function sortPdfs(
|
||||
files: PdfFile[],
|
||||
sortType: "orderProvided"|"byFileName"|"byDateModified"|"byDateCreated"|"byPDFTitle" = "orderProvided"
|
||||
): Promise<PdfFile[]> {
|
||||
|
||||
const pdfLibFiles = await convertAllToPdfLibFile(files);
|
||||
|
||||
const docCache = await PdfFile.cacheAsPdfLibDocuments(files);
|
||||
|
||||
switch(sortType) {
|
||||
case "byFileName":
|
||||
pdfLibFiles.sort((a, b) => {
|
||||
files.sort((a, b) => {
|
||||
if (!a || !b) return 0;
|
||||
const ad = a.filename, bd = b.filename;
|
||||
if (!ad || !bd) return 0;
|
||||
@@ -18,30 +18,30 @@ export async function sortPdfs(
|
||||
});
|
||||
break;
|
||||
case "byDateModified":
|
||||
pdfLibFiles.sort((a, b) => {
|
||||
const ad = a.pdfLib?.getModificationDate()?.getTime();
|
||||
const bd = b.pdfLib?.getModificationDate()?.getTime();
|
||||
files.sort((a, b) => {
|
||||
const ad = docCache.get(a)?.getModificationDate()?.getTime();
|
||||
const bd = docCache.get(b)?.getModificationDate()?.getTime();
|
||||
if (!ad || !bd) return 0;
|
||||
return ad > bd ? 1 : -1
|
||||
});
|
||||
break;
|
||||
case "byDateCreated":
|
||||
pdfLibFiles.sort((a, b) => {
|
||||
const ad = a.pdfLib?.getCreationDate()?.getTime();
|
||||
const bd = b.pdfLib?.getCreationDate()?.getTime();
|
||||
files.sort((a, b) => {
|
||||
const ad = docCache.get(a)?.getCreationDate()?.getTime();
|
||||
const bd = docCache.get(b)?.getCreationDate()?.getTime();
|
||||
if (!ad || !bd) return 0;
|
||||
return ad > bd ? 1 : -1
|
||||
});
|
||||
break;
|
||||
case "byPDFTitle":
|
||||
pdfLibFiles.sort((a, b) => {
|
||||
const ad = a.pdfLib?.getTitle();
|
||||
const bd = b.pdfLib?.getTitle();
|
||||
files.sort((a, b) => {
|
||||
const ad = docCache.get(a)?.getTitle();
|
||||
const bd = docCache.get(b)?.getTitle();
|
||||
if (!ad || !bd) return 0;
|
||||
return ad.localeCompare(bd);
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
return pdfLibFiles;
|
||||
return files;
|
||||
}
|
||||
@@ -10,10 +10,9 @@ export type SplitPdfParamsType = {
|
||||
export async function splitPDF(params: SplitPdfParamsType): Promise<PdfFile[]> {
|
||||
const { file, splitAfterPageArray } = params;
|
||||
|
||||
const byteFile = await file.convertToPdfLibFile();
|
||||
if (!byteFile?.pdfLib) return [];
|
||||
const pdflibDocument = await file.pdflibDocument;
|
||||
|
||||
const numberOfPages = byteFile.pdfLib.getPages().length;
|
||||
const numberOfPages = pdflibDocument.getPages().length;
|
||||
|
||||
let pagesArray: number[] = [];
|
||||
let splitAfter = splitAfterPageArray.shift();
|
||||
@@ -21,13 +20,13 @@ export async function splitPDF(params: SplitPdfParamsType): Promise<PdfFile[]> {
|
||||
|
||||
for (let i = 0; i < numberOfPages; i++) {
|
||||
if(splitAfter && i > splitAfter && pagesArray.length > 0) {
|
||||
subDocuments.push(await selectPages({file:byteFile, pagesToExtractArray:pagesArray}));
|
||||
subDocuments.push(await selectPages({file, pagesToExtractArray:pagesArray}));
|
||||
splitAfter = splitAfterPageArray.shift();
|
||||
pagesArray = [];
|
||||
}
|
||||
pagesArray.push(i);
|
||||
}
|
||||
subDocuments.push(await selectPages({file:byteFile, pagesToExtractArray:pagesArray}));
|
||||
subDocuments.push(await selectPages({file, pagesToExtractArray:pagesArray}));
|
||||
pagesArray = [];
|
||||
|
||||
return subDocuments;
|
||||
|
||||
Reference in New Issue
Block a user