mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-09-22 19:46:39 +00:00

# Description of Changes Add Crop to V2 --------- Co-authored-by: EthanHealy01 <80844253+EthanHealy01@users.noreply.github.com> Co-authored-by: Connor Yoh <connor@stirlingpdf.com> Co-authored-by: ConnorYoh <40631091+ConnorYoh@users.noreply.github.com>
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import { useTranslation } from 'react-i18next';
|
|
import { useToolOperation, ToolType } from '../shared/useToolOperation';
|
|
import { createStandardErrorHandler } from '../../../utils/toolErrorHandler';
|
|
import { CropParameters, defaultParameters } from './useCropParameters';
|
|
|
|
// Static configuration that can be used by both the hook and automation executor
|
|
export const buildCropFormData = (parameters: CropParameters, file: File): FormData => {
|
|
const formData = new FormData();
|
|
formData.append("fileInput", file);
|
|
const cropArea = parameters.cropArea;
|
|
|
|
// Backend expects precise float values for PDF coordinates
|
|
formData.append("x", cropArea.x.toString());
|
|
formData.append("y", cropArea.y.toString());
|
|
formData.append("width", cropArea.width.toString());
|
|
formData.append("height", cropArea.height.toString());
|
|
|
|
return formData;
|
|
};
|
|
|
|
// Static configuration object
|
|
export const cropOperationConfig = {
|
|
toolType: ToolType.singleFile,
|
|
buildFormData: buildCropFormData,
|
|
operationType: 'crop',
|
|
endpoint: '/api/v1/general/crop',
|
|
defaultParameters,
|
|
} as const;
|
|
|
|
export const useCropOperation = () => {
|
|
const { t } = useTranslation();
|
|
|
|
return useToolOperation<CropParameters>({
|
|
...cropOperationConfig,
|
|
getErrorMessage: createStandardErrorHandler(
|
|
t('crop.error.failed', 'An error occurred while cropping the PDF.')
|
|
)
|
|
});
|
|
};
|