-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (95 loc) · 3.43 KB
/
index.js
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
import { __ } from '@wordpress/i18n';
import { Button, FocalPointPicker, PanelBody } from '@wordpress/components';
import { MediaPlaceholder, InspectorControls } from '@wordpress/block-editor';
import { MediaToolbar } from '../MediaToolbar';
const SETTINGS_TEXT = __( 'Image settings', 'meom-block-components' );
const FOCALPOINT_LABEL = __( 'Image focalpoint', 'meom-block-components' );
const FOCALPOINT_HELP = __(
'Pick image focalpoint which is central point of the image.',
'meom-block-components'
);
const BUTTON_TEXT = __( 'Remove Image', 'meom-block-components' );
/**
* Image select control.
*
* @param {Object} props - Component props.
* @return {Node} Component.
*/
function ImageSelect( props ) {
const {
image,
onChange,
settingsText = SETTINGS_TEXT,
showRemoveImageButton = true,
buttonText = BUTTON_TEXT,
useFocalPoint = false,
useMediaToolbar = true,
focalPointLabel = FOCALPOINT_LABEL,
focalPointHelp = FOCALPOINT_HELP,
focalPoint = { x: 0.5, y: 0.5 },
onChangeFocalPoint,
...rest
} = props;
const imageId = image && image.id;
const imageUrl = image && image.url;
if ( useFocalPoint ) {
const focalPointStyle = {
objectFit: 'cover',
objectPosition: `${ focalPoint.x * 100 }% ${ focalPoint.y * 100 }%`,
};
// Set style changes from focalPointStyle object and use it with image.
rest.style = {
...rest.style,
...focalPointStyle,
};
}
return (
<>
{ ! imageId ? (
<MediaPlaceholder
onSelect={ ( { id, url } ) => {
onChange( { id, url } );
} }
allowedTypes={ [ 'image' ] }
></MediaPlaceholder>
) : (
<>
{ useFocalPoint && (
<InspectorControls>
<PanelBody title={ settingsText }>
<FocalPointPicker
label={ focalPointLabel }
help={ focalPointHelp }
url={ imageUrl }
value={ focalPoint }
onChange={ onChangeFocalPoint }
/>
</PanelBody>
</InspectorControls>
) }
{ useMediaToolbar && (
<MediaToolbar
id={ imageId }
onSelect={ ( { id, url } ) => {
onChange( { id, url } );
} }
onRemove={ () => {
onChange( null );
} }
/>
) }
<img src={ imageUrl } alt="" { ...rest } />
{ showRemoveImageButton && (
<Button
className="meom-media-button button button-large"
onClick={ () => onChange( null ) }
>
{ buttonText }
</Button>
) }
</>
) }
</>
);
}
export default ImageSelect;