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];
if(item.type == "number") {
if(item.rules.length == 1) {
return (
<Fragment>
<label htmlFor={fieldName}>{flags.label}:</label>
<input type="text" pattern="(\d+)(,\s*\d+)*" list={fieldName} name={fieldName}/>
<br/>
</Fragment>
);
}
else {
return (<div>comma_array, item rules are empty or bigger than one, this is not implemented.</div>);
}
const props: any = {};
item.rules.forEach((rule: { args: any, name: string}) => {
switch (rule.name) {
case "integer":
if(props.pattern) {
return (<div>props.pattern was already set, this is not implemented.</div>);
}
props.pattern = `(\\d+)(,\\s*\\d+)*`;
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 {
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>);
}
break;
case "alternatives":
return (
<Fragment>
<label htmlFor={fieldName}>{flags.label}:</label>
<input type="text" list={fieldName} name={fieldName}/>
<br/>
</Fragment>
);
default:
console.log(joiDefinition);
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;
const operation = new activeOperator.current(action);
operation.run(validationResults.value.input, (progress) => {
console.log("Progress: " + progress.operationProgress);
console.log("OperationProgress: " + progress.operationProgress, "CurFileProgress: " + progress.curFileProgress);
}).then(async pdfFiles => {
console.log("Done");
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 { PdfFile, RepresentationType } from "../wrappers/PdfFile";
export interface RotateParamsType {
file: PdfFile;
rotation: number|number[];
}
export class RotatePages extends Operator {
static type = "rotatePages";
export async function rotatePages(params: RotateParamsType): Promise<PdfFile> {
const { file, rotation } = params;
const pdfDoc = await file.pdfLibDocument;
const pages = pdfDoc.getPages();
/**
* Validation & Localisation
*/
if (Array.isArray(rotation)) {
if (rotation.length != pages.length) {
throw new Error(`Number of given rotations '${rotation.length}' is not the same as the number of pages '${pages.length}'`);
}
for (let i=0; i<rotation.length; i++) {
const oldRotation = pages[i].getRotation().angle;
pages[i].setRotation(degrees(oldRotation + rotation[i]));
}
} else {
pages.forEach(page => {
// Change page size
const oldRotation = page.getRotation().angle;
page.setRotation(degrees(oldRotation + rotation));
protected static inputSchema = JoiPDFFileSchema.label(i18next.t("inputs.pdffile.name")).description(i18next.t("inputs.pdffile.description"));
protected static valueSchema = Joi.object({
rotation: Joi.alternatives().try(
Joi.number().min(0).max(360).allow(null),
CommaArrayJoiExt.comma_array().items(Joi.number().integer().min(0).max(360))
).label(i18next.t("values.rotation.friendlyName", { ns: "rotatePages" })).description(i18next.t("values.rotation.description", { ns: "rotatePages" }))
.example("90").example("-180").example("[90, 0, 270]"),
});
protected static outputSchema = JoiPDFFileSchema.label(i18next.t("outputs.pdffile.name")).description(i18next.t("outputs.pdffile.description"));
static schema = Joi.object({
input: RotatePages.inputSchema,
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");
}
}