-
-
Notifications
You must be signed in to change notification settings - Fork 871
Expand file tree
/
Copy pathImageBlockEdit.tsx
More file actions
109 lines (99 loc) · 2.65 KB
/
Copy pathImageBlockEdit.tsx
File metadata and controls
109 lines (99 loc) · 2.65 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
import { useCallback } from 'react';
import type {
BlockEditProps,
Brain,
ContainedItem,
Content,
RelatedItem,
} from '@plone/types';
import Image from '@plone/layout/components/Image/Image';
import clsx from 'clsx';
import styles from './Image.module.css';
import config from '@plone/registry';
function flattenToAppUrl(url: string) {
const apiPath = config.settings.apiPath || '';
if (!apiPath || !url) return url;
return url.replace(`${apiPath}/`, '/').replace(apiPath, '/');
}
function isInternalUrl(url: string) {
return url.startsWith('/') || url.includes('/++api++/');
}
function getLegacyScaledSrc(url: string, size: string) {
const base = flattenToAppUrl(url);
if (size === 'm') return `${base}/@@images/image/preview`;
if (size === 's') return `${base}/@@images/image/mini`;
return `${base}/@@images/image`;
}
const ImageBlockEdit = (props: BlockEditProps) => {
const { block, data, setBlock, selected } = props;
const ImageWidget = config.getWidget('image') as
| React.ComponentType<any>
| undefined;
const handleChange = useCallback(
(
image: string | null,
item: {
title?: string;
image_field?: string;
image_scales?: Record<string, unknown>;
} = {},
) => {
const { title, image_field, image_scales } = item;
const url = image ? flattenToAppUrl(image) : '';
setBlock({
...data,
url,
image_field,
image_scales,
alt: data.alt || title || '',
});
},
[data, setBlock],
);
const imageItem = data.image_scales
? ({
'@id': data.url,
image_field: data.image_field,
image_scales: data.image_scales,
} as unknown as Content | Brain | ContainedItem | RelatedItem)
: undefined;
return (
<div
className={clsx(
'image align block',
{
center: !Boolean(data.align),
},
styles['block'],
data.align,
data.size,
)}
>
{data.url ? (
<Image
item={imageItem}
src={
data.image_scales
? undefined
: isInternalUrl(data.url)
? getLegacyScaledSrc(data.url, data.size || 'l')
: data.url
}
alt={data.alt || ''}
loading="lazy"
responsive={true}
/>
) : ImageWidget ? (
<ImageWidget
onChange={handleChange}
value={data.url || ''}
placeholderLinkInput={data.placeholder}
id={block}
selected={selected}
objectBrowserPickerType="image"
/>
) : null}
</div>
);
};
export default ImageBlockEdit;