Skip to content

Commit

Permalink
Added text align feature to cover image. (#4066)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Jan 19, 2018
1 parent 6dd01de commit 3568fc4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
8 changes: 8 additions & 0 deletions blocks/library/cover-image/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@
&.components-placeholder h2 {
color: inherit;
}

&.has-left-content .block-editable__inline-toolbar {
justify-content: flex-start;
}

&.has-right-content .block-editable__inline-toolbar{
justify-content: flex-end;
}
}
25 changes: 22 additions & 3 deletions blocks/library/cover-image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { isEmpty } from 'lodash';
/**
* WordPress dependencies
*/
import { IconButton, Toolbar } from '@wordpress/components';
import { IconButton, PanelBody, Toolbar } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import classnames from 'classnames';

Expand All @@ -17,6 +17,7 @@ import './editor.scss';
import './style.scss';
import { registerBlockType, createBlock } from '../../api';
import Editable from '../../editable';
import AlignmentToolbar from '../../alignment-toolbar';
import MediaUpload from '../../media-upload';
import ImagePlaceHolder from '../../image-placeholder';
import BlockControls from '../../block-controls';
Expand Down Expand Up @@ -48,6 +49,10 @@ registerBlockType( 'core/cover-image', {
align: {
type: 'string',
},
contentAlign: {
type: 'string',
default: 'center',
},
id: {
type: 'number',
},
Expand Down Expand Up @@ -90,7 +95,7 @@ registerBlockType( 'core/cover-image', {
},

edit( { attributes, setAttributes, focus, setFocus, className } ) {
const { url, title, align, id, hasParallax, dimRatio } = attributes;
const { url, title, align, contentAlign, id, hasParallax, dimRatio } = attributes;
const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } );
const onSelectImage = ( media ) => setAttributes( { url: media.url, id: media.id } );
const toggleParallax = () => setAttributes( { hasParallax: ! hasParallax } );
Expand All @@ -101,20 +106,30 @@ registerBlockType( 'core/cover-image', {
undefined;
const classes = classnames(
className,
contentAlign !== 'center' && `has-${ contentAlign }-content`,
dimRatioToClass( dimRatio ),
{
'has-background-dim': dimRatio !== 0,
'has-parallax': hasParallax,
}
);

const alignmentToolbar = (
<AlignmentToolbar
value={ contentAlign }
onChange={ ( nextAlign ) => {
setAttributes( { contentAlign: nextAlign } );
} }
/>
);
const controls = focus && [
<BlockControls key="controls">
<BlockAlignmentToolbar
value={ align }
onChange={ updateAlignment }
/>

{ alignmentToolbar }
<Toolbar>
<MediaUpload
onSelect={ onSelectImage }
Expand Down Expand Up @@ -146,6 +161,9 @@ registerBlockType( 'core/cover-image', {
max={ 100 }
step={ 10 }
/>
<PanelBody title={ __( 'Text Alignment' ) }>
{ alignmentToolbar }
</PanelBody>
</InspectorControls>,
];

Expand Down Expand Up @@ -195,7 +213,7 @@ registerBlockType( 'core/cover-image', {
},

save( { attributes, className } ) {
const { url, title, hasParallax, dimRatio, align } = attributes;
const { url, title, hasParallax, dimRatio, align, contentAlign } = attributes;
const style = url ?
{ backgroundImage: `url(${ url })` } :
undefined;
Expand All @@ -205,6 +223,7 @@ registerBlockType( 'core/cover-image', {
{
'has-background-dim': dimRatio !== 0,
'has-parallax': hasParallax,
[ `has-${ contentAlign }-content` ]: contentAlign !== 'center',
},
align ? `align${ align }` : null,
);
Expand Down
16 changes: 16 additions & 0 deletions blocks/library/cover-image/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@
justify-content: center;
align-items: center;

&.has-left-content {
justify-content: flex-start;
h2 {
margin-left: 0;
text-align: left;
}
}

&.has-right-content {
justify-content: flex-end;
h2 {
margin-right: 0;
text-align: right;
}
}

h2 {
color: white;
font-size: 24pt;
Expand Down
3 changes: 2 additions & 1 deletion blocks/test/fixtures/core__cover-image.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
],
"url": "https://cldup.com/uuUqE_dXzy.jpg",
"hasParallax": false,
"dimRatio": 40
"dimRatio": 40,
"contentAlign": "center"
},
"originalContent": "<section class=\"wp-block-cover-image has-background-dim-40 has-background-dim\" style=\"background-image:url(https://cldup.com/uuUqE_dXzy.jpg)\">\n <h2>Guten Berg!</h2>\n</section>"
}
Expand Down

0 comments on commit 3568fc4

Please sign in to comment.