From bef7b68eb0269b44d71f1f4fd568801d1d20ad61 Mon Sep 17 00:00:00 2001 From: Zebulan Stanphill Date: Tue, 21 Apr 2020 17:31:24 -0500 Subject: [PATCH 1/4] RSS block: use block.json --- packages/block-library/src/rss/block.json | 45 +++++++++++++++++++++++ packages/block-library/src/rss/index.js | 8 ++-- packages/block-library/src/rss/index.php | 45 +---------------------- 3 files changed, 52 insertions(+), 46 deletions(-) create mode 100644 packages/block-library/src/rss/block.json diff --git a/packages/block-library/src/rss/block.json b/packages/block-library/src/rss/block.json new file mode 100644 index 0000000000000..8b4805cc062ca --- /dev/null +++ b/packages/block-library/src/rss/block.json @@ -0,0 +1,45 @@ +{ + "name": "core/rss", + "category": "widgets", + "attributes": { + "align": { + "type": "string", + "enum": [ "left", "center", "right", "wide", "full" ] + }, + "className": { + "type": "string" + }, + "columns": { + "type": "number", + "default": 2 + }, + "blockLayout": { + "type": "string", + "default": "list" + }, + "feedURL": { + "type": "string", + "default": "" + }, + "itemsToShow": { + "type": "number", + "default": 5 + }, + "displayExcerpt": { + "type": "boolean", + "default": false + }, + "displayAuthor": { + "type": "boolean", + "default": false + }, + "displayDate": { + "type": "boolean", + "default": false + }, + "excerptLength": { + "type": "number", + "default": 55 + } + } +} diff --git a/packages/block-library/src/rss/index.js b/packages/block-library/src/rss/index.js index 461bc6c59bff6..fc0144be2efb3 100644 --- a/packages/block-library/src/rss/index.js +++ b/packages/block-library/src/rss/index.js @@ -1,21 +1,23 @@ /** * WordPress dependencies */ -import { __ } from '@wordpress/i18n'; import { rss as icon } from '@wordpress/icons'; +import { __ } from '@wordpress/i18n'; /** * Internal dependencies */ +import metadata from './block.json'; import edit from './edit'; -export const name = 'core/rss'; +const { name } = metadata; + +export { metadata, name }; export const settings = { title: __( 'RSS' ), description: __( 'Display entries from any RSS or Atom feed.' ), icon, - category: 'widgets', keywords: [ __( 'atom' ), __( 'feed' ) ], supports: { align: true, diff --git a/packages/block-library/src/rss/index.php b/packages/block-library/src/rss/index.php index 07ddb3d007e53..2942b36398ca0 100644 --- a/packages/block-library/src/rss/index.php +++ b/packages/block-library/src/rss/index.php @@ -99,50 +99,9 @@ function render_block_core_rss( $attributes ) { * Registers the `core/rss` block on server. */ function register_block_core_rss() { - register_block_type( - 'core/rss', + register_block_type_from_metadata( + __DIR__ . '/rss', array( - 'attributes' => array( - 'align' => array( - 'type' => 'string', - 'enum' => array( 'left', 'center', 'right', 'wide', 'full' ), - ), - 'className' => array( - 'type' => 'string', - ), - 'columns' => array( - 'type' => 'number', - 'default' => 2, - ), - 'blockLayout' => array( - 'type' => 'string', - 'default' => 'list', - ), - 'feedURL' => array( - 'type' => 'string', - 'default' => '', - ), - 'itemsToShow' => array( - 'type' => 'number', - 'default' => 5, - ), - 'displayExcerpt' => array( - 'type' => 'boolean', - 'default' => false, - ), - 'displayAuthor' => array( - 'type' => 'boolean', - 'default' => false, - ), - 'displayDate' => array( - 'type' => 'boolean', - 'default' => false, - ), - 'excerptLength' => array( - 'type' => 'number', - 'default' => 55, - ), - ), 'render_callback' => 'render_block_core_rss', ) ); From 01e3f4366b16e25c44ef571f1c91777281822dd7 Mon Sep 17 00:00:00 2001 From: Zebulan Stanphill Date: Tue, 21 Apr 2020 17:38:03 -0500 Subject: [PATCH 2/4] Use single quote string for consistency. --- packages/block-library/src/rss/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/rss/index.php b/packages/block-library/src/rss/index.php index 2942b36398ca0..f98f6343c121b 100644 --- a/packages/block-library/src/rss/index.php +++ b/packages/block-library/src/rss/index.php @@ -92,7 +92,7 @@ function render_block_core_rss( $attributes ) { $class .= ' ' . $attributes['className']; } - return sprintf( "", esc_attr( $class ), $list_items ); + return sprintf( '', esc_attr( $class ), $list_items ); } /** From 75ebb99d8a256f5154ada930cc7ad2c2ea5ca110 Mon Sep 17 00:00:00 2001 From: Zebulan Stanphill Date: Tue, 21 Apr 2020 17:39:51 -0500 Subject: [PATCH 3/4] RSS block: refactor edit component to use hooks. --- packages/block-library/src/rss/edit.js | 253 ++++++++++++------------- 1 file changed, 116 insertions(+), 137 deletions(-) diff --git a/packages/block-library/src/rss/edit.js b/packages/block-library/src/rss/edit.js index 5c0a2ac6c7495..77e8850a4b9cf 100644 --- a/packages/block-library/src/rss/edit.js +++ b/packages/block-library/src/rss/edit.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { Component } from '@wordpress/element'; +import { BlockControls, InspectorControls } from '@wordpress/block-editor'; import { Button, Disabled, @@ -12,169 +12,148 @@ import { ToggleControl, ToolbarGroup, } from '@wordpress/components'; +import { useState } from '@wordpress/element'; +import { grid, list, pencil, rss } from '@wordpress/icons'; import { __ } from '@wordpress/i18n'; -import { BlockControls, InspectorControls } from '@wordpress/block-editor'; import ServerSideRender from '@wordpress/server-side-render'; -import { rss, pencil, grid, list } from '@wordpress/icons'; const DEFAULT_MIN_ITEMS = 1; const DEFAULT_MAX_ITEMS = 10; -class RSSEdit extends Component { - constructor() { - super( ...arguments ); +export default function RSSEdit( { attributes, setAttributes } ) { + const [ isEditing, setIsEditing ] = useState( ! attributes.feedURL ); - this.state = { - editing: ! this.props.attributes.feedURL, - }; + const { + blockLayout, + columns, + displayAuthor, + displayDate, + displayExcerpt, + excerptLength, + feedURL, + itemsToShow, + } = attributes; - this.toggleAttribute = this.toggleAttribute.bind( this ); - this.onSubmitURL = this.onSubmitURL.bind( this ); - } - - toggleAttribute( propName ) { + function toggleAttribute( propName ) { return () => { - const value = this.props.attributes[ propName ]; - const { setAttributes } = this.props; + const value = attributes[ propName ]; setAttributes( { [ propName ]: ! value } ); }; } - onSubmitURL( event ) { + function onSubmitURL( event ) { event.preventDefault(); - const { feedURL } = this.props.attributes; if ( feedURL ) { - this.setState( { editing: false } ); + setIsEditing( false ); } } - render() { - const { - blockLayout, - columns, - displayAuthor, - displayExcerpt, - displayDate, - excerptLength, - feedURL, - itemsToShow, - } = this.props.attributes; - const { setAttributes } = this.props; + if ( isEditing ) { + return ( + +
+ + setAttributes( { feedURL: value } ) + } + className="blocks-rss__placeholder-input" + /> + + +
+ ); + } + + const toolbarControls = [ + { + icon: pencil, + title: __( 'Edit RSS URL' ), + onClick: () => setIsEditing( true ), + }, + { + icon: list, + title: __( 'List view' ), + onClick: () => setAttributes( { blockLayout: 'list' } ), + isActive: blockLayout === 'list', + }, + { + icon: grid, + title: __( 'Grid view' ), + onClick: () => setAttributes( { blockLayout: 'grid' } ), + isActive: blockLayout === 'grid', + }, + ]; - if ( this.state.editing ) { - return ( - -
- + + + + + + + setAttributes( { itemsToShow: value } ) + } + min={ DEFAULT_MIN_ITEMS } + max={ DEFAULT_MAX_ITEMS } + required + /> + + + + { displayExcerpt && ( + - setAttributes( { feedURL: value } ) + setAttributes( { excerptLength: value } ) } - className="blocks-rss__placeholder-input" + min={ 10 } + max={ 100 } + required /> - - -
- ); - } - - const toolbarControls = [ - { - icon: pencil, - title: __( 'Edit RSS URL' ), - onClick: () => this.setState( { editing: true } ), - }, - { - icon: list, - title: __( 'List view' ), - onClick: () => setAttributes( { blockLayout: 'list' } ), - isActive: blockLayout === 'list', - }, - { - icon: grid, - title: __( 'Grid view' ), - onClick: () => setAttributes( { blockLayout: 'grid' } ), - isActive: blockLayout === 'grid', - }, - ]; - - return ( - <> - - - - - + ) } + { blockLayout === 'grid' && ( - setAttributes( { itemsToShow: value } ) + setAttributes( { columns: value } ) } - min={ DEFAULT_MIN_ITEMS } - max={ DEFAULT_MAX_ITEMS } + min={ 2 } + max={ 6 } required /> - - - - { displayExcerpt && ( - - setAttributes( { excerptLength: value } ) - } - min={ 10 } - max={ 100 } - required - /> - ) } - { blockLayout === 'grid' && ( - - setAttributes( { columns: value } ) - } - min={ 2 } - max={ 6 } - required - /> - ) } - - - - - - - ); - } + ) } + + + + + + + ); } - -export default RSSEdit; From fb3af1ed79b0f182f2493d0832c4b0ed96738062 Mon Sep 17 00:00:00 2001 From: Zebulan Stanphill Date: Tue, 21 Apr 2020 17:41:34 -0500 Subject: [PATCH 4/4] RSS block: update editor classes for consistency, --- packages/block-library/src/rss/edit.js | 4 ++-- packages/block-library/src/rss/editor.scss | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/rss/edit.js b/packages/block-library/src/rss/edit.js index 77e8850a4b9cf..9e8ebe788d974 100644 --- a/packages/block-library/src/rss/edit.js +++ b/packages/block-library/src/rss/edit.js @@ -55,7 +55,7 @@ export default function RSSEdit( { attributes, setAttributes } ) {
setAttributes( { feedURL: value } ) } - className="blocks-rss__placeholder-input" + className="wp-block-rss__placeholder-input" />