-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The first version of the calendar block. The design tries to follow the mockup proposed.
- Loading branch information
1 parent
baa90ac
commit 9e31eb5
Showing
11 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!-- wp:calendar /--> |
10 changes: 10 additions & 0 deletions
10
test/integration/full-content/fixtures/core__calendar.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
test/integration/full-content/fixtures/core__calendar.parsed.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
test/integration/full-content/fixtures/core__calendar.serialized.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!-- wp:calendar /--> |