Skip to content

Commit

Permalink
Parser: Only support query with object nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Oct 23, 2017
1 parent 18c5e05 commit a4c2982
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 74 deletions.
8 changes: 4 additions & 4 deletions blocks/api/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ export function matcherFromSource( sourceConfig ) {
case 'node':
return node( sourceConfig.selector );
case 'query':
return query( sourceConfig.selector, matcherFromSource( sourceConfig.query ) );
case 'object':
return keys( sourceConfig.object ).reduce( ( memo, key ) => {
memo[ key ] = matcherFromSource( sourceConfig.object[ key ] );
const subMatchers = keys( sourceConfig.query ).reduce( ( memo, key ) => {
memo[ key ] = matcherFromSource( sourceConfig.query[ key ] );
return memo;
}, {} );

return query( sourceConfig.selector, subMatchers );
default:
// eslint-disable-next-line no-console
console.error( `Unkown source type "${ sourceConfig.source }"` );
Expand Down
25 changes: 11 additions & 14 deletions blocks/library/gallery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,17 @@ registerBlockType( 'core/gallery', {
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',
},
url: {
source: 'attribute',
attribute: 'src',
},
alt: {
source: 'attribute',
attribute: 'alt',
},
id: {
source: 'attribute',
attribute: 'data-id',
},
},
},
Expand Down
17 changes: 13 additions & 4 deletions blocks/library/pullquote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import BlockAlignmentToolbar from '../../block-alignment-toolbar';
import InspectorControls from '../../inspector-controls';
import BlockDescription from '../../block-description';

const toEditableValue = value => value.map( ( subValue => subValue.children ) );
const fromEditableValue = value => value.map( ( subValue ) => ( {
children: subValue,
} ) );

registerBlockType( 'core/pullquote', {

title: __( 'Pullquote' ),
Expand All @@ -29,7 +34,9 @@ registerBlockType( 'core/pullquote', {
source: 'query',
selector: 'blockquote > p',
query: {
source: 'node',
children: {
source: 'node',
},
},
},
citation: {
Expand Down Expand Up @@ -73,10 +80,10 @@ registerBlockType( 'core/pullquote', {
<blockquote key="quote" className={ className }>
<Editable
multiline="p"
value={ value }
value={ toEditableValue( value ) }
onChange={
( nextValue ) => setAttributes( {
value: nextValue,
value: fromEditableValue( nextValue ),
} )
}
placeholder={ __( 'Write quote…' ) }
Expand Down Expand Up @@ -107,7 +114,9 @@ registerBlockType( 'core/pullquote', {

return (
<blockquote className={ `align${ align }` }>
{ value && value.map( ( paragraph, i ) => <p key={ i }>{ paragraph.props.children }</p> ) }
{ value && value.map( ( paragraph, i ) =>
<p key={ i }>{ paragraph.children && paragraph.children.props.children }</p>
) }
{ citation && citation.length > 0 && (
<footer>{ citation }</footer>
) }
Expand Down
59 changes: 38 additions & 21 deletions blocks/library/quote/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { isString, isObject } from 'lodash';
import { isString } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -20,6 +20,11 @@ import Editable from '../../editable';
import InspectorControls from '../../inspector-controls';
import BlockDescription from '../../block-description';

const toEditableValue = value => value.map( ( subValue => subValue.children ) );
const fromEditableValue = value => value.map( ( subValue ) => ( {
children: subValue,
} ) );

registerBlockType( 'core/quote', {
title: __( 'Quote' ),
icon: 'format-quote',
Expand All @@ -31,7 +36,9 @@ registerBlockType( 'core/quote', {
source: 'query',
selector: 'blockquote > p',
query: {
source: 'node',
children: {
source: 'node',
},
},
default: [],
},
Expand All @@ -57,7 +64,7 @@ registerBlockType( 'core/quote', {
transform: ( { content } ) => {
return createBlock( 'core/quote', {
value: [
<p key="1">{ content }</p>,
{ children: <p key="1">{ content }</p> },
],
} );
},
Expand All @@ -68,7 +75,7 @@ registerBlockType( 'core/quote', {
transform: ( { content } ) => {
return createBlock( 'core/quote', {
value: [
<p key="1">{ content }</p>,
{ children: <p key="1">{ content }</p> },
],
} );
},
Expand All @@ -79,7 +86,7 @@ registerBlockType( 'core/quote', {
transform: ( { content } ) => {
return createBlock( 'core/quote', {
value: [
<p key="1">{ content }</p>,
{ children: <p key="1">{ content }</p> },
],
} );
},
Expand All @@ -100,15 +107,19 @@ registerBlockType( 'core/quote', {
content: citation,
} );
}
const textContent = isString( textElement ) ? textElement : textElement.props.children;
const textContent = isString( textElement.children )
? textElement.children
: textElement.children.props.children;
if ( Array.isArray( value ) || citation ) {
const text = createBlock( 'core/paragraph', {
content: textContent,
} );
const quote = createBlock( 'core/quote', {
...attrs,
citation,
value: Array.isArray( value ) ? value.slice( 1 ) : '',
value: Array.isArray( value )
? value.slice( 1 )
: [],
} );

return [ text, quote ];
Expand All @@ -122,25 +133,31 @@ registerBlockType( 'core/quote', {
type: 'block',
blocks: [ 'core/heading' ],
transform: ( { value, citation, ...attrs } ) => {
const isMultiParagraph = Array.isArray( value ) && isObject( value[ 0 ] ) && value[ 0 ].type === 'p';
const headingElement = isMultiParagraph ? value[ 0 ] : value;
const headingContent = isObject( headingElement ) && value[ 0 ].type === 'p'
? headingElement.props.children
: headingElement;
if ( isMultiParagraph || citation ) {
const heading = createBlock( 'core/heading', {
content: headingContent,
const textElement = value[ 0 ];
if ( ! textElement ) {
return createBlock( 'core/heading', {
content: citation,
} );
}
const textContent = isString( textElement.children )
? textElement.children
: textElement.children.props.children;
if ( Array.isArray( value ) || citation ) {
const text = createBlock( 'core/heading', {
content: textContent,
} );
const quote = createBlock( 'core/quote', {
...attrs,
citation,
value: Array.isArray( value ) ? value.slice( 1 ) : '',
value: Array.isArray( value )
? value.slice( 1 )
: [],
} );

return [ heading, quote ];
return [ text, quote ];
}
return createBlock( 'core/heading', {
content: headingContent,
content: textContent,
} );
},
},
Expand Down Expand Up @@ -183,10 +200,10 @@ registerBlockType( 'core/quote', {
>
<Editable
multiline="p"
value={ value }
value={ toEditableValue( value ) }
onChange={
( nextValue ) => setAttributes( {
value: nextValue,
value: fromEditableValue( nextValue ),
} )
}
focus={ focusedEditable === 'value' ? focus : null }
Expand Down Expand Up @@ -222,7 +239,7 @@ registerBlockType( 'core/quote', {
style={ { textAlign: align ? align : null } }
>
{ value.map( ( paragraph, i ) => (
<p key={ i }>{ paragraph.props.children }</p>
<p key={ i }>{ paragraph.children && paragraph.children.props.children }</p>
) ) }
{ citation && citation.length > 0 && (
<footer>{ citation }</footer>
Expand Down
10 changes: 6 additions & 4 deletions blocks/library/text-columns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ registerBlockType( 'core/text-columns', {
source: 'query',
selector: 'p',
query: {
source: 'children',
children: {
source: 'children',
},
},
default: [ [], [] ],
},
Expand Down Expand Up @@ -86,12 +88,12 @@ registerBlockType( 'core/text-columns', {
<div className="wp-block-column" key={ `column-${ index }` }>
<Editable
tagName="p"
value={ content && content[ index ] }
value={ content && content[ index ].children }
onChange={ ( nextContent ) => {
setAttributes( {
content: [
...content.slice( 0, index ),
nextContent,
{ children: nextContent },
...content.slice( index + 1 ),
],
} );
Expand All @@ -112,7 +114,7 @@ registerBlockType( 'core/text-columns', {
<section className={ `align${ width } columns-${ columns }` }>
{ times( columns, ( index ) =>
<div className="wp-block-column" key={ `column-${ index }` }>
<p>{ content && content[ index ] }</p>
<p>{ content && content[ index ].children }</p>
</div>
) }
</section>
Expand Down
12 changes: 10 additions & 2 deletions blocks/test/fixtures/core__pullquote.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@
"attributes": {
"value": [
{
"type": "p",
"children": "Testing pullquote block..."
"children": {
"type": "p",
"key": null,
"ref": null,
"props": {
"children": "Testing pullquote block..."
},
"_owner": null,
"_store": {}
}
}
],
"citation": [
Expand Down
42 changes: 32 additions & 10 deletions blocks/test/fixtures/core__pullquote__multi-paragraph.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,40 @@
"attributes": {
"value": [
{
"type": "p",
"children": [
"Paragraph ",
{
"type": "strong",
"children": "one"
}
]
"children": {
"type": "p",
"key": null,
"ref": null,
"props": {
"children": [
"Paragraph ",
{
"type": "strong",
"key": "_domReact68",
"ref": null,
"props": {
"children": "one"
},
"_owner": null,
"_store": {}
}
]
},
"_owner": null,
"_store": {}
}
},
{
"type": "p",
"children": "Paragraph two"
"children": {
"type": "p",
"key": null,
"ref": null,
"props": {
"children": "Paragraph two"
},
"_owner": null,
"_store": {}
}
}
],
"citation": [
Expand Down
12 changes: 10 additions & 2 deletions blocks/test/fixtures/core__quote__style-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@
"attributes": {
"value": [
{
"type": "p",
"children": "The editor will endeavour to create a new page and post building experience that makes writing rich posts effortless, and has “blocks” to make it easy what today might take shortcodes, custom HTML, or “mystery meat” embed discovery."
"children": {
"type": "p",
"key": null,
"ref": null,
"props": {
"children": "The editor will endeavour to create a new page and post building experience that makes writing rich posts effortless, and has “blocks” to make it easy what today might take shortcodes, custom HTML, or “mystery meat” embed discovery."
},
"_owner": null,
"_store": {}
}
}
],
"citation": [
Expand Down
12 changes: 10 additions & 2 deletions blocks/test/fixtures/core__quote__style-2.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@
"attributes": {
"value": [
{
"type": "p",
"children": "There is no greater agony than bearing an untold story inside you."
"children": {
"type": "p",
"key": null,
"ref": null,
"props": {
"children": "There is no greater agony than bearing an untold story inside you."
},
"_owner": null,
"_store": {}
}
}
],
"citation": [
Expand Down
Loading

0 comments on commit a4c2982

Please sign in to comment.