Skip to content

Commit

Permalink
Block API: Simplify the source declaration (drop one nesting level)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Oct 13, 2017
1 parent c6b430e commit f9c9356
Show file tree
Hide file tree
Showing 25 changed files with 112 additions and 174 deletions.
32 changes: 16 additions & 16 deletions blocks/api/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }"` );
}
}

Expand All @@ -97,15 +97,15 @@ 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':
value = commentAttributes ? commentAttributes[ attributeKey ] : undefined;
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;
Expand Down
2 changes: 1 addition & 1 deletion blocks/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion blocks/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
14 changes: 5 additions & 9 deletions blocks/library/audio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
},

Expand Down
22 changes: 8 additions & 14 deletions blocks/library/button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 3 additions & 5 deletions blocks/library/code/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ registerBlockType( 'core/code', {
attributes: {
content: {
type: 'string',
source: {
type: 'property',
selector: 'code',
property: 'textContent',
},
source: 'property',
selector: 'code',
property: 'textContent',
},
},

Expand Down
6 changes: 2 additions & 4 deletions blocks/library/cover-image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ registerBlockType( 'core/cover-image', {
attributes: {
title: {
type: 'array',
source: {
type: 'children',
selector: 'h2',
},
source: 'children',
selector: 'h2',
},
url: {
type: 'string',
Expand Down
6 changes: 2 additions & 4 deletions blocks/library/embed/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
6 changes: 2 additions & 4 deletions blocks/library/freeform/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ registerBlockType( 'core/freeform', {
attributes: {
content: {
type: 'string',
source: {
type: 'property',
property: 'innerHTML',
},
source: 'property',
property: 'innerHTML',
},
},

Expand Down
34 changes: 16 additions & 18 deletions blocks/library/gallery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
},
},
Expand Down
14 changes: 5 additions & 9 deletions blocks/library/heading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
4 changes: 1 addition & 3 deletions blocks/library/html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ registerBlockType( 'core/html', {
attributes: {
content: {
type: 'string',
source: {
type: 'html',
},
source: 'html',
},
},

Expand Down
30 changes: 11 additions & 19 deletions blocks/library/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
14 changes: 5 additions & 9 deletions blocks/library/list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
},
},
Expand Down
6 changes: 2 additions & 4 deletions blocks/library/paragraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ registerBlockType( 'core/paragraph', {
attributes: {
content: {
type: 'array',
source: {
type: 'children',
selector: 'p',
},
source: 'children',
selector: 'p',
},
align: {
type: 'string',
Expand Down
6 changes: 2 additions & 4 deletions blocks/library/preformatted/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ registerBlockType( 'core/preformatted', {
attributes: {
content: {
type: 'array',
source: {
type: 'children',
selector: 'pre',
},
source: 'children',
selector: 'pre',
},
},

Expand Down
16 changes: 6 additions & 10 deletions blocks/library/pullquote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Loading

0 comments on commit f9c9356

Please sign in to comment.