Skip to content

Commit 98c9132

Browse files
committed
[FEAT]: Add prop smartImgFit in ImagePreview FileMosaic File Card and Avatar components
1 parent cff6693 commit 98c9132

File tree

9 files changed

+157
-36
lines changed

9 files changed

+157
-36
lines changed

src/components/MainPage/SecondaryRight/ExtraComponentsMainPageAvatar.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,18 @@ const ExtraComponentsMainPageAvatar: React.FC<ExtraComponentsMainPageProps> = (
6363
src={avatarSrc}
6464
onChange={handleChange}
6565
isUloading={isUloading}
66-
smart={false}
66+
smartImgFit={"center"}
67+
style={{ width: "170px", height: "170px" }}
68+
//variant={"circle"}
69+
/>
70+
<Avatar
71+
src={avatarSrc}
72+
onChange={handleChange}
73+
isUloading={isUloading}
74+
smartImgFit={"center"}
6775
variant={"circle"}
76+
style={{ width: "170px", height: "170px" }}
6877
/>
69-
7078
<MaterialButton
7179
onClick={() => setAvatarSrc(undefined)}
7280
disabled={avatarSrc === undefined}

src/files-ui/components/avatar/Avatar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const Avatar: React.FC<AvatarProps> = (props: AvatarProps) => {
2121
uploadingLabel,
2222
isUloading,
2323
onError,
24-
smart,
24+
smartImgFit,
2525
} = mergeProps(props, defaultAvatarProps);
2626

2727
const inputRef: React.RefObject<HTMLInputElement> =
@@ -80,7 +80,7 @@ const Avatar: React.FC<AvatarProps> = (props: AvatarProps) => {
8080
src={src}
8181
alt={alt}
8282
onError={handleError}
83-
smart={smart}
83+
smartImgFit={smartImgFit}
8484
/>
8585
) : (
8686
<div className={"fui-avatar-label"}>{emptyLabel}</div>

src/files-ui/components/avatar/AvatarProps.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,32 @@ export interface AvatarFullProps extends OverridableComponentProps {
2929
* In that case height will be set to 100%. Otherwise width will be set to 100%
3030
*/
3131
smart?: boolean;
32+
/**
33+
* If not present, image width will be set to 100%.
34+
*
35+
* If present, image will be analized and displayed according to its heigh and width.
36+
* Image width height greater than its width has a "portrait" orientation.
37+
* Otherwise it has a "landscape" orientation.
38+
* - If value is "orientation", image will be displayed complete by giving 100%
39+
* to width prop if the orientation is "landscape".
40+
* When orientation is "portrait", height prop will be set to 100%. Some images
41+
* will show an empty space.
42+
* - If value is "center", image will be centered and will not be displayed complete.
43+
* This the empty space is avoided. This is achived by giving 100% to width prop if
44+
* the orientation is "portrait". When orientation is "landscape", height prop will be set to 100%.
45+
* @default orientation
46+
*/
47+
smartImgFit?: false | "orientation" | "center";
3248
}
3349

3450
export declare type AvatarProps =
35-
/* {
36-
[D in keyof React.HTMLProps<HTMLDivElement>]: React.HTMLProps<HTMLDivElement>[D]
37-
} & */
38-
{
39-
[P in keyof AvatarFullProps]: AvatarFullProps[P];
51+
/* {
52+
[D in keyof React.HTMLProps<HTMLDivElement>]: React.HTMLProps<HTMLDivElement>[D]
53+
} & */
54+
{
55+
[P in keyof AvatarFullProps]: AvatarFullProps[P];
4056

41-
}
57+
}
4258
//React.HTMLProps<HTMLDivElement>
4359
export const defaultAvatarProps: AvatarProps =
4460
{
@@ -49,4 +65,5 @@ export const defaultAvatarProps: AvatarProps =
4965
uploadingLabel: "Uploading...",
5066
readOnly: false,
5167
smart: false,
68+
smartImgFit: "orientation",
5269
}

src/files-ui/components/file-card/FileCard.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const FileCard: React.FC<FileCardProps> = (props: FileCardProps) => {
104104
onRightClick,
105105

106106
elevation = 4,
107-
107+
smartImgFit = "orientation",
108108
//} = mergeProps(props, FileCardPropsDefault);
109109
} = props;
110110

@@ -268,6 +268,7 @@ const FileCard: React.FC<FileCardProps> = (props: FileCardProps) => {
268268
fileName={localName}
269269
url={url}
270270
isBlur={true}
271+
smartImgFit={false}
271272
/>
272273
</Layer>
273274
{/** IMAGE LAYER NO BLUR */}
@@ -277,6 +278,7 @@ const FileCard: React.FC<FileCardProps> = (props: FileCardProps) => {
277278
url={url}
278279
fileName={localName}
279280
isBlur={false}
281+
smartImgFit={smartImgFit}
280282
/>
281283
</Layer>
282284
<Layer className="file-card-status-layer" visible={true}>
@@ -300,7 +302,7 @@ const FileCard: React.FC<FileCardProps> = (props: FileCardProps) => {
300302
</div>
301303
</div>
302304
</Layer>
303-
305+
304306
{/** INFO LAYER */}
305307
<Layer
306308
className="file-card-info-layer-container"
@@ -333,7 +335,6 @@ const FileCard: React.FC<FileCardProps> = (props: FileCardProps) => {
333335
/>
334336
</div>
335337
</Layer>
336-
337338
</LayerContainer>
338339
<FileCardRightActions
339340
deleteIcon={onDelete !== undefined}

src/files-ui/components/file-mosaic/components/FIleMosaicImageLayer/FileMosaicImageLayer.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,28 @@ interface FileMosaicImageLayerProps {
2222
*
2323
*/
2424
isBlur?: boolean;
25+
/**
26+
* If not present, image width will be set to 100%.
27+
*
28+
* If present, image will be analized and displayed according to its heigh and width.
29+
* Image width height greater than its width has a "portrait" orientation.
30+
* Otherwise it has a "landscape" orientation.
31+
* - If value is "orientation", image will be displayed complete by giving 100%
32+
* to width prop if the orientation is "landscape".
33+
* When orientation is "portrait", height prop will be set to 100%. Some images
34+
* will show an empty space.
35+
* - If value is "center", image will be centered and will not be displayed complete.
36+
* This the empty space is avoided. This is achived by giving 100% to width prop if
37+
* the orientation is "portrait". When orientation is "landscape", height prop will be set to 100%.
38+
* @default orientation
39+
*/
40+
smartImgFit?: false | "orientation" | "center";
2541
}
2642
const FileMosaicImageLayer: React.FC<FileMosaicImageLayerProps> = (
2743
props: FileMosaicImageLayerProps
2844
) => {
2945
//console.log("FileMosaicImageLayer", props);
30-
const { imageSource, url, fileName, card, isBlur } = props;
46+
const { imageSource, url, fileName, card, isBlur,smartImgFit } = props;
3147

3248
const [localSource, setLocalSource] = React.useState<string | undefined>(
3349
undefined
@@ -58,7 +74,7 @@ const FileMosaicImageLayer: React.FC<FileMosaicImageLayerProps> = (
5874
<ImagePreview
5975
src={localSource}
6076
alt={`blur ${fileName}`}
61-
smart={false}
77+
smartImgFit={false}
6278
/>
6379
)}
6480
</React.Fragment>
@@ -71,6 +87,7 @@ const FileMosaicImageLayer: React.FC<FileMosaicImageLayerProps> = (
7187
src={localSource}
7288
style={{ borderRadius: "0px" }}
7389
alt={`preview ${fileName}`}
90+
smartImgFit={smartImgFit}
7491
/>
7592
</React.Fragment>
7693
);

src/files-ui/components/file-mosaic/components/file-mosaic/FileMosaic.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const FileMosaic: React.FC<FileMosaicProps> = (props: FileMosaicProps) => {
6565
onDoubleClick,
6666
onClick,
6767
onRightClick,
68+
smartImgFit,
6869
} = props;
6970

7071
//ref for anchor download element
@@ -232,6 +233,7 @@ const FileMosaic: React.FC<FileMosaicProps> = (props: FileMosaicProps) => {
232233
url={url}
233234
fileName={localName}
234235
isBlur={true}
236+
smartImgFit={false}
235237
/>
236238
</Layer>
237239

@@ -242,6 +244,7 @@ const FileMosaic: React.FC<FileMosaicProps> = (props: FileMosaicProps) => {
242244
url={url}
243245
fileName={localName}
244246
isBlur={false}
247+
smartImgFit={smartImgFit}
245248
/>
246249
</Layer>
247250

src/files-ui/components/file-mosaic/components/file-mosaic/FileMosaicProps.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,22 @@ export interface FileMosaicPropsMap extends OverridableComponentProps {
145145
* In case onDownload prop is given
146146
*/
147147
downloadUrl?: string;
148+
/**
149+
* If not present, image width will be set to 100%.
150+
*
151+
* If present, image will be analized and displayed according to its heigh and width.
152+
* Image width height greater than its width has a "portrait" orientation.
153+
* Otherwise it has a "landscape" orientation.
154+
* - If value is "orientation", image will be displayed complete by giving 100%
155+
* to width prop if the orientation is "landscape".
156+
* When orientation is "portrait", height prop will be set to 100%. Some images
157+
* will show an empty space.
158+
* - If value is "center", image will be centered and will not be displayed complete.
159+
* This the empty space is avoided. This is achived by giving 100% to width prop if
160+
* the orientation is "portrait". When orientation is "landscape", height prop will be set to 100%.
161+
* @default orientation
162+
*/
163+
smartImgFit?: false | "orientation" | "center";
148164
}
149165
//React.HTMLProps<HTMLDivElement>
150166
export type FileMosaicProps =

src/files-ui/components/previews/ImagePreview/ImagePreview.tsx

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,21 @@ import "./ImagePreview.scss";
1111
const ImagePreview: React.FC<ImagePreviewProps> = (
1212
props: ImagePreviewProps
1313
) => {
14-
const { src, alt, className, style, width, height, onError, smart } =
15-
mergeProps(props, ImagePreviewDefaultProps);
14+
const {
15+
src,
16+
alt,
17+
className,
18+
style,
19+
width,
20+
height,
21+
onError,
22+
smart,
23+
smartImgFit,
24+
} = mergeProps(props, ImagePreviewDefaultProps);
25+
26+
const [[finalHeight, finalWidth], setfinalDimensions] = React.useState<
27+
[number | undefined, number | undefined]
28+
>([undefined, undefined]);
1629

1730
//console.table({ src, alt, className, style, width, height });
1831
const [source, setSource] = React.useState<string | undefined>(undefined);
@@ -30,21 +43,42 @@ const ImagePreview: React.FC<ImagePreviewProps> = (
3043

3144
setSource(imageSource);
3245

33-
if (!smart) return;
46+
let finalHeight = undefined;
47+
let finalWidth = undefined;
3448

35-
const orientation: "landscape" | "portrait" = await getImageOrientation(
36-
imageSource
37-
);
38-
setOrientation(orientation);
49+
if (!smartImgFit) {
50+
//if not given
51+
finalWidth = 100;
52+
} else {
53+
const orientation: "landscape" | "portrait" = await getImageOrientation(
54+
imageSource
55+
);
56+
if (orientation === "landscape") {
57+
if (smartImgFit === "orientation") {
58+
finalHeight = undefined;
59+
finalWidth = 100;
60+
} else {
61+
finalHeight = 100;
62+
finalWidth = undefined;
63+
}
64+
} else {
65+
if (smartImgFit === "center") {
66+
finalHeight = undefined;
67+
finalWidth = 100;
68+
} else {
69+
finalHeight = 100;
70+
finalWidth = undefined;
71+
}
72+
}
73+
}
3974

75+
setfinalDimensions([finalHeight, finalWidth]);
4076
setSource(imageSource);
4177
};
4278

4379
React.useEffect(() => {
4480
//if not undefined
45-
if (!src) {
46-
return;
47-
}
81+
if (!src) return;
4882
//console.log("ImagePreview There is source :D");
4983

5084
if (typeof src === "string") {
@@ -53,20 +87,18 @@ const ImagePreview: React.FC<ImagePreviewProps> = (
5387
} else {
5488
//if a File object is given, check if is a supported format and read it
5589
const headerMime = src.type ? src.type.split("/")[0] : "octet";
56-
57-
if (headerMime === "image") {
58-
//set the video source and create the uri string if is a supported video format
90+
if (headerMime === "image")
91+
//set the image source and create the uri string if it's a supported image format
5992
getSource(src);
60-
}
6193
}
6294
// eslint-disable-next-line
6395
}, [src]);
6496
//console.log("ImagePreview", src, source);
6597

66-
const finalWidth: string | number | undefined =
98+
/* const finalWidth: string | number | undefined =
6799
width || (orientation === "landscape" && smart ? "100%" : undefined);
68100
const finalHeight: string | number | undefined =
69-
height || (orientation === "portrait" && smart ? "100%" : undefined);
101+
height || (orientation === "portrait" && smart ? "100%" : undefined); */
70102

71103
console.log("Image result", finalHeight, finalWidth, orientation, smart);
72104
const handleError = (evt: React.SyntheticEvent<HTMLImageElement, Event>) => {
@@ -76,14 +108,14 @@ const ImagePreview: React.FC<ImagePreviewProps> = (
76108

77109
return (
78110
<React.Fragment>
79-
{src && source && (
111+
{src && source && (finalHeight || finalWidth) && (
80112
<img
81113
style={style || {}}
82114
onClick={(evt) => {
83115
evt.preventDefault();
84116
}}
85-
width={!smart && !finalWidth?"100%":finalWidth}
86-
height={finalHeight}
117+
width={finalWidth?`${finalWidth}%`:finalWidth}
118+
height={finalHeight?`${finalHeight}%`:finalHeight}
87119
src={source}
88120
alt={alt}
89121
className={className}

src/files-ui/components/previews/ImagePreview/ImagePreviewProps.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { OverridableComponentProps } from "../../overridable";
22

33

44

5-
export interface ImagePreviewProps extends OverridableComponentProps {
5+
export interface ImagePreviewFullProps extends OverridableComponentProps {
66
/**
77
* Image source in string format (URL) or File Object (Will be read as URL)
88
*/
@@ -31,12 +31,39 @@ export interface ImagePreviewProps extends OverridableComponentProps {
3131
* In that case height will be set to 100%. Otherwise width will be set to 100%
3232
*/
3333
smart?: boolean;
34+
/**
35+
* If not present, image width will be set to 100%.
36+
*
37+
* If present, image will be analized and displayed according to its heigh and width.
38+
* Image width height greater than its width has a "portrait" orientation.
39+
* Otherwise it has a "landscape" orientation.
40+
* - If value is "orientation", image will be displayed complete by giving 100%
41+
* to width prop if the orientation is "landscape".
42+
* When orientation is "portrait", height prop will be set to 100%. Some images
43+
* will show an empty space.
44+
* - If value is "center", image will be centered and will not be displayed complete.
45+
* This the empty space is avoided. This is achived by giving 100% to width prop if
46+
* the orientation is "portrait". When orientation is "landscape", height prop will be set to 100%.
47+
* @default orientation
48+
*/
49+
smartImgFit?: false | "orientation" | "center";
3450

3551
}
52+
/*
53+
type DefImageProps = React.HTMLProps<HTMLImageElement>;
54+
type ImgPropsOmitImagePreviewFullProps = Omit<DefImageProps, keyof ImagePreviewFullProps>;
55+
*/
56+
57+
export declare type ImagePreviewProps =
58+
//ImgPropsOmitImagePreviewFullProps &
59+
{
60+
[I in keyof ImagePreviewFullProps]: ImagePreviewFullProps[I]
61+
}
62+
3663
export const ImagePreviewDefaultProps: ImagePreviewProps = {
3764
//width: "100%",
3865
//height: "100%",
3966
alt: "image-preview",
4067
//className: "fui-image-preview"
41-
smart:true
68+
smartImgFit: "orientation"
4269
}

0 commit comments

Comments
 (0)