-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
@@ -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 ) => { | ||
// 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( [] ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is |
||
} else { | ||
// No line breaks form prettier. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.' ), | ||
|
@@ -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> } ) ), | ||
} ); | ||
}, | ||
}, | ||
|
There was a problem hiding this comment.
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 :)