From 96bac91fa1b4aa901c6e5c049fa26b28acd664d4 Mon Sep 17 00:00:00 2001 From: Felix Kaspar Date: Tue, 17 Oct 2023 01:38:51 +0200 Subject: [PATCH] modularization --- public/index.js | 138 +---------------------------------- public/traverseOperations.js | 125 +++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 136 deletions(-) create mode 100644 public/traverseOperations.js diff --git a/public/index.js b/public/index.js index 8190ca76..8811c40d 100644 --- a/public/index.js +++ b/public/index.js @@ -1,7 +1,7 @@ import { scaleContent } from "./functions/scaleContent.js"; import { scalePage, PageSize } from "./functions/scalePage.js"; -import { organizeWaitOperations } from "./organizeWaitOperations.js"; import { testWorkflow } from "./testWorkflow.js"; +import { traverseOperations } from "./traverseOperations.js"; (async (workflow) => { const pdfFileInput = document.getElementById('pdfFile'); @@ -19,130 +19,7 @@ import { testWorkflow } from "./testWorkflow.js"; })); console.log(pdfBuffers); - const waitOperations = organizeWaitOperations(workflow.operations); - - nextOperation(workflow.operations, pdfBuffers); - - async function nextOperation(operations, input) { - if(Array.isArray(operations) && operations.length == 0) { // isEmpty - console.log("operation done: " + input.fileName); - //TODO: Download Restult - return; - } - - for (let i = 0; i < operations.length; i++) { - console.warn(input); - await computeOperation(operations[i], structuredClone(input)); // break references - } - } - - async function computeOperation(operation, input) { - switch (operation.type) { - case "done": - console.log("Done operation will get called if all waits are done. Skipping for now.") - break; - case "wait": - const waitOperation = waitOperations[operation.values.id]; - waitOperation.input.push(input); - waitOperation.waitCount--; - if(waitOperation.waitCount == 0) { - await nextOperation(waitOperation.doneOperation.operations, waitOperation.input); - } - break; - case "removeObjects": - if(Array.isArray(input)) { - for (let i = 0; i < input.length; i++) { - // TODO: modfiy input - input[i].fileName += "_removedObjects"; - await nextOperation(operation.operations, input[i]); - } - } - else { - // TODO: modfiy input - input.fileName += "_removedObjects"; - await nextOperation(operation.operations, input); - } - break; - case "extract": - if(Array.isArray(input)) { - for (let i = 0; i < input.length; i++) { - // TODO: modfiy input - input[i].fileName += "_extractedPages"; - await nextOperation(operation.operations, input[i]); - } - } - else { - // TODO: modfiy input - input.fileName += "_extractedPages"; - await nextOperation(operation.operations, input); - } - break; - case "fillField": - if(Array.isArray(input)) { - for (let i = 0; i < input.length; i++) { - // TODO: modfiy input - input[i].fileName += "_filledField"; - await nextOperation(operation.operations, input[i]); - } - } - else { - // TODO: modfiy input - input.fileName += "_filledField"; - await nextOperation(operation.operations, input); - } - break; - case "extractImages": - if(Array.isArray(input)) { - for (let i = 0; i < input.length; i++) { - // TODO: modfiy input - input[i].fileName += "_extractedImages"; - await nextOperation(operation.operations, input[i]); - } - } - else { - // TODO: modfiy input - input.fileName += "_extractedImages"; - await nextOperation(operation.operations, input); - } - break; - case "merge": - if(Array.isArray(input)) { - input = { - originalFileName: input.map(input => input.originalFileName).join("_and_"), - fileName: input.map(input => input.fileName).join("_and_") + "_merged", - buffer: input[0].buffer // TODO: merge inputs - } - } - else { - // TODO: modfiy input - input.fileName += "_merged"; - } - await nextOperation(operation.operations, input); - break; - case "transform": { - if(Array.isArray(input)) { - for (let i = 0; i < input.length; i++) { - // TODO: modfiy input - input[i].fileName += "_transformed"; - await nextOperation(operation.operations, input[i]); - } - } - else { - // TODO: modfiy input - input.fileName += "_transformed"; - await nextOperation(operation.operations, input); - } - break; - } - default: - console.log("operation type unknown: ", operation.type); - break; - } - } - - - - + await traverseOperations(workflow.operations, pdfBuffers); // if(selectedElementsList[0].textContent == "mergePDFs") { @@ -178,15 +55,4 @@ import { testWorkflow } from "./testWorkflow.js"; // } // } }); - - // document.getElementById("addButton").addEventListener("click", function() { - // const selectedOption = document.getElementById("pdfOptions").value; - // const operations = document.getElementById("operations"); - - // if (selectedOption) { - // const listItem = document.createElement("li"); - // listItem.textContent = selectedOption; - // operations.appendChild(listItem); - // } - // }); })(testWorkflow); diff --git a/public/traverseOperations.js b/public/traverseOperations.js new file mode 100644 index 00000000..50ad7ad6 --- /dev/null +++ b/public/traverseOperations.js @@ -0,0 +1,125 @@ +import { organizeWaitOperations } from "./organizeWaitOperations.js"; + +export async function traverseOperations(operations, input) { + const waitOperations = organizeWaitOperations(operations); + await nextOperation(operations, input); + + async function nextOperation(operations, input) { + if(Array.isArray(operations) && operations.length == 0) { // isEmpty + console.log("operation done: " + input.fileName); + + //TODO: Delay the download + download(input, input.fileName, "application/pdf"); + return; + } + + for (let i = 0; i < operations.length; i++) { + console.warn(input); + await computeOperation(operations[i], structuredClone(input)); // break references + } + } + + async function computeOperation(operation, input) { + switch (operation.type) { + case "done": + console.log("Done operation will get called if all waits are done. Skipping for now.") + break; + case "wait": + const waitOperation = waitOperations[operation.values.id]; + waitOperation.input.push(input); + waitOperation.waitCount--; + if(waitOperation.waitCount == 0) { + await nextOperation(waitOperation.doneOperation.operations, waitOperation.input); + } + break; + case "removeObjects": + if(Array.isArray(input)) { + for (let i = 0; i < input.length; i++) { + // TODO: modfiy input + input[i].fileName += "_removedObjects"; + await nextOperation(operation.operations, input[i]); + } + } + else { + // TODO: modfiy input + input.fileName += "_removedObjects"; + await nextOperation(operation.operations, input); + } + break; + case "extract": + if(Array.isArray(input)) { + for (let i = 0; i < input.length; i++) { + // TODO: modfiy input + input[i].fileName += "_extractedPages"; + await nextOperation(operation.operations, input[i]); + } + } + else { + // TODO: modfiy input + input.fileName += "_extractedPages"; + await nextOperation(operation.operations, input); + } + break; + case "fillField": + if(Array.isArray(input)) { + for (let i = 0; i < input.length; i++) { + // TODO: modfiy input + input[i].fileName += "_filledField"; + await nextOperation(operation.operations, input[i]); + } + } + else { + // TODO: modfiy input + input.fileName += "_filledField"; + await nextOperation(operation.operations, input); + } + break; + case "extractImages": + if(Array.isArray(input)) { + for (let i = 0; i < input.length; i++) { + // TODO: modfiy input + input[i].fileName += "_extractedImages"; + await nextOperation(operation.operations, input[i]); + } + } + else { + // TODO: modfiy input + input.fileName += "_extractedImages"; + await nextOperation(operation.operations, input); + } + break; + case "merge": + if(Array.isArray(input)) { + input = { + originalFileName: input.map(input => input.originalFileName).join("_and_"), + fileName: input.map(input => input.fileName).join("_and_") + "_merged", + buffer: input[0].buffer // TODO: merge inputs + } + } + else { + // TODO: modfiy input + input.fileName += "_merged"; + } + await nextOperation(operation.operations, input); + break; + case "transform": { + if(Array.isArray(input)) { + for (let i = 0; i < input.length; i++) { + // TODO: modfiy input + input[i].fileName += "_transformed"; + await nextOperation(operation.operations, input[i]); + } + } + else { + // TODO: modfiy input + input.fileName += "_transformed"; + await nextOperation(operation.operations, input); + } + break; + } + default: + console.log("operation type unknown: ", operation.type); + break; + } + } +} \ No newline at end of file