From 74151255e5c3b7c6673efd6170ce7c9f7f066e49 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Fri, 5 May 2017 16:53:18 -0400 Subject: [PATCH 1/3] Default text block to empty array Same value type as we assume from children() matcher --- blocks/library/text/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blocks/library/text/index.js b/blocks/library/text/index.js index d788e81f2f24b8..8fb10caeba9692 100644 --- a/blocks/library/text/index.js +++ b/blocks/library/text/index.js @@ -18,7 +18,7 @@ registerBlock( 'core/text', { }, defaultAttributes: { - content:

+ content: [] }, merge( attributes, attributesToMerge ) { From b2f2361f4e8110729e7be749c1cb974f64a77e95 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Fri, 5 May 2017 16:54:26 -0400 Subject: [PATCH 2/3] Merge children values assumed as arrays --- blocks/library/heading/index.js | 59 ++++++++++++++------------------- blocks/library/quote/index.js | 5 ++- blocks/library/text/index.js | 5 ++- 3 files changed, 32 insertions(+), 37 deletions(-) diff --git a/blocks/library/heading/index.js b/blocks/library/heading/index.js index 0b121478265eac..53a6459aaebe81 100644 --- a/blocks/library/heading/index.js +++ b/blocks/library/heading/index.js @@ -37,37 +37,26 @@ registerBlock( 'core/heading', { type: 'block', blocks: [ 'core/text' ], transform: ( { content, ...attrs } ) => { - if ( Array.isArray( content ) ) { - const heading = { - blockType: 'core/heading', - attributes: { - nodeName: 'H2', - content: content[ 0 ].props.children - } - }; - const blocks = [ heading ]; + if ( ! content ) { + return wp.blocks.createBlock( 'core/heading' ); + } - const remainingContent = content.slice( 1 ); - if ( remainingContent.length ) { - const text = { - blockType: 'core/text', - attributes: { - ...attrs, - content: remainingContent - } - }; - blocks.push( text ); - } + const [ paragraph, ...remainingContent ] = content; + const heading = wp.blocks.createBlock( 'core/heading', { + content: paragraph.props.children + } ); - return blocks; + if ( ! remainingContent.length ) { + return heading; } - return { - blockType: 'core/heading', - attributes: { - nodeName: 'H2', - content - } - }; + + return [ + heading, + wp.blocks.createBlock( 'core/text', { + ...attrs, + content: remainingContent + } ) + ]; } } ], @@ -76,12 +65,9 @@ registerBlock( 'core/heading', { type: 'block', blocks: [ 'core/text' ], transform: ( { content } ) => { - return { - blockType: 'core/text', - attributes: { - content - } - }; + return wp.blocks.createBlock( 'core/text', { + content + } ); } } ] @@ -89,7 +75,10 @@ registerBlock( 'core/heading', { merge( attributes, attributesToMerge ) { return { - content: wp.element.concatChildren( attributes.content, attributesToMerge.content ) + content: [ + ...attributes.content, + ...attributesToMerge.content + ] }; }, diff --git a/blocks/library/quote/index.js b/blocks/library/quote/index.js index e983c5af3a791c..93d8446248b758 100644 --- a/blocks/library/quote/index.js +++ b/blocks/library/quote/index.js @@ -62,7 +62,10 @@ registerBlock( 'core/quote', { return { blockType: 'core/text', attributes: { - content: wp.element.concatChildren( value, citation ) + content: [ + ...( value || [] ), + ...( citation || [] ) + ] } }; } diff --git a/blocks/library/text/index.js b/blocks/library/text/index.js index 8fb10caeba9692..f6fa88f65ccdee 100644 --- a/blocks/library/text/index.js +++ b/blocks/library/text/index.js @@ -23,7 +23,10 @@ registerBlock( 'core/text', { merge( attributes, attributesToMerge ) { return { - content: wp.element.concatChildren( attributes.content, attributesToMerge.content ) + content: [ + ...attributes.content, + ...attributesToMerge.content + ] }; }, From c8ddf594f787416ad3ecf0ed027acfb25dab08e3 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Fri, 5 May 2017 16:54:36 -0400 Subject: [PATCH 3/3] Remove unused concatChildren elements utility --- element/index.js | 22 ---------------------- element/test/index.js | 22 +--------------------- 2 files changed, 1 insertion(+), 43 deletions(-) diff --git a/element/index.js b/element/index.js index 8c8b2762a88755..dfd6ace3df8622 100644 --- a/element/index.js +++ b/element/index.js @@ -69,25 +69,3 @@ export function renderToString( element ) { return renderToStaticMarkup( element ); } - -/** - * Concatenate two or more React children objects - * - * @param {...?Object} childrens Set of children to concatenate - * @return {Array} The concatenated value - */ -export function concatChildren( ...childrens ) { - return childrens.reduce( ( memo, children, i ) => { - Children.forEach( children, ( child, j ) => { - if ( child && 'string' !== typeof child ) { - child = cloneElement( child, { - key: [ i, j ].join() - } ); - } - - memo.push( child ); - } ); - - return memo; - }, [] ); -} diff --git a/element/test/index.js b/element/test/index.js index 842ac6a0465e16..333acd3ffb9ee6 100644 --- a/element/test/index.js +++ b/element/test/index.js @@ -6,7 +6,7 @@ import { expect } from 'chai'; /** * Internal dependencies */ -import { createElement, renderToString, concatChildren } from '../'; +import { createElement, renderToString } from '../'; describe( 'element', () => { describe( 'renderToString', () => { @@ -35,24 +35,4 @@ describe( 'element', () => { ) ).to.equal( 'Courgette' ); } ); } ); - - describe( 'concatChildren', () => { - it( 'should return an empty array for undefined children', () => { - expect( concatChildren() ).to.eql( [] ); - } ); - - it( 'should concat the string arrays', () => { - expect( concatChildren( [ 'a' ], 'b' ) ).to.eql( [ 'a', 'b' ] ); - } ); - - it( 'should concat the object arrays and rewrite keys', () => { - const concat = concatChildren( - [ createElement( 'strong', {}, 'Courgette' ) ], - createElement( 'strong', {}, 'Concombre' ) - ); - expect( concat.length ).to.equal( 2 ); - expect( concat[ 0 ].key ).to.equal( '0,0' ); - expect( concat[ 1 ].key ).to.equal( '1,0' ); - } ); - } ); } );