mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-09-23 03:56:20 +00:00
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.')
|
||
|
)
|
||
|
});
|
||
|
};
|