-
Notifications
You must be signed in to change notification settings - Fork 0
/
RudrImageControl.js
50 lines (43 loc) · 1.33 KB
/
RudrImageControl.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
const { Button, Spinner, BaseControl } = window.wp.components
const { MediaUpload, MediaUploadCheck } = window.wp.blockEditor
const { useSelect, useDispatch } = window.wp.data
const RudrImageControl = ( props ) => {
const { imageId, image } = useSelect( select => {
const id = select( 'core/editor' ).getEditedPostAttribute( 'meta' )[ props.metaKey ];
return {
imageId: id,
image: select( 'core' ).getMedia( id ),
}
})
const { editPost } = useDispatch( 'core/editor', [ imageId ] )
return(
<BaseControl id={ props.metaKey } label={ props.label }>
<MediaUploadCheck>
<MediaUpload
onSelect={ ( media ) => editPost( { meta: { [props.metaKey]: media.id } } ) }
allowedTypes={ [ 'image' ] }
value={ imageId }
render={ ( { open } ) => (
<div>
{ ! imageId && <Button variant="secondary" onClick={ open }>Upload image</Button> }
{ !! imageId && ! image &&
<Spinner />
}
{ !! image && image &&
<Button variant="link" onClick={ open }>
<img src={ image.source_url } />
</Button>
}
</div>
) }
/>
</MediaUploadCheck>
{ !! imageId &&
<Button onClick={ () => editPost( { meta: { [props.metaKey]: null } } ) } isLink isDestructive>
Remove image
</Button>
}
</BaseControl>
)
}
export default RudrImageControl