-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
141 lines (124 loc) · 3.73 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
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
136
137
138
139
140
141
/**
* WordPress dependencies
*/
import { Placeholder } from 'components';
/**
* Internal dependencies
*/
import './style.scss';
import { registerBlockType, query } from '../../api';
import Editable from '../../editable';
import MediaUploadButton from '../../media-upload-button';
const { attr, children } = query;
/**
* Returns an attribute setter with behavior that if the target value is
* already the assigned attribute value, it will be set to undefined.
*
* @param {string} align Alignment value
* @return {Function} Attribute setter
*/
function toggleAlignment( align ) {
return ( attributes, setAttributes ) => {
const nextAlign = attributes.align === align ? undefined : align;
setAttributes( { align: nextAlign } );
};
}
registerBlockType( 'core/image', {
title: wp.i18n.__( 'Image' ),
icon: 'format-image',
category: 'common',
attributes: {
url: attr( 'img', 'src' ),
alt: attr( 'img', 'alt' ),
caption: children( 'figcaption' ),
},
controls: [
{
icon: 'align-left',
title: wp.i18n.__( 'Align left' ),
isActive: ( { align } ) => 'left' === align,
onClick: toggleAlignment( 'left' ),
},
{
icon: 'align-center',
title: wp.i18n.__( 'Align center' ),
isActive: ( { align } ) => ! align || 'center' === align,
onClick: toggleAlignment( 'center' ),
},
{
icon: 'align-right',
title: wp.i18n.__( 'Align right' ),
isActive: ( { align } ) => 'right' === align,
onClick: toggleAlignment( 'right' ),
},
{
icon: 'align-full-width',
title: wp.i18n.__( 'Wide width' ),
isActive: ( { align } ) => 'wide' === align,
onClick: toggleAlignment( 'wide' ),
},
],
getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( 'left' === align || 'right' === align || 'wide' === align ) {
return { 'data-align': align };
}
},
edit( { attributes, setAttributes, focus, setFocus } ) {
const { url, alt, caption } = attributes;
if ( ! url ) {
const uploadButtonProps = { isLarge: true };
const setMediaURL = ( media ) => setAttributes( { url: media.url } );
return (
<Placeholder
instructions={ wp.i18n.__( 'Drag image here or insert from media library' ) }
icon="format-image"
label={ wp.i18n.__( 'Image' ) }
className="blocks-image">
<MediaUploadButton
buttonProps={ uploadButtonProps }
onSelect={ setMediaURL }
type="image"
auto-open
>
{ wp.i18n.__( 'Insert from Media Library' ) }
</MediaUploadButton>
</Placeholder>
);
}
const focusCaption = ( focusValue ) => setFocus( { editable: 'caption', ...focusValue } );
// Disable reason: Each block can be selected by clicking on it
/* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
return (
<figure className="blocks-image">
<img src={ url } alt={ alt } onClick={ setFocus } />
{ ( caption && caption.length > 0 ) || !! focus ? (
<Editable
tagName="figcaption"
placeholder={ wp.i18n.__( 'Write caption…' ) }
value={ caption }
focus={ focus && focus.editable === 'caption' ? focus : undefined }
onFocus={ focusCaption }
onChange={ ( value ) => setAttributes( { caption: value } ) }
inline
inlineToolbar
/>
) : null }
</figure>
);
/* eslint-enable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
},
save( { attributes } ) {
const { url, alt, caption, align = 'none' } = attributes;
const img = <img src={ url } alt={ alt } className={ `align${ align }` } />;
if ( ! caption || ! caption.length ) {
return img;
}
return (
<figure>
{ img }
<figcaption>{ caption }</figcaption>
</figure>
);
},
} );