-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathgallery-image.js
67 lines (58 loc) · 1.79 KB
/
gallery-image.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
/**
* External Depenedencies
*/
import classnames from 'classnames';
/**
* WordPress Dependencies
*/
import { Component } from '@wordpress/element';
import { IconButton, withAPIData, Spinner } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
class GalleryImage extends Component {
componentWillReceiveProps( { image } ) {
if ( image && image.data && ! this.props.url ) {
this.props.setAttributes( {
url: image.data.source_url,
alt: image.data.alt_text,
} );
}
}
render() {
const { url, alt, id, linkTo, link, isSelected, onClick, onRemove } = this.props;
let href;
switch ( linkTo ) {
case 'media':
href = url;
break;
case 'attachment':
href = link;
break;
}
const img = url ? <img src={ url } alt={ alt } data-id={ id } /> : <Spinner />;
const className = classnames( {
'is-selected': isSelected,
'is-transient': 0 === url.indexOf( 'blob:' ),
} );
// Disable reason: Each block can be selected by clicking on it and we should keep the same saved markup
/* eslint-disable jsx-a11y/no-noninteractive-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
return (
<figure className={ className } onClick={ onClick }>
{ isSelected &&
<div className="blocks-gallery-item__inline-menu">
<IconButton
icon="no-alt"
onClick={ onRemove }
className="blocks-gallery-item__remove"
label={ __( 'Remove Image' ) }
/>
</div>
}
{ href ? <a href={ href }>{ img }</a> : img }
</figure>
);
/* eslint-enable jsx-a11y/no-noninteractive-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
}
}
export default withAPIData( ( { id } ) => ( {
image: id ? `/wp/v2/media/${ id }` : {},
} ) )( GalleryImage );