Skip to content
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

Add alignment buttons for embed #840

Merged
merged 1 commit into from
May 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions blocks/library/embed/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ import Editable from '../../editable';

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 } );
};
}

registerBlock( 'core/embed', {
title: wp.i18n.__( 'Embed' ),

Expand All @@ -26,6 +40,40 @@ registerBlock( 'core/embed', {
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, title, caption } = attributes;

Expand Down
42 changes: 42 additions & 0 deletions blocks/library/embed/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,45 @@
width: 100%;
height: 100%;
}

div[data-type="core/embed"] {
&[data-align="left"],
&[data-align="right"] {
// Without z-index, won't be clickable as "above" adjacent content
z-index: z-index( '.editor-visual-editor__block {core/image aligned left or right}' );
width: 370px;
}

&[data-align="left"] {
float: left;
margin-right: $block-padding;
}

&[data-align="right"] {
float: right;
margin-left: $block-padding;
}

&[data-align="wide"] {
padding-left: 0;
padding-right: 0;
margin-right: -#{ $block-padding + $block-mover-margin }; /* Compensate for .editor-visual-editor centering padding */

&:before {
left: 0;
border-left-width: 0;
border-right-width: 0;
}

.editor-block-mover {
display: none;
}

.editor-visual-editor__block-controls {
max-width: #{ $visual-editor-max-width - $block-padding - ( $block-padding * 2 + $block-mover-margin ) };
margin-left: auto;
margin-right: auto;
}

}
}
2 changes: 1 addition & 1 deletion blocks/library/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ registerBlock( 'core/image', {
{
icon: 'align-center',
title: wp.i18n.__( 'Align center' ),
isActive: ( { align } ) => 'center' === align,
isActive: ( { align } ) => ! align || 'center' === align,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No align isn't same as center aligned, is it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm confusing this... It has the same style.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears the same in the editor, but different on the front-end.

none center
none center

We should probably make this distinction more obvious in the editor though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably would be good to get #812 in, before fixing centering issues.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with reverting that part.

onClick: toggleAlignment( 'center' ),
},
{
Expand Down