rotatePages

This commit is contained in:
Felix Kaspar 2024-05-17 19:40:12 +02:00
parent 2420e59cd8
commit 6f4bb8242b
3 changed files with 105 additions and 38 deletions

View File

@ -67,18 +67,35 @@ export function GenericField({ fieldName, joiDefinition }: GenericFieldProps) {
const item: Joi.Description = joiDefinition.items[0]; const item: Joi.Description = joiDefinition.items[0];
if(item.type == "number") { if(item.type == "number") {
if(item.rules.length == 1) { const props: any = {};
return (
<Fragment> item.rules.forEach((rule: { args: any, name: string}) => {
<label htmlFor={fieldName}>{flags.label}:</label>
<input type="text" pattern="(\d+)(,\s*\d+)*" list={fieldName} name={fieldName}/> switch (rule.name) {
<br/> case "integer":
</Fragment> if(props.pattern) {
); return (<div>props.pattern was already set, this is not implemented.</div>);
} }
else { props.pattern = `(\\d+)(,\\s*\\d+)*`;
return (<div>comma_array, item rules are empty or bigger than one, this is not implemented.</div>); break;
} case "min":
// TODO: Could validate this in frontend first.
break;
case "max":
// TODO: Could validate this in frontend first.
break;
default:
return (<div>comma_array, item rule {rule.name} is not implemented.</div>);
}
});
return (
<Fragment>
<label htmlFor={fieldName}>{flags.label}:</label>
<input type="text" pattern={props.pattern} list={fieldName} name={fieldName}/>
<br/>
</Fragment>
);
} }
else { else {
return (<div>comma_array, other types than numbers are not implemented yet.</div>); return (<div>comma_array, other types than numbers are not implemented yet.</div>);
@ -89,6 +106,14 @@ export function GenericField({ fieldName, joiDefinition }: GenericFieldProps) {
return (<div>comma_array, joi items are empty or bigger than one, this is not implemented</div>); return (<div>comma_array, joi items are empty or bigger than one, this is not implemented</div>);
} }
break; break;
case "alternatives":
return (
<Fragment>
<label htmlFor={fieldName}>{flags.label}:</label>
<input type="text" list={fieldName} name={fieldName}/>
<br/>
</Fragment>
);
default: default:
console.log(joiDefinition); console.log(joiDefinition);
return (<div>GenericField.tsx: <br/> "{fieldName}": requested type "{joiDefinition.type}" not found. Check console for further info.</div>) return (<div>GenericField.tsx: <br/> "{fieldName}": requested type "{joiDefinition.type}" not found. Check console for further info.</div>)

View File

@ -84,7 +84,7 @@ function Dynamic() {
action.values = validationResults.value.values; action.values = validationResults.value.values;
const operation = new activeOperator.current(action); const operation = new activeOperator.current(action);
operation.run(validationResults.value.input, (progress) => { operation.run(validationResults.value.input, (progress) => {
console.log("Progress: " + progress.operationProgress); console.log("OperationProgress: " + progress.operationProgress, "CurFileProgress: " + progress.curFileProgress);
}).then(async pdfFiles => { }).then(async pdfFiles => {
console.log("Done"); console.log("Done");
console.log(pdfFiles); console.log(pdfFiles);

View File

@ -1,33 +1,75 @@
import { Operator, Progress, oneToOne } from ".";
import Joi from "@stirling-tools/joi";
import { JoiPDFFileSchema } from "../wrappers/PdfFileJoi";
import i18next from "i18next";
import CommaArrayJoiExt from "../wrappers/CommaArrayJoiExt";
import { degrees } from "pdf-lib"; import { degrees } from "pdf-lib";
import { PdfFile, RepresentationType } from "../wrappers/PdfFile"; import { PdfFile, RepresentationType } from "../wrappers/PdfFile";
export interface RotateParamsType { export class RotatePages extends Operator {
file: PdfFile; static type = "rotatePages";
rotation: number|number[];
}
export async function rotatePages(params: RotateParamsType): Promise<PdfFile> { /**
const { file, rotation } = params; * Validation & Localisation
*/
const pdfDoc = await file.pdfLibDocument;
const pages = pdfDoc.getPages();
if (Array.isArray(rotation)) { protected static inputSchema = JoiPDFFileSchema.label(i18next.t("inputs.pdffile.name")).description(i18next.t("inputs.pdffile.description"));
if (rotation.length != pages.length) { protected static valueSchema = Joi.object({
throw new Error(`Number of given rotations '${rotation.length}' is not the same as the number of pages '${pages.length}'`); rotation: Joi.alternatives().try(
} Joi.number().min(0).max(360).allow(null),
for (let i=0; i<rotation.length; i++) { CommaArrayJoiExt.comma_array().items(Joi.number().integer().min(0).max(360))
const oldRotation = pages[i].getRotation().angle; ).label(i18next.t("values.rotation.friendlyName", { ns: "rotatePages" })).description(i18next.t("values.rotation.description", { ns: "rotatePages" }))
pages[i].setRotation(degrees(oldRotation + rotation[i])); .example("90").example("-180").example("[90, 0, 270]"),
} });
} else { protected static outputSchema = JoiPDFFileSchema.label(i18next.t("outputs.pdffile.name")).description(i18next.t("outputs.pdffile.description"));
pages.forEach(page => {
// Change page size static schema = Joi.object({
const oldRotation = page.getRotation().angle; input: RotatePages.inputSchema,
page.setRotation(degrees(oldRotation + rotation)); values: RotatePages.valueSchema.required(),
output: RotatePages.outputSchema
}).label(i18next.t("friendlyName", { ns: "rotatePages" })).description(i18next.t("description", { ns: "rotatePages" }));
/**
* Logic
*/
/** Detect and remove white pages */
async run(input: PdfFile[], progressCallback: (state: Progress) => void): Promise<PdfFile[]> {
return oneToOne<PdfFile, PdfFile>(input, async (input, index, max) => {
const pdfDoc = await input.pdfLibDocument;
const pages = pdfDoc.getPages();
// Different rotations applied to each page
if (Array.isArray(this.actionValues.rotation)) {
if (this.actionValues.rotation.length != pages.length) {
throw new Error(`Number of given rotations '${this.actionValues.rotation.length}' is not the same as the number of pages '${pages.length}'`);
}
for (let pageIdx = 0; pageIdx < this.actionValues.rotation.length; pageIdx++) {
const oldRotation = pages[pageIdx].getRotation().angle;
pages[pageIdx].setRotation(degrees(oldRotation + this.actionValues.rotation[pageIdx]));
progressCallback({ curFileProgress: pageIdx/pages.length, operationProgress: index/max });
}
}
// Only one rotation applied to each page
else {
pages.forEach((page, pageIdx) => {
// Change page size
const oldRotation = page.getRotation().angle;
page.setRotation(degrees(oldRotation + this.actionValues.rotation));
progressCallback({ curFileProgress: pageIdx/pages.length, operationProgress: index/max });
});
}
progressCallback({ curFileProgress: 1, operationProgress: index/max });
return new PdfFile(input.originalFilename, pdfDoc, RepresentationType.PDFLibDocument, input.filename + "_rotated");
}); });
} }
}
return new PdfFile(file.originalFilename, pdfDoc, RepresentationType.PDFLibDocument, file.filename+"_rotated");
}