Skip to content

Commit

Permalink
fixup! Feat(web-react): FileUploader - Support for crop image #DS-954
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelklibani committed Oct 10, 2023
1 parent 13c1241 commit 8f3e01b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const FileUploaderWithModalImagePreview = (args) => {
};

const confirmPreview = () => {
const meta = { x: 10, y: 10, width: 50, height: 50 };
const meta = { x: 10, y: 10, width: 80, height: 80 };

setIsModalOpen(false);
addToQueue(fileToPreview?.name || '', fileToPreview as File, meta);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,12 @@ type AttachmentImagePreviewProps = {
};

const AttachmentImagePreview = ({ label, imagePreview, meta }: AttachmentImagePreviewProps) => {
const { classProps } = useFileUploaderStyleProps();

// TODO: Add CSS styles for crop
const { x, y, width, height } = meta || {};
console.log('x:', x, 'y:', y, 'width:', width, 'height:', height);
const { classProps } = useFileUploaderStyleProps({ meta });
const { imageCropStyles } = classProps;

return (
<span className={classProps.attachment.image}>
<img
src={imagePreview}
width={IMAGE_DIMENSION}
height={IMAGE_DIMENSION}
alt={label}
style={{
'--file-uploader-attachment-image-top': `-${y}px`,
'--file-uploader-attachment-image-left': `-${x}px`,
'--file-uploader-attachment-image-width': `${width}px`,
'--file-uploader-attachment-image-height': `${height}px`,
}}
/>
<img src={imagePreview} width={IMAGE_DIMENSION} height={IMAGE_DIMENSION} alt={label} style={imageCropStyles} />
</span>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CSSProperties } from 'react';
import classNames from 'classnames';
import { useClassNamePrefix } from '../../hooks';
import { FileUploaderQueueLimitBehaviorType, Validation } from '../../types';
import { FileQueueValueMetaType, FileUploaderQueueLimitBehaviorType, Validation } from '../../types';

export interface FileUploaderStyleProps extends Validation {
isDragAndDropSupported?: boolean;
Expand All @@ -11,6 +12,14 @@ export interface FileUploaderStyleProps extends Validation {
isDropZoneHidden?: boolean;
isFluid?: boolean;
queueLimitBehavior?: FileUploaderQueueLimitBehaviorType;
meta?: FileQueueValueMetaType;
}

interface ImageCropCSS extends CSSProperties {
'--file-uploader-attachment-image-top'?: string;
'--file-uploader-attachment-image-left'?: string;
'--file-uploader-attachment-image-width'?: string;
'--file-uploader-attachment-image-height'?: string;
}

export interface FileUploaderStyleReturn {
Expand Down Expand Up @@ -38,6 +47,7 @@ export interface FileUploaderStyleReturn {
image: string;
slot: string;
};
imageCropStyles: ImageCropCSS | undefined;
};
}

Expand Down Expand Up @@ -68,6 +78,15 @@ export const useFileUploaderStyleProps = (props?: FileUploaderStyleProps): FileU
const fileUploaderAttachmentImageClass = `${fileUploaderAttachmentClass}__image`;
const fileUploaderAttachmentSlotClass = `${fileUploaderAttachmentClass}__slot`;

const { x, y, width, height } = props?.meta || {};
const hasCoords = x && y && width && height;
const imageCropCSS: ImageCropCSS = {
'--file-uploader-attachment-image-top': `-${props?.meta?.y}px`,
'--file-uploader-attachment-image-left': `-${props?.meta?.x}px`,
'--file-uploader-attachment-image-width': `${props?.meta?.width}px`,
'--file-uploader-attachment-image-height': `${props?.meta?.height}px`,
};

return {
classProps: {
root: classNames(fileUploaderClass, { [fileUploaderFluidClass]: props?.isFluid }),
Expand Down Expand Up @@ -102,6 +121,7 @@ export const useFileUploaderStyleProps = (props?: FileUploaderStyleProps): FileU
image: fileUploaderAttachmentImageClass,
slot: fileUploaderAttachmentSlotClass,
},
imageCropStyles: hasCoords ? imageCropCSS : undefined,
},
};
};

0 comments on commit 8f3e01b

Please sign in to comment.