-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathPointImage.jsx
More file actions
135 lines (126 loc) · 3.33 KB
/
PointImage.jsx
File metadata and controls
135 lines (126 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { Box, CardMedia, Modal, Typography } from '@mui/material';
import { alpha, useTheme } from '@mui/material/styles';
function PointImage({ data, sx }) {
const theme = useTheme();
const [fullScreenImg, setFullScreenImg] = useState(null);
const renderImages = () => {
const images = [];
function isImgUrl(string) {
// Handle data URIs
if (/^data:image\//.test(string)) {
return true;
}
let url;
try {
url = new URL(string);
} catch (_) {
return false;
}
if (url) {
const pathname = url.pathname.split('?')[0].split('#')[0];
return /\.(jpg|jpeg|png|webp|gif|svg|bmp|ico|tiff|tif|avif|heic|heif)$/i.test(pathname);
}
return false;
}
// Loop through the object's properties
for (const key in data) {
if (typeof data[key] == 'string') {
// Check if the value is an image URL
if (isImgUrl(data[key])) {
images.push(
<Box
key={key}
sx={{
py: 1,
px: 0.5,
ml: 5,
}}
>
<CardMedia
component="img"
sx={{
width: '120px',
height: '120px',
aspectRatio: '1/1',
wordWrap: 'break-word',
border: `1px solid ${theme.palette.divider}`,
borderRadius: '5px',
...sx,
}}
image={data[key]}
alt={data[key]}
onClick={() => setFullScreenImg(data[key])}
/>
</Box>
);
}
}
}
return images;
};
const images = renderImages();
if (images.length === 0) {
return null;
}
return (
<Box>
{images}
<Modal
open={!!fullScreenImg}
onClose={() => setFullScreenImg(null)}
slotProps={{
backdrop: {
sx: { cursor: 'pointer' },
title: 'Click to close',
},
}}
>
<Box
sx={{
position: 'absolute',
top: '50%',
left: '50%',
transform: 'translate(-50%,-50%)',
maxWidth: '100%',
maxHeight: '100%',
'&:focus': { outline: 'none' },
}}
>
<Typography
variant="h6"
component="p"
onClick={() => setFullScreenImg(null)}
sx={{
position: 'absolute',
top: '-60px',
right: 0,
padding: 1,
cursor: 'pointer',
color: theme.palette.common.white,
backgroundColor: alpha(theme.palette.common.black, 0.5),
borderRadius: '5px',
}}
>
Close [ESC]
</Typography>
<img
src={fullScreenImg}
alt={fullScreenImg}
style={{
maxWidth: '100%',
maxHeight: '100%',
}}
/>
</Box>
</Modal>
</Box>
);
}
PointImage.propTypes = {
data: PropTypes.object.isRequired,
sx: PropTypes.object,
xs: PropTypes.number, // Size of the image grid item. Default is 3.
};
export default PointImage;