Skip to content

Commit 2793467

Browse files
committed
Implement image alignment controls
1 parent 3911be4 commit 2793467

File tree

2 files changed

+65
-14
lines changed

2 files changed

+65
-14
lines changed

blocks/library/image/index.js

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ import Editable from 'components/editable';
66

77
const { attr, html } = query;
88

9+
function Image( { url, alt, align } ) {
10+
let className;
11+
if ( align ) {
12+
className = `align${ align }`;
13+
}
14+
15+
return <img src={ url } alt={ alt } className={ className } />;
16+
}
17+
18+
function applyOrUnset( align ) {
19+
return ( attributes, setAttributes ) => {
20+
const nextAlign = attributes.align === align ? undefined : align;
21+
setAttributes( { align: nextAlign } );
22+
};
23+
}
24+
925
registerBlock( 'core/image', {
1026
title: wp.i18n.__( 'Image' ),
1127

@@ -16,15 +32,43 @@ registerBlock( 'core/image', {
1632
attributes: {
1733
url: attr( 'img', 'src' ),
1834
alt: attr( 'img', 'alt' ),
19-
caption: html( 'figcaption' )
35+
caption: html( 'figcaption' ),
36+
align: ( node ) => ( node.className.match( /\balign(\S+)/ ) || [] )[ 1 ]
2037
},
2138

39+
controls: [
40+
{
41+
icon: 'align-left',
42+
title: wp.i18n.__( 'Align left' ),
43+
isActive: ( { align } ) => 'left' === align,
44+
onClick: applyOrUnset( 'left' )
45+
},
46+
{
47+
icon: 'align-center',
48+
title: wp.i18n.__( 'Align center' ),
49+
isActive: ( { align } ) => 'center' === align,
50+
onClick: applyOrUnset( 'center' )
51+
},
52+
{
53+
icon: 'align-right',
54+
title: wp.i18n.__( 'Align right' ),
55+
isActive: ( { align } ) => 'right' === align,
56+
onClick: applyOrUnset( 'right' )
57+
},
58+
{
59+
icon: 'align-none',
60+
title: wp.i18n.__( 'No alignment' ),
61+
isActive: ( { align } ) => ! align || 'none' === align,
62+
onClick: applyOrUnset( 'none' )
63+
}
64+
],
65+
2266
edit( { attributes, isSelected, setAttributes } ) {
23-
const { url, alt, caption } = attributes;
67+
const { url, alt, align, caption } = attributes;
2468

2569
return (
2670
<figure>
27-
<img src={ url } alt={ alt } />
71+
<Image url={ url } alt={ alt } align={ align } />
2872
{ caption || isSelected ? (
2973
<Editable
3074
tagName="figcaption"
@@ -37,8 +81,8 @@ registerBlock( 'core/image', {
3781
},
3882

3983
save( { attributes } ) {
40-
const { url, alt, caption } = attributes;
41-
const img = <img src={ url } alt={ alt } />;
84+
const { url, alt, align, caption } = attributes;
85+
const img = <Image url={ url } alt={ alt } align={ align } />;
4286

4387
if ( ! caption ) {
4488
return img;

languages/gutenberg.pot

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,40 @@ msgstr ""
77
msgid "Freeform"
88
msgstr ""
99

10-
#: blocks/library/image/index.js:10
10+
#: blocks/library/image/index.js:26
1111
msgid "Image"
1212
msgstr ""
1313

14-
#: blocks/library/image/index.js:31
15-
msgid "Write caption…"
16-
msgstr ""
17-
18-
#: blocks/library/list/index.js:11
19-
msgid "List"
20-
msgstr ""
21-
14+
#: blocks/library/image/index.js:42
2215
#: blocks/library/list/index.js:25
2316
#: blocks/library/text/index.js:24
2417
msgid "Align left"
2518
msgstr ""
2619

20+
#: blocks/library/image/index.js:48
2721
#: blocks/library/list/index.js:33
2822
#: blocks/library/text/index.js:32
2923
msgid "Align center"
3024
msgstr ""
3125

26+
#: blocks/library/image/index.js:54
3227
#: blocks/library/list/index.js:41
3328
#: blocks/library/text/index.js:40
3429
msgid "Align right"
3530
msgstr ""
3631

32+
#: blocks/library/image/index.js:60
33+
msgid "No alignment"
34+
msgstr ""
35+
36+
#: blocks/library/image/index.js:75
37+
msgid "Write caption…"
38+
msgstr ""
39+
40+
#: blocks/library/list/index.js:11
41+
msgid "List"
42+
msgstr ""
43+
3744
#: blocks/library/list/index.js:49
3845
msgid "Justify"
3946
msgstr ""

0 commit comments

Comments
 (0)