2023-11-14 03:26:42 +03:00
|
|
|
import { organizeWaitOperations } from "./organizeWaitOperations";
|
2023-11-14 17:34:50 +01:00
|
|
|
import { Action, WaitAction } from "../../declarations/Action";
|
2023-11-14 03:26:42 +03:00
|
|
|
import { OperationsType } from "../../src/index";
|
|
|
|
import { PdfFile } from "../wrappers/PdfFile";
|
2023-10-17 01:38:51 +02:00
|
|
|
|
2023-11-14 17:29:19 +01:00
|
|
|
export async function * traverseOperations(operations: Action[], input: PdfFile[] | PdfFile, Operations: OperationsType): AsyncGenerator<string, PdfFile[], void> {
|
2023-10-17 01:38:51 +02:00
|
|
|
const waitOperations = organizeWaitOperations(operations);
|
2023-11-14 03:26:42 +03:00
|
|
|
let results: PdfFile[] = [];
|
2023-10-22 01:11:17 +02:00
|
|
|
yield* nextOperation(operations, input);
|
2023-10-17 02:14:06 +02:00
|
|
|
return results;
|
2023-10-17 01:38:51 +02:00
|
|
|
|
2023-11-14 17:29:19 +01:00
|
|
|
async function * nextOperation(actions: Action[], input: PdfFile[] | PdfFile): AsyncGenerator<string, void, void> {
|
2023-11-14 03:26:42 +03:00
|
|
|
if(Array.isArray(actions) && actions.length == 0) { // isEmpty
|
2023-10-22 01:11:17 +02:00
|
|
|
if(Array.isArray(input)) {
|
2023-11-14 03:26:42 +03:00
|
|
|
console.log("operation done: " + input[0].filename + (input.length > 1 ? "+" : ""));
|
2023-10-22 01:11:17 +02:00
|
|
|
results = results.concat(input);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
2023-11-14 03:26:42 +03:00
|
|
|
console.log("operation done: " + input.filename);
|
2023-10-22 01:11:17 +02:00
|
|
|
results.push(input);
|
|
|
|
return;
|
|
|
|
}
|
2023-10-17 01:38:51 +02:00
|
|
|
}
|
|
|
|
|
2023-11-14 03:26:42 +03:00
|
|
|
for (let i = 0; i < actions.length; i++) {
|
|
|
|
yield* computeOperation(actions[i], structuredClone(input));
|
2023-10-17 01:38:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-14 17:29:19 +01:00
|
|
|
async function * computeOperation(action: Action, input: PdfFile|PdfFile[]): AsyncGenerator<string, void, void> {
|
2023-11-14 03:26:42 +03:00
|
|
|
yield "Starting: " + action.type;
|
|
|
|
switch (action.type) {
|
2023-10-22 01:11:17 +02:00
|
|
|
case "done": // Skip this, because it is a valid node.
|
2023-10-17 01:38:51 +02:00
|
|
|
break;
|
|
|
|
case "wait":
|
2023-11-14 17:34:50 +01:00
|
|
|
const waitOperation = waitOperations[(action as WaitAction).values.id];
|
2023-10-17 03:40:54 +02:00
|
|
|
|
2023-10-17 01:38:51 +02:00
|
|
|
if(Array.isArray(input)) {
|
2023-10-22 01:11:17 +02:00
|
|
|
waitOperation.input.concat(input); // TODO: May have unexpected concequences. Needs further testing!
|
2023-10-17 01:38:51 +02:00
|
|
|
}
|
|
|
|
else {
|
2023-10-22 01:11:17 +02:00
|
|
|
waitOperation.input.push(input);
|
2023-10-17 01:38:51 +02:00
|
|
|
}
|
2023-10-17 03:40:54 +02:00
|
|
|
|
2023-10-22 01:11:17 +02:00
|
|
|
waitOperation.waitCount--;
|
2023-11-14 03:26:42 +03:00
|
|
|
if(waitOperation.waitCount == 0 && waitOperation.doneOperation.actions) {
|
|
|
|
yield* nextOperation(waitOperation.doneOperation.actions, waitOperation.input);
|
2023-10-17 03:40:54 +02:00
|
|
|
}
|
|
|
|
break;
|
2023-10-22 01:11:17 +02:00
|
|
|
case "extract":
|
2023-11-14 03:26:42 +03:00
|
|
|
yield* nToN(input, action, async (input) => {
|
|
|
|
const newPdf = await Operations.selectPages({file: input, pagesToExtractArray: action.values["pagesToExtractArray"]});
|
|
|
|
newPdf.filename += "_extractedPages";
|
|
|
|
return newPdf;
|
2023-10-22 01:11:17 +02:00
|
|
|
});
|
2023-10-17 01:38:51 +02:00
|
|
|
break;
|
2023-10-22 01:11:17 +02:00
|
|
|
case "impose":
|
2023-11-14 03:26:42 +03:00
|
|
|
yield* nToN(input, action, async (input) => {
|
|
|
|
const newPdf = await Operations.impose({file: input, nup: action.values["nup"], format: action.values["format"]});
|
|
|
|
newPdf.filename += "_imposed";
|
|
|
|
return newPdf;
|
2023-10-22 01:11:17 +02:00
|
|
|
});
|
2023-10-17 01:38:51 +02:00
|
|
|
break;
|
|
|
|
case "merge":
|
2023-11-14 03:26:42 +03:00
|
|
|
yield* nToOne(input, action, async (inputs) => {
|
|
|
|
const newPdf = await Operations.mergePDFs({files: inputs});
|
|
|
|
newPdf.filename = inputs.map(input => input.filename).join("_and_") + "_merged";
|
|
|
|
return newPdf;
|
2023-10-22 01:11:17 +02:00
|
|
|
});
|
2023-10-17 03:40:54 +02:00
|
|
|
break;
|
|
|
|
case "rotate":
|
2023-11-14 03:26:42 +03:00
|
|
|
yield* nToN(input, action, async (input) => {
|
|
|
|
const newPdf = await Operations.rotatePages({file: input, rotation: action.values["rotation"]});
|
|
|
|
newPdf.filename += "_turned";
|
|
|
|
return newPdf;
|
2023-10-22 01:11:17 +02:00
|
|
|
});
|
2023-10-17 03:40:54 +02:00
|
|
|
break;
|
2023-10-22 01:11:17 +02:00
|
|
|
case "split":
|
|
|
|
// TODO: A split might break the done condition, it may count multiple times. Needs further testing!
|
2023-11-14 03:26:42 +03:00
|
|
|
yield* oneToN(input, action, async (input) => {
|
|
|
|
const splitResult = await Operations.splitPDF({file: input, splitAfterPageArray: action.values["splitAfterPageArray"]});
|
2023-10-22 01:11:17 +02:00
|
|
|
for (let j = 0; j < splitResult.length; j++) {
|
2023-11-14 03:26:42 +03:00
|
|
|
splitResult[j].filename = splitResult[j].filename + "_split" + j;
|
2023-10-18 01:20:31 +02:00
|
|
|
}
|
2023-11-14 03:26:42 +03:00
|
|
|
return splitResult;
|
2023-10-22 01:11:17 +02:00
|
|
|
});
|
2023-10-18 01:20:31 +02:00
|
|
|
break;
|
2023-11-11 18:35:33 +03:00
|
|
|
case "updateMetadata":
|
2023-11-14 03:26:42 +03:00
|
|
|
yield* nToN(input, action, async (input) => {
|
|
|
|
const newPdf = await Operations.updateMetadata({file: input, ...action.values["metadata"]});
|
|
|
|
newPdf.filename += "_metadataEdited";
|
|
|
|
return newPdf;
|
2023-10-22 18:30:45 +02:00
|
|
|
});
|
|
|
|
break;
|
2023-11-14 03:26:42 +03:00
|
|
|
case "sortPagesWithPreset":
|
|
|
|
yield* nToN(input, action, async (input) => {
|
|
|
|
const newPdf = await Operations.sortPagesWithPreset({file: input, sortPreset: action.values["sortPreset"], fancyPageSelector: action.values["fancyPageSelector"]});
|
|
|
|
newPdf.filename += "_pagesOrganized";
|
|
|
|
return newPdf;
|
2023-10-23 14:11:49 +02:00
|
|
|
});
|
|
|
|
break;
|
2023-10-24 16:09:10 +02:00
|
|
|
case "removeBlankPages":
|
2023-11-14 03:26:42 +03:00
|
|
|
yield* nToN(input, action, async (input) => {
|
|
|
|
const newPdf = await Operations.removeBlankPages({file: input, whiteThreashold: action.values["whiteThreashold"]});
|
|
|
|
newPdf.filename += "_removedBlanks";
|
|
|
|
return newPdf;
|
2023-10-24 16:09:10 +02:00
|
|
|
});
|
|
|
|
break;
|
2023-10-27 02:56:13 +02:00
|
|
|
case "splitOn":
|
2023-11-14 03:26:42 +03:00
|
|
|
yield* oneToN(input, action, async (input) => {
|
|
|
|
const splitResult = await Operations.splitOn({file: input, type: action.values["type"], whiteThreashold: action.values["whiteThreashold"]});
|
2023-10-28 19:30:12 +02:00
|
|
|
for (let j = 0; j < splitResult.length; j++) {
|
2023-11-14 03:26:42 +03:00
|
|
|
splitResult[j].filename = splitResult[j].filename + "_split" + j;
|
2023-10-28 19:30:12 +02:00
|
|
|
}
|
2023-11-14 03:26:42 +03:00
|
|
|
return splitResult;
|
2023-10-27 02:56:13 +02:00
|
|
|
});
|
|
|
|
break;
|
2023-10-17 01:38:51 +02:00
|
|
|
default:
|
2023-11-14 03:26:42 +03:00
|
|
|
throw new Error(`${action.type} not implemented yet.`);
|
2023-10-17 01:38:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-10-22 01:11:17 +02:00
|
|
|
|
2023-11-07 00:11:14 +01:00
|
|
|
/**
|
|
|
|
*
|
2023-11-14 03:26:42 +03:00
|
|
|
* @param {PdfFile|PdfFile[]} input
|
|
|
|
* @param {JSON} action
|
2023-11-07 00:11:14 +01:00
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
2023-11-14 17:29:19 +01:00
|
|
|
async function * nToOne(inputs: PdfFile|PdfFile[], action: Action, callback: (pdf: PdfFile[]) => Promise<PdfFile>): AsyncGenerator<string, void, void> {
|
2023-11-14 03:26:42 +03:00
|
|
|
const input = Array.isArray(inputs) ? inputs : [inputs]; // Convert single values to array, keep arrays as is.
|
2023-10-22 01:11:17 +02:00
|
|
|
|
2023-11-14 03:26:42 +03:00
|
|
|
const newInputs = await callback(input);
|
|
|
|
if (action.actions) {
|
|
|
|
yield* nextOperation(action.actions, newInputs);
|
|
|
|
}
|
2023-10-22 01:11:17 +02:00
|
|
|
}
|
|
|
|
|
2023-11-07 00:11:14 +01:00
|
|
|
/**
|
|
|
|
*
|
2023-11-14 03:26:42 +03:00
|
|
|
* @param {PdfFile|PdfFile[]} input
|
|
|
|
* @param {JSON} action
|
2023-11-07 00:11:14 +01:00
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
2023-11-14 17:29:19 +01:00
|
|
|
async function * oneToN(input: PdfFile|PdfFile[], action: Action, callback: (pdf: PdfFile) => Promise<PdfFile[]>): AsyncGenerator<string, void, void> {
|
2023-10-22 01:11:17 +02:00
|
|
|
if(Array.isArray(input)) {
|
2023-11-14 03:26:42 +03:00
|
|
|
let output: PdfFile[] = [];
|
2023-10-22 01:11:17 +02:00
|
|
|
for (let i = 0; i < input.length; i++) {
|
2023-10-28 19:30:12 +02:00
|
|
|
output = output.concat(await callback(input[i]));
|
2023-10-22 01:11:17 +02:00
|
|
|
}
|
2023-11-14 03:26:42 +03:00
|
|
|
if (action.actions) {
|
|
|
|
yield* nextOperation(action.actions, output);
|
|
|
|
}
|
2023-10-22 01:11:17 +02:00
|
|
|
}
|
|
|
|
else {
|
2023-11-14 03:26:42 +03:00
|
|
|
const nextInput = await callback(input);
|
|
|
|
if (action.actions) {
|
|
|
|
yield* nextOperation(action.actions, nextInput);
|
|
|
|
}
|
2023-10-22 01:11:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-14 17:29:19 +01:00
|
|
|
async function * nToN(input: PdfFile|PdfFile[], action: Action, callback: (pdf: PdfFile) => Promise<PdfFile>): AsyncGenerator<string, void, void> {
|
2023-10-22 01:11:17 +02:00
|
|
|
if(Array.isArray(input)) {
|
2023-11-14 03:26:42 +03:00
|
|
|
const nextInputs: PdfFile[] = []
|
2023-10-22 01:11:17 +02:00
|
|
|
for (let i = 0; i < input.length; i++) {
|
2023-11-14 03:26:42 +03:00
|
|
|
nextInputs.concat(await callback(input[i]));
|
|
|
|
}
|
|
|
|
if (action.actions) {
|
|
|
|
yield* nextOperation(action.actions, nextInputs);
|
2023-10-22 01:11:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2023-11-14 03:26:42 +03:00
|
|
|
const nextInput = await callback(input);
|
|
|
|
if (action.actions) {
|
|
|
|
yield* nextOperation(action.actions, nextInput);
|
|
|
|
}
|
2023-10-22 01:11:17 +02:00
|
|
|
}
|
|
|
|
}
|
2023-10-17 01:38:51 +02:00
|
|
|
}
|