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

Fix list to paragraph transform #4460

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
55 changes: 46 additions & 9 deletions blocks/library/list/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { find, compact, get, initial, last, isEmpty } from 'lodash';
import { find, get, last, isEmpty, castArray } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -17,6 +17,47 @@ import { registerBlockType, createBlock } from '../../api';
import Editable from '../../editable';
import BlockControls from '../../block-controls';

/**
* Converts an array of list items to an array of content.
*
* @param {Array} items Array of React list items.
* @return {Array} Array of Editable value arrays.
*/
function listItemsToArray( items ) {
return items.reduce( ( acc, item ) => {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not totally certain but it looks like we may be able to use flatMap here and it has some performance advantages according to @aduth test cases. It may also simplify the code :)

// Ignore spacing between list items.
if ( typeof item === 'string' ) {
return acc;
}

const listContent = castArray( get( item, 'props.children', [] ) );

listContent.forEach( ( content, index ) => {
if ( content.type === 'ol' || content.type === 'ul' ) {
acc.push( ...listItemsToArray( castArray( get( content, 'props.children', [] ) ) ) );
acc.push( [] );
Copy link
Member

Choose a reason for hiding this comment

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

Is acc.push( [] ); required to have line breaks? maybe we specify in a comment the reason it is needed.

} else {
// No line breaks form prettier.
Copy link
Member

Choose a reason for hiding this comment

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

What does this comment mean? Should it be "from Prettier"? 🤷‍♂️

if ( ! /\S/.test( content ) ) {
return;
}

if ( index === 0 ) {
acc.push( [] );
}

last( acc ).push( content );
}
} );

if ( last( acc ).length === 0 ) {
acc.splice( -1, 1 );
}

return acc;
}, [] );
}

registerBlockType( 'core/list', {
title: __( 'List' ),
description: __( 'List. Numbered or bulleted.' ),
Expand Down Expand Up @@ -104,20 +145,16 @@ registerBlockType( 'core/list', {
type: 'block',
blocks: [ 'core/paragraph' ],
transform: ( { values } ) =>
compact( values.map( ( value ) => get( value, 'props.children', null ) ) )
.map( ( content ) => createBlock( 'core/paragraph', {
content: [ content ],
} ) ),
listItemsToArray( values )
.map( ( content ) => createBlock( 'core/paragraph', { content } ) ),
},
{
type: 'block',
blocks: [ 'core/quote' ],
transform: ( { values } ) => {
return createBlock( 'core/quote', {
value: compact( ( values.length === 1 ? values : initial( values ) )
.map( ( value ) => get( value, 'props.children', null ) ) )
.map( ( children ) => ( { children: <p>{ children }</p> } ) ),
citation: ( values.length === 1 ? undefined : [ get( last( values ), 'props.children' ) ] ),
value: listItemsToArray( values )
.map( ( content ) => ( { children: <p>{ content }</p> } ) ),
} );
},
},
Expand Down