Skip to content

Commit

Permalink
Add calendar block v1 (#13772)
Browse files Browse the repository at this point in the history
The first version of the calendar block.
The design tries to follow the mockup proposed.
  • Loading branch information
jorgefilipecosta authored and youknowriad committed Mar 6, 2019
1 parent 068040e commit 6ea4468
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
}
// Currently merged in core as `gutenberg_render_block_core_latest_comments`,
// expected to change soon.
if ( ! function_exists( 'render_block_core_calendar' ) ) {
require dirname( __FILE__ ) . '/../packages/block-library/src/calendar/index.php';
}
if ( ! function_exists( 'render_block_core_latest_comments' )
&& ! function_exists( 'gutenberg_render_block_core_latest_comments' ) ) {
require dirname( __FILE__ ) . '/../packages/block-library/src/latest-comments/index.php';
Expand Down
67 changes: 67 additions & 0 deletions packages/block-library/src/calendar/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* External dependencies
*/
import moment from 'moment';
import memoize from 'memize';

/**
* WordPress dependencies
*/
import {
Disabled,
ServerSideRender,
} from '@wordpress/components';
import { Component } from '@wordpress/element';
import { withSelect } from '@wordpress/data';

class CalendarEdit extends Component {
constructor() {
super( ...arguments );
this.getYearMonth = memoize(
this.getYearMonth.bind( this ),
{ maxSize: 1 }
);
this.getServerSideAttributes = memoize(
this.getServerSideAttributes.bind( this ),
{ maxSize: 1 }
);
}

getYearMonth( date ) {
if ( ! date ) {
return {};
}
const momentDate = moment( date );
return {
year: momentDate.year(),
month: momentDate.month() + 1,
};
}

getServerSideAttributes( attributes, date ) {
return {
...attributes,
...this.getYearMonth( date ),
};
}

render() {
return (
<Disabled>
<ServerSideRender
block="core/calendar"
attributes={ this.getServerSideAttributes(
this.props.attributes,
this.props.date
) }
/>
</Disabled>
);
}
}

export default withSelect( ( select ) => {
return {
date: select( 'core/editor' ).getEditedPostAttribute( 'date' ),
};
} )( CalendarEdit );
33 changes: 33 additions & 0 deletions packages/block-library/src/calendar/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import edit from './edit';

export const name = 'core/calendar';

export const settings = {
title: __( 'Calendar' ),

description: __( 'A calendar of your site’s posts.' ),

icon: 'calendar',

category: 'widgets',

keywords: [ __( 'posts' ), __( 'archive' ) ],

supports: {
align: true,
},

edit,

save() {
return null;
},
};
71 changes: 71 additions & 0 deletions packages/block-library/src/calendar/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Server-side rendering of the `core/calendar` block.
*
* @package WordPress
*/

/**
* Renders the `core/calendar` block on server.
*
* @param array $attributes The block attributes.
*
* @return string Returns the block content.
*/
function render_block_core_calendar( $attributes ) {
global $monthnum, $year, $post;
$previous_monthnum = $monthnum;
$previous_year = $year;

if ( isset( $attributes['month'] ) ) {
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
$monthnum = $attributes['month'];
}

if ( isset( $attributes['year'] ) ) {
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
$year = $attributes['year'];
}

$custom_class_name = empty( $attributes['className'] ) ? '' : ' ' . $attributes['className'];
$align_class_name = empty( $attributes['align'] ) ? '' : ' ' . "align{$attributes['align']}";

return sprintf(
'<div class="%1$s">%2$s</div>',
esc_attr( 'wp-block-calendar' . $custom_class_name . $align_class_name ),
get_calendar( true, false )
);

// phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
$monthnum = $previous_monthnum;
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
$year = $previous_year;
}

/**
* Registers the `core/calendar` block on server.
*/
function register_block_core_calendar() {
register_block_type(
'core/calendar',
array(
'attributes' => array(
'align' => array(
'type' => 'string',
),
'className' => array(
'type' => 'string',
),
'month' => array(
'type' => 'integer',
),
'year' => array(
'type' => 'integer',
),
),
'render_callback' => 'render_block_core_calendar',
)
);
}

add_action( 'init', 'register_block_core_calendar' );
37 changes: 37 additions & 0 deletions packages/block-library/src/calendar/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
.wp-block-calendar {
text-align: center;

th,
tbody td {
padding: 4px;
border: 1px solid $light-gray-500;
}

tfoot td {
border: none;
}

table {
width: 100%;
border-collapse: collapse;
font-family: $default-font;
}

table th {
font-weight: 440;
background: $light-gray-300;
}

a {
text-decoration: underline;
}

tfoot a {
color: $blue-medium-800;
}

table tbody,
table caption {
color: $dark-gray-600;
}
}
2 changes: 2 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as gallery from './gallery';
import * as archives from './archives';
import * as audio from './audio';
import * as button from './button';
import * as calendar from './calendar';
import * as categories from './categories';
import * as code from './code';
import * as columns from './columns';
Expand Down Expand Up @@ -68,6 +69,7 @@ export const registerCoreBlocks = () => {
archives,
audio,
button,
calendar,
categories,
code,
columns,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@import "./block/edit-panel/style.scss";
@import "./block/indicator/style.scss";
@import "./button/style.scss";
@import "./calendar/style.scss";
@import "./categories/style.scss";
@import "./columns/style.scss";
@import "./cover/style.scss";
Expand Down
1 change: 1 addition & 0 deletions test/integration/full-content/fixtures/core__calendar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:calendar /-->
10 changes: 10 additions & 0 deletions test/integration/full-content/fixtures/core__calendar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"clientId": "_clientId_0",
"name": "core/calendar",
"isValid": true,
"attributes": {},
"innerBlocks": [],
"originalContent": ""
}
]
18 changes: 18 additions & 0 deletions test/integration/full-content/fixtures/core__calendar.parsed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"blockName": "core/calendar",
"attrs": {},
"innerBlocks": [],
"innerHTML": "",
"innerContent": []
},
{
"blockName": null,
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n",
"innerContent": [
"\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:calendar /-->

0 comments on commit 6ea4468

Please sign in to comment.