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

Standardize on children value as array (step 1) #689

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
59 changes: 24 additions & 35 deletions blocks/library/heading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
} )
];
}
}
],
Expand All @@ -76,20 +65,20 @@ registerBlock( 'core/heading', {
type: 'block',
blocks: [ 'core/text' ],
transform: ( { content } ) => {
return {
blockType: 'core/text',
attributes: {
content
}
};
return wp.blocks.createBlock( 'core/text', {
content
} );
}
}
]
},

merge( attributes, attributesToMerge ) {
return {
content: wp.element.concatChildren( attributes.content, attributesToMerge.content )
content: [
...attributes.content,
...attributesToMerge.content
]
};
},

Expand Down
5 changes: 4 additions & 1 deletion blocks/library/quote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ registerBlock( 'core/quote', {
return {
blockType: 'core/text',
attributes: {
content: wp.element.concatChildren( value, citation )
content: [
...( value || [] ),
...( citation || [] )
]
}
};
}
Expand Down
7 changes: 5 additions & 2 deletions blocks/library/text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ registerBlock( 'core/text', {
},

defaultAttributes: {
content: <p />
content: []
},

merge( attributes, attributesToMerge ) {
return {
content: wp.element.concatChildren( attributes.content, attributesToMerge.content )
content: [
...attributes.content,
...attributesToMerge.content
]
};
},

Expand Down
22 changes: 0 additions & 22 deletions element/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}, [] );
}
22 changes: 1 addition & 21 deletions element/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { expect } from 'chai';
/**
* Internal dependencies
*/
import { createElement, renderToString, concatChildren } from '../';
import { createElement, renderToString } from '../';

describe( 'element', () => {
describe( 'renderToString', () => {
Expand Down Expand Up @@ -35,24 +35,4 @@ describe( 'element', () => {
) ).to.equal( '<strong>Courgette</strong>' );
} );
} );

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' );
} );
} );
} );