Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify quote values #5265

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions blocks/api/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ export function matcherFromSource( sourceConfig ) {
return children( sourceConfig.selector );
case 'node':
return node( sourceConfig.selector );
case 'query':
const subMatchers = mapValues( sourceConfig.query, matcherFromSource );
case 'query': {
const subMatchers = sourceConfig.query ?
mapValues( sourceConfig.query, matcherFromSource ) :
( element ) => node()( element );

return query( sourceConfig.selector, subMatchers );
}
default:
// eslint-disable-next-line no-console
console.error( `Unknown source type "${ sourceConfig.source }"` );
Expand Down
22 changes: 3 additions & 19 deletions blocks/library/pullquote/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { map } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -18,20 +13,11 @@ import RichText from '../../rich-text';
import BlockControls from '../../block-controls';
import BlockAlignmentToolbar from '../../block-alignment-toolbar';

const toRichTextValue = value => map( value, ( subValue => subValue.children ) );
const fromRichTextValue = value => map( value, ( subValue ) => ( {
children: subValue,
} ) );
const blockAttributes = {
value: {
type: 'array',
source: 'query',
selector: 'blockquote > p',
query: {
children: {
source: 'node',
},
},
},
citation: {
type: 'array',
Expand Down Expand Up @@ -84,10 +70,10 @@ export const settings = {
<blockquote key="quote" className={ className }>
<RichText
multiline="p"
value={ toRichTextValue( value ) }
value={ value }
onChange={
( nextValue ) => setAttributes( {
value: fromRichTextValue( nextValue ),
value: nextValue,
} )
}
placeholder={ __( 'Write quote…' ) }
Expand Down Expand Up @@ -118,9 +104,7 @@ export const settings = {

return (
<blockquote className={ `align${ align }` }>
{ value && value.map( ( paragraph, i ) =>
<p key={ i }>{ paragraph.children && paragraph.children.props.children }</p>
) }
{ [ value ] /* Prevent keys warning... */ }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we should do something else here... :)

{ citation && citation.length > 0 && (
<cite>{ citation }</cite>
) }
Expand Down
30 changes: 9 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 { castArray, get, isString } from 'lodash';
import { castArray, get, isString, first } from 'lodash';
import classnames from 'classnames';

/**
Expand All @@ -20,21 +20,11 @@ import AlignmentToolbar from '../../alignment-toolbar';
import BlockControls from '../../block-controls';
import RichText from '../../rich-text';

const toRichTextValue = value => value.map( ( subValue => subValue.children ) );
const fromRichTextValue = value => value.map( ( subValue ) => ( {
children: subValue,
} ) );

const blockAttributes = {
value: {
type: 'array',
source: 'query',
selector: 'blockquote > p',
query: {
children: {
source: 'node',
},
},
default: [],
},
citation: {
Expand Down Expand Up @@ -69,7 +59,7 @@ export const settings = {
transform: ( { content } ) => {
return createBlock( 'core/quote', {
value: [
{ children: <p key="1">{ content }</p> },
<p key="1">{ content }</p>,
],
} );
},
Expand All @@ -80,7 +70,7 @@ export const settings = {
transform: ( { content } ) => {
return createBlock( 'core/quote', {
value: [
{ children: <p key="1">{ content }</p> },
<p key="1">{ content }</p>,
],
} );
},
Expand All @@ -91,7 +81,7 @@ export const settings = {
transform: ( { content } ) => {
return createBlock( 'core/quote', {
value: [
{ children: <p key="1">{ content }</p> },
<p key="1">{ content }</p>,
],
} );
},
Expand All @@ -112,7 +102,7 @@ export const settings = {
}
// transforming a quote with content
return ( value || [] ).map( item => createBlock( 'core/paragraph', {
content: [ get( item, 'children.props.children', '' ) ],
content: [ get( item, 'props.children', '' ) ],
} ) ).concat( citation ? createBlock( 'core/paragraph', {
content: citation,
} ) : [] );
Expand All @@ -130,7 +120,7 @@ export const settings = {
} );
}

const firstValue = get( value, [ 0, 'children' ] );
const firstValue = first( value );
const headingContent = castArray( isString( firstValue ) ?
firstValue :
get( firstValue, [ 'props', 'children' ], '' )
Expand Down Expand Up @@ -197,10 +187,10 @@ export const settings = {
>
<RichText
multiline="p"
value={ toRichTextValue( value ) }
value={ value }
onChange={
( nextValue ) => setAttributes( {
value: fromRichTextValue( nextValue ),
value: nextValue,
} )
}
onMerge={ mergeBlocks }
Expand Down Expand Up @@ -240,9 +230,7 @@ export const settings = {
className={ style === 2 ? 'is-large' : '' }
style={ { textAlign: align ? align : null } }
>
{ value.map( ( paragraph, i ) => (
<p key={ i }>{ paragraph.children && paragraph.children.props.children }</p>
) ) }
{ [ value ] /* Prevent keys warning... */ }
{ citation && citation.length > 0 && (
<cite>{ citation }</cite>
) }
Expand Down
9 changes: 2 additions & 7 deletions blocks/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export class RichText extends Component {
};

this.isEmpty = ! value || ! value.length;
this.savedContent = value;
}

/**
Expand Down Expand Up @@ -695,13 +696,7 @@ export class RichText extends Component {
!! this.editor &&
this.props.tagName === prevProps.tagName &&
this.props.value !== prevProps.value &&
this.props.value !== this.savedContent &&

// Comparing using isEqual is necessary especially to avoid unnecessary updateContent calls
// This fixes issues in multi richText blocks like quotes when moving the focus between
// the different editables.
! isEqual( this.props.value, prevProps.value ) &&
! isEqual( this.props.value, this.savedContent )
this.props.value !== this.savedContent
) {
this.updateContent();
}
Expand Down
12 changes: 2 additions & 10 deletions blocks/test/fixtures/core__pullquote.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,8 @@
"attributes": {
"value": [
{
"children": {
"type": "p",
"key": null,
"ref": null,
"props": {
"children": "Testing pullquote block..."
},
"_owner": null,
"_store": {}
}
"type": "p",
"children": "Testing pullquote block..."
}
],
"citation": [
Expand Down
42 changes: 10 additions & 32 deletions blocks/test/fixtures/core__pullquote__multi-paragraph.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,18 @@
"attributes": {
"value": [
{
"children": {
"type": "p",
"key": null,
"ref": null,
"props": {
"children": [
"Paragraph ",
{
"type": "strong",
"key": "_domReact71",
"ref": null,
"props": {
"children": "one"
},
"_owner": null,
"_store": {}
}
]
},
"_owner": null,
"_store": {}
}
"type": "p",
"children": [
"Paragraph ",
{
"type": "strong",
"children": "one"
}
]
},
{
"children": {
"type": "p",
"key": null,
"ref": null,
"props": {
"children": "Paragraph two"
},
"_owner": null,
"_store": {}
}
"type": "p",
"children": "Paragraph two"
}
],
"citation": [
Expand Down
12 changes: 2 additions & 10 deletions blocks/test/fixtures/core__quote__style-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,8 @@
"attributes": {
"value": [
{
"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": {}
}
"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."
}
],
"citation": [
Expand Down
12 changes: 2 additions & 10 deletions blocks/test/fixtures/core__quote__style-2.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,8 @@
"attributes": {
"value": [
{
"children": {
"type": "p",
"key": null,
"ref": null,
"props": {
"children": "There is no greater agony than bearing an untold story inside you."
},
"_owner": null,
"_store": {}
}
"type": "p",
"children": "There is no greater agony than bearing an untold story inside you."
}
],
"citation": [
Expand Down