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 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
6 changes: 6 additions & 0 deletions blocks/library/latest-posts/block.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.wp-block-latest-posts.alignright {
margin-left: 2em;
}
.wp-block-latest-posts.alignleft {
margin-right: 2em;
}
27 changes: 25 additions & 2 deletions blocks/library/latest-posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ 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 BlockControls from '../../block-controls';
import BlockAlignmentToolbar from '../../block-alignment-toolbar';

const MIN_POSTS = 1;
const MAX_POSTS = 100;
Expand All @@ -32,6 +35,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 +95,7 @@ registerBlockType( 'core/latest-posts', {

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

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

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

return [
focus && (
<BlockControls key="controls">
<BlockAlignmentToolbar
value={ align }
onChange={ ( nextAlign ) => {
setAttributes( { align: nextAlign } );
} }
controls={ [ 'left', 'center', 'right', 'wide', 'full' ] }
/>
</BlockControls>
),
focus && (
<InspectorControls key="inspector">
<BlockDescription>
<p>{ __( 'Shows a list of your site\'s most recent posts.' ) }</p>
</BlockDescription>
<h3>{ __( 'Latest Posts Settings' ) }</h3>

<ToggleControl
label={ __( 'Display post date' ) }
checked={ displayPostDate }
Expand All @@ -125,7 +148,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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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 = 'wp-block-latest-posts ' . esc_attr( 'align' . $align );

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