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

Fixed quote paragraph transformations. #3802

Merged
merged 1 commit into from Dec 5, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 11 additions & 27 deletions blocks/library/quote/index.js
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { isString } from 'lodash';
import { isString, get } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -101,33 +101,17 @@ registerBlockType( 'core/quote', {
{
type: 'block',
blocks: [ 'core/paragraph' ],
transform: ( { value, citation, ...attrs } ) => {
const textElement = value[ 0 ];
if ( ! textElement ) {
return createBlock( 'core/paragraph', {
content: citation,
} );
transform: ( { value, citation } ) => {
// transforming an empty quote
if ( ( ! value || ! value.length ) && ! citation ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

you're checking ! value here which means the value could be undefined. Should we check the same when we use value.map down here, or maybe just replace it with map?

unless value is always an array and this first check is unnecessary then?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch @youknowriad, I also checked for undefined/null on the map.

return createBlock( 'core/paragraph' );
}
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 ) :
[],
} );

return [ text, quote ];
}
return createBlock( 'core/paragraph', {
content: textContent,
} );
// transforming a quote with content
return ( value || [] ).map( item => createBlock( 'core/paragraph', {
content: [ get( item, 'children.props.children', '' ) ],
} ) ).concat( citation ? createBlock( 'core/paragraph', {
content: citation,
} ) : [] );
},
},
{
Expand Down