From f9c93568f9d4bb0b31ebd853bf8a92edb5275814 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Fri, 13 Oct 2017 14:32:47 +0100 Subject: [PATCH] Block API: Simplify the source declaration (drop one nesting level) --- blocks/api/parser.js | 32 +++++++++++++------------- blocks/api/registration.js | 2 +- blocks/api/serializer.js | 2 +- blocks/library/audio/index.js | 14 ++++-------- blocks/library/button/index.js | 22 +++++++----------- blocks/library/code/index.js | 8 +++---- blocks/library/cover-image/index.js | 6 ++--- blocks/library/embed/index.js | 6 ++--- blocks/library/freeform/index.js | 6 ++--- blocks/library/gallery/index.js | 34 +++++++++++++--------------- blocks/library/heading/index.js | 14 ++++-------- blocks/library/html/index.js | 4 +--- blocks/library/image/index.js | 30 +++++++++--------------- blocks/library/list/index.js | 14 ++++-------- blocks/library/paragraph/index.js | 6 ++--- blocks/library/preformatted/index.js | 6 ++--- blocks/library/pullquote/index.js | 16 +++++-------- blocks/library/quote/index.js | 16 +++++-------- blocks/library/shortcode/index.js | 4 +--- blocks/library/table/index.js | 6 ++--- blocks/library/text-columns/index.js | 10 ++++---- blocks/library/verse/index.js | 6 ++--- blocks/library/video/index.js | 14 ++++-------- editor/modes/visual-editor/block.js | 4 ++-- editor/selectors.js | 4 ++-- 25 files changed, 112 insertions(+), 174 deletions(-) diff --git a/blocks/api/parser.js b/blocks/api/parser.js index 40c60d20aa0ce0..227b8e910b4283 100644 --- a/blocks/api/parser.js +++ b/blocks/api/parser.js @@ -55,33 +55,33 @@ export function asType( value, type ) { /** * Returns an hpq matcher given a source object * - * @param {Object} source Attribute Source object - * @return {Function} hpq Matcher + * @param {Object} sourceConfig Attribute Source object + * @return {Function} hpq Matcher */ -export function matcherFromSource( source ) { - switch ( source.type ) { +export function matcherFromSource( sourceConfig ) { + switch ( sourceConfig.source ) { case 'attribute': - return attr( source.selector, source.attribute ); + return attr( sourceConfig.selector, sourceConfig.attribute ); case 'property': - return prop( source.selector, source.property ); + return prop( sourceConfig.selector, sourceConfig.property ); case 'html': - return html( source.selector ); + return html( sourceConfig.selector ); case 'text': - return text( source.selector ); + return text( sourceConfig.selector ); case 'children': - return children( source.selector ); + return children( sourceConfig.selector ); case 'node': - return node( source.selector ); + return node( sourceConfig.selector ); case 'query': - return query( source.selector, matcherFromSource( source.source ) ); + return query( sourceConfig.selector, matcherFromSource( sourceConfig.query ) ); case 'object': - return keys( source.source ).reduce( ( memo, key ) => { - memo[ key ] = matcherFromSource( source.source[ key ] ); + return keys( sourceConfig.object ).reduce( ( memo, key ) => { + memo[ key ] = matcherFromSource( sourceConfig.object[ key ] ); return memo; }, {} ); default: // eslint-disable-next-line no-console - console.error( `Unkown source type "${ source.type }"` ); + console.error( `Unkown source type "${ sourceConfig.source }"` ); } } @@ -97,7 +97,7 @@ export function matcherFromSource( source ) { */ export function getBlockAttribute( attributeKey, attributeSchema, rawContent, commentAttributes ) { let value; - switch ( attributeSchema.source.type ) { + switch ( attributeSchema.source ) { case 'meta': break; case 'comment': @@ -105,7 +105,7 @@ export function getBlockAttribute( attributeKey, attributeSchema, rawContent, co break; default: { // Coerce value to specified type - const matcher = matcherFromSource( attributeSchema.source ); + const matcher = matcherFromSource( attributeSchema ); const rawValue = hpqParse( rawContent, matcher ); value = rawValue === undefined ? rawValue : asType( rawValue, attributeSchema.type ); break; diff --git a/blocks/api/registration.js b/blocks/api/registration.js index b20a5136ecd040..ed91cf85bd9b06 100644 --- a/blocks/api/registration.js +++ b/blocks/api/registration.js @@ -123,7 +123,7 @@ export function registerBlockType( name, settings ) { name, attributes: keys( attributes ).reduce( ( memo, attributeKey ) => { memo[ attributeKey ] = { - source: { type: 'comment' }, + source: 'comment', ...attributes[ attributeKey ], }; return memo; diff --git a/blocks/api/serializer.js b/blocks/api/serializer.js index 3e24ed916f8fbe..0bf3f9e22ca364 100644 --- a/blocks/api/serializer.js +++ b/blocks/api/serializer.js @@ -102,7 +102,7 @@ export function getCommentAttributes( allAttributes, blockType ) { } // Ignore values sources from content and post meta - if ( attributeSchema.source.type !== 'comment' ) { + if ( attributeSchema.source !== 'comment' ) { return result; } diff --git a/blocks/library/audio/index.js b/blocks/library/audio/index.js index 664b002dc975b6..2504da2da80151 100644 --- a/blocks/library/audio/index.js +++ b/blocks/library/audio/index.js @@ -31,21 +31,17 @@ registerBlockType( 'core/audio', { attributes: { src: { type: 'string', - source: { - type: 'attribute', - selector: 'audio', - attribute: 'src', - }, + source: 'attribute', + selector: 'audio', + attribute: 'src', }, align: { type: 'string', }, caption: { type: 'array', - source: { - type: 'children', - selector: 'figcaption', - }, + source: 'children', + selector: 'figcaption', }, }, diff --git a/blocks/library/button/index.js b/blocks/library/button/index.js index b3f522878d07fb..91495982d45b5b 100644 --- a/blocks/library/button/index.js +++ b/blocks/library/button/index.js @@ -29,26 +29,20 @@ registerBlockType( 'core/button', { attributes: { url: { type: 'string', - source: { - type: 'attribute', - selector: 'a', - attribute: 'href', - }, + source: 'attribute', + selector: 'a', + attribute: 'href', }, title: { type: 'string', - source: { - type: 'attribute', - selector: 'a', - attribute: 'title', - }, + source: 'attribute', + selector: 'a', + attribute: 'title', }, text: { type: 'array', - source: { - type: 'children', - selector: 'a', - }, + source: 'children', + selector: 'a', }, align: { type: 'string', diff --git a/blocks/library/code/index.js b/blocks/library/code/index.js index 1e0b34c9b918ad..a27eae3d359279 100644 --- a/blocks/library/code/index.js +++ b/blocks/library/code/index.js @@ -26,11 +26,9 @@ registerBlockType( 'core/code', { attributes: { content: { type: 'string', - source: { - type: 'property', - selector: 'code', - property: 'textContent', - }, + source: 'property', + selector: 'code', + property: 'textContent', }, }, diff --git a/blocks/library/cover-image/index.js b/blocks/library/cover-image/index.js index b4fc62a9f27a7c..845dd025db41c3 100644 --- a/blocks/library/cover-image/index.js +++ b/blocks/library/cover-image/index.js @@ -32,10 +32,8 @@ registerBlockType( 'core/cover-image', { attributes: { title: { type: 'array', - source: { - type: 'children', - selector: 'h2', - }, + source: 'children', + selector: 'h2', }, url: { type: 'string', diff --git a/blocks/library/embed/index.js b/blocks/library/embed/index.js index 1c30b64585f70f..72382b50291b97 100644 --- a/blocks/library/embed/index.js +++ b/blocks/library/embed/index.js @@ -43,10 +43,8 @@ function getEmbedBlockSettings( { title, icon, category = 'embed', transforms, k }, caption: { type: 'array', - source: { - type: 'children', - selector: 'figcaption', - }, + source: 'children', + selector: 'figcaption', default: [], }, align: { diff --git a/blocks/library/freeform/index.js b/blocks/library/freeform/index.js index 53c82abb0fb365..2bb358e4cd1293 100644 --- a/blocks/library/freeform/index.js +++ b/blocks/library/freeform/index.js @@ -20,10 +20,8 @@ registerBlockType( 'core/freeform', { attributes: { content: { type: 'string', - source: { - type: 'property', - property: 'innerHTML', - }, + source: 'property', + property: 'innerHTML', }, }, diff --git a/blocks/library/gallery/index.js b/blocks/library/gallery/index.js index 3863d4a4b9fafe..18d3a424240811 100644 --- a/blocks/library/gallery/index.js +++ b/blocks/library/gallery/index.js @@ -26,24 +26,22 @@ registerBlockType( 'core/gallery', { images: { type: 'array', default: [], - source: { - type: 'query', - selector: 'div.wp-block-gallery figure.blocks-gallery-image img', - source: { - type: 'object', - source: { - url: { - type: 'attribute', - attribute: 'src', - }, - alt: { - type: 'attribute', - attribute: 'alt', - }, - id: { - type: 'attribute', - attribute: 'data-id', - }, + source: 'query', + selector: 'div.wp-block-gallery figure.blocks-gallery-image img', + query: { + source: 'object', + object: { + url: { + source: 'attribute', + attribute: 'src', + }, + alt: { + source: 'attribute', + attribute: 'alt', + }, + id: { + source: 'attribute', + attribute: 'data-id', }, }, }, diff --git a/blocks/library/heading/index.js b/blocks/library/heading/index.js index 9f103ec6c6e69e..9b5db1e4c1a65b 100644 --- a/blocks/library/heading/index.js +++ b/blocks/library/heading/index.js @@ -32,18 +32,14 @@ registerBlockType( 'core/heading', { attributes: { content: { type: 'array', - source: { - type: 'children', - selector: 'h1,h2,h3,h4,h5,h6', - }, + source: 'children', + selector: 'h1,h2,h3,h4,h5,h6', }, nodeName: { type: 'string', - source: { - type: 'property', - selector: 'h1,h2,h3,h4,h5,h6', - property: 'nodeName', - }, + source: 'property', + selector: 'h1,h2,h3,h4,h5,h6', + property: 'nodeName', default: 'H2', }, align: { diff --git a/blocks/library/html/index.js b/blocks/library/html/index.js index 1b2a491440e8bf..9b9281d7f4980c 100644 --- a/blocks/library/html/index.js +++ b/blocks/library/html/index.js @@ -32,9 +32,7 @@ registerBlockType( 'core/html', { attributes: { content: { type: 'string', - source: { - type: 'html', - }, + source: 'html', }, }, diff --git a/blocks/library/image/index.js b/blocks/library/image/index.js index d1e17775df8790..93eb1f5bb1b281 100644 --- a/blocks/library/image/index.js +++ b/blocks/library/image/index.js @@ -24,34 +24,26 @@ registerBlockType( 'core/image', { attributes: { url: { type: 'string', - source: { - type: 'attribute', - selector: 'img', - attribute: 'src', - }, + source: 'attribute', + selector: 'img', + attribute: 'src', }, alt: { type: 'string', - source: { - type: 'attribute', - selector: 'img', - attribute: 'alt', - }, + source: 'attribute', + selector: 'img', + attribute: 'alt', }, caption: { type: 'array', - source: { - type: 'children', - selector: 'figcaption', - }, + source: 'children', + selector: 'figcaption', }, href: { type: 'string', - source: { - type: 'attribute', - selector: 'a', - attribute: 'href', - }, + source: 'attribute', + selector: 'a', + attribute: 'href', }, id: { type: 'number', diff --git a/blocks/library/list/index.js b/blocks/library/list/index.js index d1cbbd64ccbd87..a5751a799de150 100644 --- a/blocks/library/list/index.js +++ b/blocks/library/list/index.js @@ -82,19 +82,15 @@ registerBlockType( 'core/list', { attributes: { nodeName: { type: 'string', - source: { - type: 'property', - selector: 'ol,ul', - property: 'nodeName', - }, + source: 'property', + selector: 'ol,ul', + property: 'nodeName', default: 'UL', }, values: { type: 'array', - source: { - type: 'children', - selector: 'ol,ul', - }, + source: 'children', + selector: 'ol,ul', default: [], }, }, diff --git a/blocks/library/paragraph/index.js b/blocks/library/paragraph/index.js index d8c84f11f0adc6..ece0452667e808 100644 --- a/blocks/library/paragraph/index.js +++ b/blocks/library/paragraph/index.js @@ -40,10 +40,8 @@ registerBlockType( 'core/paragraph', { attributes: { content: { type: 'array', - source: { - type: 'children', - selector: 'p', - }, + source: 'children', + selector: 'p', }, align: { type: 'string', diff --git a/blocks/library/preformatted/index.js b/blocks/library/preformatted/index.js index 877236df9dab98..ae5893e5a0b15c 100644 --- a/blocks/library/preformatted/index.js +++ b/blocks/library/preformatted/index.js @@ -22,10 +22,8 @@ registerBlockType( 'core/preformatted', { attributes: { content: { type: 'array', - source: { - type: 'children', - selector: 'pre', - }, + source: 'children', + selector: 'pre', }, }, diff --git a/blocks/library/pullquote/index.js b/blocks/library/pullquote/index.js index 26a8fa3d0fad38..2a112f8bcd9e86 100644 --- a/blocks/library/pullquote/index.js +++ b/blocks/library/pullquote/index.js @@ -26,20 +26,16 @@ registerBlockType( 'core/pullquote', { attributes: { value: { type: 'array', - source: { - type: 'query', - selector: 'blockquote > p', - source: { - type: 'node', - }, + source: 'query', + selector: 'blockquote > p', + query: { + source: 'node', }, }, citation: { type: 'array', - source: { - type: 'children', - selector: 'footer', - }, + source: 'children', + selector: 'footer', }, align: { type: 'string', diff --git a/blocks/library/quote/index.js b/blocks/library/quote/index.js index b4ae9e89cad143..a031c98810d6b7 100644 --- a/blocks/library/quote/index.js +++ b/blocks/library/quote/index.js @@ -28,21 +28,17 @@ registerBlockType( 'core/quote', { attributes: { value: { type: 'array', - source: { - type: 'query', - selector: 'blockquote > p', - source: { - type: 'node', - }, + source: 'query', + selector: 'blockquote > p', + query: { + source: 'node', }, default: [], }, citation: { type: 'array', - source: { - type: 'children', - selector: 'footer', - }, + source: 'children', + selector: 'footer', }, align: { type: 'string', diff --git a/blocks/library/shortcode/index.js b/blocks/library/shortcode/index.js index e14eb954cfc38a..4a273a62ffbe9d 100644 --- a/blocks/library/shortcode/index.js +++ b/blocks/library/shortcode/index.js @@ -22,9 +22,7 @@ registerBlockType( 'core/shortcode', { attributes: { text: { type: 'string', - source: { - type: 'text', - }, + source: 'text', }, }, diff --git a/blocks/library/table/index.js b/blocks/library/table/index.js index b74b161da7f81a..861017d695fcfa 100644 --- a/blocks/library/table/index.js +++ b/blocks/library/table/index.js @@ -23,10 +23,8 @@ registerBlockType( 'core/table', { attributes: { content: { type: 'array', - source: { - type: 'children', - selector: 'table', - }, + source: 'children', + selector: 'table', default: [

diff --git a/blocks/library/text-columns/index.js b/blocks/library/text-columns/index.js index 8091fb73f80222..3843af938c9bac 100644 --- a/blocks/library/text-columns/index.js +++ b/blocks/library/text-columns/index.js @@ -31,12 +31,10 @@ registerBlockType( 'core/text-columns', { attributes: { content: { type: 'array', - source: { - type: 'query', - selector: 'p', - source: { - type: 'children', - }, + source: 'query', + selector: 'p', + query: { + source: 'children', }, default: [ [], [] ], }, diff --git a/blocks/library/verse/index.js b/blocks/library/verse/index.js index ea23025897e672..2d137cfac858c1 100644 --- a/blocks/library/verse/index.js +++ b/blocks/library/verse/index.js @@ -24,10 +24,8 @@ registerBlockType( 'core/verse', { attributes: { content: { type: 'array', - source: { - type: 'children', - selector: 'pre', - }, + source: 'children', + selector: 'pre', }, }, diff --git a/blocks/library/video/index.js b/blocks/library/video/index.js index 10da12b2b2bc9e..427054dbbabb0e 100644 --- a/blocks/library/video/index.js +++ b/blocks/library/video/index.js @@ -36,18 +36,14 @@ registerBlockType( 'core/video', { }, src: { type: 'string', - source: { - type: 'attribute', - selector: 'video', - attribute: 'src', - }, + source: 'attribute', + selector: 'video', + attribute: 'src', }, caption: { type: 'array', - source: { - type: 'children', - selector: 'figcaption', - }, + source: 'children', + selector: 'figcaption', }, }, diff --git a/editor/modes/visual-editor/block.js b/editor/modes/visual-editor/block.js index b1bbf3847efc6d..c08839dafd610b 100644 --- a/editor/modes/visual-editor/block.js +++ b/editor/modes/visual-editor/block.js @@ -145,8 +145,8 @@ class VisualEditorBlock extends Component { onChange( block.uid, attributes ); const metaAttributes = reduce( attributes, ( result, value, key ) => { - if ( type && get( type, [ 'attributes', key, 'source', 'type' ] ) === 'meta' ) { - result[ type.attributes[ key ].source.meta ] = value; + if ( type && get( type, [ 'attributes', key, 'source' ] ) === 'meta' ) { + result[ type.attributes[ key ].meta ] = value; } return result; diff --git a/editor/selectors.js b/editor/selectors.js index 5030e9300d83eb..acfa921920f650 100644 --- a/editor/selectors.js +++ b/editor/selectors.js @@ -397,8 +397,8 @@ export const getBlock = createSelector( } const metaAttributes = reduce( type.attributes, ( result, value, key ) => { - if ( value.source.type === 'meta' ) { - result[ key ] = getPostMeta( state, value.source.meta ); + if ( value.source === 'meta' ) { + result[ key ] = getPostMeta( state, value.meta ); } return result;