refactor: apply eslint
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
|
||||
import { PdfFile } from '../../wrappers/PdfFile';
|
||||
import { PdfFile } from "../../wrappers/PdfFile";
|
||||
import { PDFPageProxy } from "pdfjs-dist/types/src/display/api.js";
|
||||
import { Image, ImageKind } from 'image-js';
|
||||
import { Image, ImageKind } from "image-js";
|
||||
|
||||
import { getImagesOnPage, PDFJSImage } from "./getImagesOnPage.js";
|
||||
|
||||
@@ -45,8 +45,8 @@ async function areImagesBlank(page: PDFPageProxy, threshold: number): Promise<bo
|
||||
|
||||
// TODO: Fix this function
|
||||
async function isImageBlank(image: PDFJSImage, threshold: number): Promise<boolean> {
|
||||
var img = new Image(image.width, image.height, image.data, { kind: "RGB" as ImageKind }); // TODO: Maybe respect image.kind and convert accordingly, needs to be tested with a pdf with alpha-image
|
||||
var grey = img.grey();
|
||||
var mean = grey.getMean();
|
||||
const img = new Image(image.width, image.height, image.data, { kind: "RGB" as ImageKind }); // TODO: Maybe respect image.kind and convert accordingly, needs to be tested with a pdf with alpha-image
|
||||
const grey = img.grey();
|
||||
const mean = grey.getMean();
|
||||
return mean[0] <= threshold;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
|
||||
import jsQR from "jsqr";
|
||||
|
||||
import { PdfFile } from '../../wrappers/PdfFile.js';
|
||||
import { PdfFile } from "../../wrappers/PdfFile.js";
|
||||
import { getImagesOnPage, PDFJSImage } from "./getImagesOnPage.js";
|
||||
|
||||
export async function detectQRCodePages(file: PdfFile) {
|
||||
@@ -24,7 +24,7 @@ export async function detectQRCodePages(file: PdfFile) {
|
||||
}
|
||||
}
|
||||
if(pagesWithQR.length == 0) {
|
||||
console.warn("Could not find any QR Codes in the provided PDF.")
|
||||
console.warn("Could not find any QR Codes in the provided PDF.");
|
||||
}
|
||||
return pagesWithQR;
|
||||
}
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
|
||||
import { PDFPageProxy } from "pdfjs-dist/types/src/display/api.js";
|
||||
|
||||
import * as PDFJS from 'pdfjs-dist';
|
||||
import * as PDFJS from "pdfjs-dist";
|
||||
|
||||
export type PDFJSImage = {
|
||||
export interface PDFJSImage {
|
||||
width: number;
|
||||
height: number;
|
||||
interpolate?: any;
|
||||
kind: number; // TODO: Document what this is, maybe hasAlpha?
|
||||
data: Uint8ClampedArray;
|
||||
};
|
||||
}
|
||||
|
||||
export async function getImagesOnPage(page: PDFPageProxy): Promise<PDFJSImage[]> {
|
||||
const ops = await page.getOperatorList();
|
||||
const images: PDFJSImage[] = [];
|
||||
for (var j=0; j < ops.fnArray.length; j++) {
|
||||
for (let j=0; j < ops.fnArray.length; j++) {
|
||||
if (ops.fnArray[j] == PDFJS.OPS.paintImageXObject) {
|
||||
const image = page.objs.get(ops.argsArray[j][0]) as PDFJSImage;
|
||||
images.push(image);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
|
||||
import { PdfFile, RepresentationType } from '../../wrappers/PdfFile.js';
|
||||
import { PDFDocument } from 'pdf-lib';
|
||||
import { PdfFile, RepresentationType } from "../../wrappers/PdfFile.js";
|
||||
import { PDFDocument } from "pdf-lib";
|
||||
|
||||
export async function getPages(file: PdfFile, pageIndexes: number[]): Promise<PdfFile> {
|
||||
const pdfLibDocument = await file.pdfLibDocument;
|
||||
|
||||
@@ -55,11 +55,11 @@ function bookletSort(totalPages: number): number[] {
|
||||
function sideStitchBooklet(totalPages: number): number[] {
|
||||
const newPageOrder: number[] = [];
|
||||
for (let i = 0; i < (totalPages + 3) / 4; i++) {
|
||||
const begin = i * 4;
|
||||
newPageOrder.push(Math.min(begin + 3, totalPages - 1));
|
||||
newPageOrder.push(Math.min(begin, totalPages - 1));
|
||||
newPageOrder.push(Math.min(begin + 1, totalPages - 1));
|
||||
newPageOrder.push(Math.min(begin + 2, totalPages - 1));
|
||||
const begin = i * 4;
|
||||
newPageOrder.push(Math.min(begin + 3, totalPages - 1));
|
||||
newPageOrder.push(Math.min(begin, totalPages - 1));
|
||||
newPageOrder.push(Math.min(begin + 1, totalPages - 1));
|
||||
newPageOrder.push(Math.min(begin + 2, totalPages - 1));
|
||||
}
|
||||
return newPageOrder;
|
||||
}
|
||||
@@ -72,10 +72,10 @@ function sideStitchBooklet(totalPages: number): number[] {
|
||||
function oddEvenSplit(totalPages: number): number[] {
|
||||
const newPageOrder: number[] = [];
|
||||
for (let i = 1; i <= totalPages; i += 2) {
|
||||
newPageOrder.push(i - 1);
|
||||
newPageOrder.push(i - 1);
|
||||
}
|
||||
for (let i = 2; i <= totalPages; i += 2) {
|
||||
newPageOrder.push(i - 1);
|
||||
newPageOrder.push(i - 1);
|
||||
}
|
||||
return newPageOrder;
|
||||
}
|
||||
@@ -108,9 +108,7 @@ function removeFirstAndLast(totalPages: number): number[] {
|
||||
}
|
||||
|
||||
export type SortFunction = (totalPages: number) => number[];
|
||||
type Sorts = {
|
||||
[key: string]: SortFunction;
|
||||
};
|
||||
type Sorts = Record<string, SortFunction>;
|
||||
export const Sorts: Sorts = Object.freeze({
|
||||
"REVERSE_ORDER": reverseSort,
|
||||
"DUPLEX_SORT": duplexSort,
|
||||
|
||||
@@ -18,7 +18,7 @@ export function invertSelection(selection: number[], pages: number|number[]): nu
|
||||
*/
|
||||
export function parsePageIndexSpecification(specification: string, totalPages: number): number[] {
|
||||
// Translated to JS from the original Java function
|
||||
const pageOrderArr = specification.split(",")
|
||||
const pageOrderArr = specification.split(",");
|
||||
const newPageOrder: number[] = [];
|
||||
|
||||
// loop through the page order array
|
||||
@@ -32,13 +32,13 @@ export function parsePageIndexSpecification(specification: string, totalPages: n
|
||||
}
|
||||
else if (element.match("\\d*n\\+?-?\\d*|\\d*\\+?n")) {
|
||||
// Handle page order as a function
|
||||
var coefficient = 0;
|
||||
var constant = 0;
|
||||
var coefficientExists = false;
|
||||
var constantExists = false;
|
||||
let coefficient = 0;
|
||||
let constant = 0;
|
||||
let coefficientExists = false;
|
||||
let constantExists = false;
|
||||
|
||||
if (element.includes("n")) {
|
||||
var parts = element.split("n");
|
||||
const parts = element.split("n");
|
||||
if (!parts[0]) {
|
||||
coefficient = parseInt(parts[0]);
|
||||
coefficientExists = true;
|
||||
@@ -53,7 +53,7 @@ export function parsePageIndexSpecification(specification: string, totalPages: n
|
||||
}
|
||||
|
||||
for (var i = 1; i <= totalPages; i++) {
|
||||
var pageNum = coefficientExists ? coefficient * i : i;
|
||||
let pageNum = coefficientExists ? coefficient * i : i;
|
||||
pageNum += constantExists ? constant : 0;
|
||||
|
||||
if (pageNum <= totalPages && pageNum > 0) {
|
||||
@@ -64,13 +64,13 @@ export function parsePageIndexSpecification(specification: string, totalPages: n
|
||||
// split the range into start and end page
|
||||
const range = element.split("-");
|
||||
const start = parseInt(range[0]);
|
||||
var end = parseInt(range[1]);
|
||||
let end = parseInt(range[1]);
|
||||
// check if the end page is greater than total pages
|
||||
if (end > totalPages) {
|
||||
end = totalPages;
|
||||
}
|
||||
// loop through the range of pages
|
||||
for (var j = start; j <= end; j++) {
|
||||
for (let j = start; j <= end; j++) {
|
||||
// print the current index
|
||||
newPageOrder.push(j - 1);
|
||||
}
|
||||
|
||||
@@ -1,46 +1,46 @@
|
||||
|
||||
import { PdfFile } from '../../wrappers/PdfFile';
|
||||
import { PdfFile } from "../../wrappers/PdfFile";
|
||||
|
||||
export async function sortPdfArray(
|
||||
files: PdfFile[],
|
||||
sortType: "orderProvided"|"byFileName"|"byDateModified"|"byDateCreated"|"byPDFTitle" = "orderProvided"
|
||||
): Promise<PdfFile[]> {
|
||||
files: PdfFile[],
|
||||
sortType: "orderProvided"|"byFileName"|"byDateModified"|"byDateCreated"|"byPDFTitle" = "orderProvided"
|
||||
): Promise<PdfFile[]> {
|
||||
|
||||
const docCache = await PdfFile.cacheAsPdfLibDocuments(files);
|
||||
|
||||
switch(sortType) {
|
||||
case "byFileName":
|
||||
files.sort((a, b) => {
|
||||
if (!a || !b) return 0;
|
||||
const ad = a.filename, bd = b.filename;
|
||||
if (!ad || !bd) return 0;
|
||||
return ad.localeCompare(bd);
|
||||
});
|
||||
break;
|
||||
case "byDateModified":
|
||||
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":
|
||||
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":
|
||||
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;
|
||||
case "byFileName":
|
||||
files.sort((a, b) => {
|
||||
if (!a || !b) return 0;
|
||||
const ad = a.filename, bd = b.filename;
|
||||
if (!ad || !bd) return 0;
|
||||
return ad.localeCompare(bd);
|
||||
});
|
||||
break;
|
||||
case "byDateModified":
|
||||
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":
|
||||
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":
|
||||
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 files;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
|
||||
import { PdfFile } from '../../wrappers/PdfFile.js';
|
||||
import { PdfFile } from "../../wrappers/PdfFile.js";
|
||||
import { getPages } from "./getPagesByIndex";
|
||||
|
||||
export async function splitPagesByIndex(file: PdfFile, splitAfterPageIndexes: number[]): Promise<PdfFile[]> {
|
||||
@@ -22,4 +22,4 @@ export async function splitPagesByIndex(file: PdfFile, splitAfterPageIndexes: nu
|
||||
pagesArray = [];
|
||||
|
||||
return subDocuments;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user