-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Implement image alignment controls #416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,32 @@ import Editable from 'components/editable'; | |
|
|
||
| const { attr, children } = query; | ||
|
|
||
| /** | ||
| * Component returning an image element. | ||
| * | ||
| * @param {string} props.url Image source | ||
| * @param {string} props.alt Image alt | ||
| * @param {?string} props.align Optional alignment | ||
| * @return {WPElement} Image element | ||
| */ | ||
| function Image( { url, alt, align = 'none' } ) { | ||
| return <img src={ url } alt={ alt } className={ `align${ align }` } />; | ||
| } | ||
|
|
||
| /** | ||
| * 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 applyOrUnset( align ) { | ||
| return ( attributes, setAttributes ) => { | ||
| const nextAlign = attributes.align === align ? undefined : align; | ||
| setAttributes( { align: nextAlign } ); | ||
| }; | ||
| } | ||
|
|
||
| registerBlock( 'core/image', { | ||
| title: wp.i18n.__( 'Image' ), | ||
|
|
||
|
|
@@ -16,15 +42,43 @@ registerBlock( 'core/image', { | |
| attributes: { | ||
| url: attr( 'img', 'src' ), | ||
| alt: attr( 'img', 'alt' ), | ||
| caption: children( 'figcaption' ) | ||
| caption: children( 'figcaption' ), | ||
| align: ( node ) => ( node.className.match( /\balign(\S+)/ ) || [] )[ 1 ] | ||
| }, | ||
|
|
||
| controls: [ | ||
| { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the full-bleed or whatever we call that missing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it's not an alignment. It's a width manipulation.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But it's a control. |
||
| icon: 'align-left', | ||
| title: wp.i18n.__( 'Align left' ), | ||
| isActive: ( { align } ) => 'left' === align, | ||
| onClick: applyOrUnset( 'left' ) | ||
| }, | ||
| { | ||
| icon: 'align-center', | ||
| title: wp.i18n.__( 'Align center' ), | ||
| isActive: ( { align } ) => 'center' === align, | ||
| onClick: applyOrUnset( 'center' ) | ||
| }, | ||
| { | ||
| icon: 'align-right', | ||
| title: wp.i18n.__( 'Align right' ), | ||
| isActive: ( { align } ) => 'right' === align, | ||
| onClick: applyOrUnset( 'right' ) | ||
| }, | ||
| { | ||
| icon: 'align-none', | ||
| title: wp.i18n.__( 'No alignment' ), | ||
| isActive: ( { align } ) => ! align || 'none' === align, | ||
| onClick: applyOrUnset( 'none' ) | ||
| } | ||
| ], | ||
|
|
||
| edit( { attributes, setAttributes, focus, setFocus } ) { | ||
| const { url, alt, caption } = attributes; | ||
| const { url, alt, align, caption } = attributes; | ||
|
|
||
| return ( | ||
| <figure> | ||
| <img src={ url } alt={ alt } /> | ||
| <Image url={ url } alt={ alt } align={ align } /> | ||
| { caption || !! focus ? ( | ||
| <Editable | ||
| tagName="figcaption" | ||
|
|
@@ -39,8 +93,8 @@ registerBlock( 'core/image', { | |
| }, | ||
|
|
||
| save( { attributes } ) { | ||
| const { url, alt, caption } = attributes; | ||
| const img = <img src={ url } alt={ alt } />; | ||
| const { url, alt, align, caption } = attributes; | ||
| const img = <Image url={ url } alt={ alt } align={ align } />; | ||
|
|
||
| if ( ! caption ) { | ||
| return img; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this just be toggle? :D
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're too slow @mtias *! But yeah, toggle is appropriate too.
Edit: * Re: Having been merged seconds prior.