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 for latest posts block #1803

Merged
merged 6 commits into from Jul 10, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions blocks/library/latest-posts/block.scss
@@ -0,0 +1,6 @@
.blocks-latest-posts.alignright {
margin-left: 2em;
}
.blocks-latest-posts.alignleft {
margin-right: 2em;
}
24 changes: 22 additions & 2 deletions blocks/library/latest-posts/index.js
Expand Up @@ -10,12 +10,14 @@ import moment from 'moment';
* Internal dependencies
*/
import './style.scss';
import './block.scss';
import { registerBlockType } from '../../api';
import { getLatestPosts } from './data.js';
import InspectorControls from '../../inspector-controls';
import TextControl from '../../inspector-controls/text-control';
import ToggleControl from '../../inspector-controls/toggle-control';
import BlockDescription from '../../block-description';
import BlockAlignmentToolbar from '../../block-alignment-toolbar';

const MIN_POSTS = 1;
const MAX_POSTS = 100;
Expand All @@ -32,6 +34,13 @@ registerBlockType( 'core/latest-posts', {
displayPostDate: false,
},

getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( 'left' === align || 'right' === align || 'wide' === align || 'full' === align ) {
return { 'data-align': align };
}
},

edit: class extends Component {
constructor() {
super( ...arguments );
Expand Down Expand Up @@ -85,6 +94,7 @@ registerBlockType( 'core/latest-posts', {

render() {
const { latestPosts } = this.state;
const { setAttributes } = this.props;

if ( ! latestPosts.length ) {
return (
Expand All @@ -98,7 +108,7 @@ registerBlockType( 'core/latest-posts', {
}

const { focus } = this.props;
const { displayPostDate } = this.props.attributes;
const { displayPostDate, align } = this.props.attributes;

return [
focus && (
Expand All @@ -107,6 +117,16 @@ registerBlockType( 'core/latest-posts', {
<p>{ __( 'Shows a list of your site\'s most recent posts.' ) }</p>
</BlockDescription>
<h3>{ __( 'Latest Posts Settings' ) }</h3>

<p>{ __( 'Alignment' ) }</p>
<BlockAlignmentToolbar
value={ align }
onChange={ ( nextAlign ) => {
setAttributes( { align: nextAlign } );
} }
controls={ [ 'left', 'center', 'right', 'wide', 'full' ] }
/>

<ToggleControl
label={ __( 'Display post date' ) }
checked={ displayPostDate }
Expand All @@ -125,7 +145,7 @@ registerBlockType( 'core/latest-posts', {
<ul className={ this.props.className } key="latest-posts">
{ latestPosts.map( ( post, i ) =>
<li key={ i }>
<a href={ post.link }>{ post.title.rendered }</a>
<a href={ post.link } target="_blank">{ post.title.rendered }</a>
{ displayPostDate && post.date_gmt &&
<span className={ `${ this.props.className }__post-date` }>
{ moment( post.date_gmt ).local().format( 'MMM DD h:mm A' ) }
Expand Down
17 changes: 10 additions & 7 deletions blocks/library/latest-posts/style.scss
@@ -1,10 +1,13 @@

.wp-block-latest-posts {
padding-left: 2.5em;
}
.editor-visual-editor__block[data-type="core/latest-posts"] {

.wp-block-latest-posts {
padding-left: 2.5em;
}

.wp-block-latest-posts__post-date {
display: block;
color: $dark-gray-100;
font-size: $default-font-size;
.wp-block-latest-posts__post-date {
display: block;
color: $dark-gray-100;
font-size: $default-font-size;
}
}
9 changes: 8 additions & 1 deletion lib/blocks/latest-posts.php
Expand Up @@ -28,6 +28,11 @@ function gutenberg_render_block_core_latest_posts( $attributes ) {
}
}

$align = 'center';
if ( isset( $attributes['align'] ) && in_array( $attributes['align'], array( 'left', 'right', 'wide', 'full' ), true ) ) {
$align = $attributes['align'];
}

$recent_posts = wp_get_recent_posts( array(
'numberposts' => $posts_to_show,
'post_status' => 'publish',
Expand All @@ -43,8 +48,10 @@ function gutenberg_render_block_core_latest_posts( $attributes ) {
$posts_content .= "<li><a href='{$post_permalink}'>{$post_title}</a></li>\n";
}

$class = 'blocks-latest-posts ' . esc_attr( 'align' . $align );
Copy link
Contributor

Choose a reason for hiding this comment

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

We should follow the same convention as the frontend blocks to generate the classname. This should be wp-block-latest-posts


$block_content = <<<CONTENT
<div class="blocks-latest-posts">
<div class="{$class}">
<ul>
{$posts_content}
</ul>
Expand Down