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

Use createBlock helper in place of manual block creation #690

Merged
merged 3 commits into from
May 10, 2017
Merged
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
2 changes: 1 addition & 1 deletion blocks/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function switchToBlockType( block, blockType ) {
return {
// The first transformed block whose type matches the "destination"
// type gets to keep the existing block's UID.
uid: index === firstSwitchedBlock ? block.uid : uuid(),
uid: index === firstSwitchedBlock ? block.uid : result.uid,
blockType: result.blockType,
attributes: result.attributes
};
Expand Down
69 changes: 24 additions & 45 deletions blocks/api/test/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,9 @@ describe( 'block factory', () => {
from: [ {
blocks: [ 'core/text-block' ],
transform: ( { value } ) => {
return {
blockType: 'core/updated-text-block',
attributes: {
value: 'chicken ' + value
}
};
return createBlock( 'core/updated-text-block', {
value: 'chicken ' + value
} );
}
} ]
}
Expand Down Expand Up @@ -82,12 +79,9 @@ describe( 'block factory', () => {
to: [ {
blocks: [ 'core/updated-text-block' ],
transform: ( { value } ) => {
return {
blockType: 'core/updated-text-block',
attributes: {
value: 'chicken ' + value
}
};
return createBlock( 'core/updated-text-block', {
value: 'chicken ' + value
} );
}
} ]
}
Expand Down Expand Up @@ -214,12 +208,10 @@ describe( 'block factory', () => {
blocks: [ 'core/text-block' ],
transform: ( { value } ) => {
return [
createBlock( 'core/updated-text-block', {
value: 'chicken ' + value
} ),
{
blockType: 'core/updated-text-block',
attributes: {
value: 'chicken ' + value
}
}, {
attributes: {
value: 'smoked ' + value
}
Expand Down Expand Up @@ -251,12 +243,9 @@ describe( 'block factory', () => {
to: [ {
blocks: [ 'core/updated-text-block' ],
transform: ( { value } ) => {
return {
blockType: 'core/text-block',
attributes: {
value: 'chicken ' + value
}
};
return createBlock( 'core/text-block', {
value: 'chicken ' + value
} );
}
} ]
}
Expand All @@ -283,17 +272,12 @@ describe( 'block factory', () => {
blocks: [ 'core/updated-text-block' ],
transform: ( { value } ) => {
return [
{
blockType: 'core/text-block',
attributes: {
value: 'chicken ' + value
}
}, {
blockType: 'core/text-block',
attributes: {
value: 'smoked ' + value
}
}
createBlock( 'core/text-block', {
value: 'chicken ' + value
} ),
createBlock( 'core/text-block', {
value: 'smoked ' + value
} )
];
}
} ]
Expand Down Expand Up @@ -321,17 +305,12 @@ describe( 'block factory', () => {
blocks: [ 'core/updated-text-block' ],
transform: ( { value } ) => {
return [
{
blockType: 'core/text-block',
attributes: {
value: 'chicken ' + value
}
}, {
blockType: 'core/updated-text-block',
attributes: {
value: 'smoked ' + value
}
}
createBlock( 'core/text-block', {
value: 'chicken ' + value
} ),
createBlock( 'core/updated-text-block', {
value: 'smoked ' + value
} )
];
}
} ]
Expand Down
42 changes: 14 additions & 28 deletions blocks/library/heading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import './style.scss';
import { registerBlock, query } from '../../api';
import { registerBlock, createBlock, query } from '../../api';
import Editable from '../../editable';

const { children, prop } = query;
Expand Down Expand Up @@ -38,36 +38,25 @@ registerBlock( 'core/heading', {
blocks: [ 'core/text' ],
transform: ( { content, ...attrs } ) => {
if ( Array.isArray( content ) ) {
const heading = {
blockType: 'core/heading',
attributes: {
nodeName: 'H2',
content: content[ 0 ].props.children
}
};
const heading = createBlock( 'core/heading', {
content: content[ 0 ].props.children
} );
const blocks = [ heading ];

const remainingContent = content.slice( 1 );
if ( remainingContent.length ) {
const text = {
blockType: 'core/text',
attributes: {
...attrs,
content: remainingContent
}
};
const text = createBlock( 'core/text', {
...attrs,
content: remainingContent
} );
blocks.push( text );
}

return blocks;
}
return {
blockType: 'core/heading',
attributes: {
nodeName: 'H2',
content
}
};
return createBlock( 'core/heading', {
content
} );
}
}
],
Expand All @@ -76,12 +65,9 @@ registerBlock( 'core/heading', {
type: 'block',
blocks: [ 'core/text' ],
transform: ( { content } ) => {
return {
blockType: 'core/text',
attributes: {
content
}
};
return createBlock( 'core/text', {
content
} );
}
}
]
Expand Down
62 changes: 21 additions & 41 deletions blocks/library/quote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import './style.scss';
import { registerBlock, query as hpq } from '../../api';
import { registerBlock, createBlock, query as hpq } from '../../api';
import Editable from '../../editable';

const { children, query } = hpq;
Expand Down Expand Up @@ -33,24 +33,18 @@ registerBlock( 'core/quote', {
type: 'block',
blocks: [ 'core/text' ],
transform: ( { content } ) => {
return {
blockType: 'core/quote',
attributes: {
value: content
}
};
return createBlock( 'core/quote', {
value: content
} );
}
},
{
type: 'block',
blocks: [ 'core/heading' ],
transform: ( { content } ) => {
return {
blockType: 'core/quote',
attributes: {
value: content
}
};
return createBlock( 'core/quote', {
value: content
} );
}
}
],
Expand All @@ -59,44 +53,30 @@ registerBlock( 'core/quote', {
type: 'block',
blocks: [ 'core/text' ],
transform: ( { value, citation } ) => {
return {
blockType: 'core/text',
attributes: {
content: wp.element.concatChildren( value, citation )
}
};
return createBlock( 'core/text', {
content: wp.element.concatChildren( value, citation )
} );
}
},
{
type: 'block',
blocks: [ 'core/heading' ],
transform: ( { value, citation, ...attrs } ) => {
if ( Array.isArray( value ) || citation ) {
const heading = {
blockType: 'core/heading',
attributes: {
nodeName: 'H2',
content: Array.isArray( value ) ? value[ 0 ] : value
}
};
const quote = {
blockType: 'core/quote',
attributes: {
...attrs,
citation,
value: Array.isArray( value ) ? value.slice( 1 ) : ''
}
};
const heading = createBlock( 'core/heading', {
content: Array.isArray( value ) ? value[ 0 ] : value
} );
const quote = createBlock( 'core/quote', {
...attrs,
citation,
value: Array.isArray( value ) ? value.slice( 1 ) : ''
} );

return [ heading, quote ];
}
return {
blockType: 'core/heading',
attributes: {
nodeName: 'H2',
content: value
}
};
return createBlock( 'core/heading', {
content: value
} );
}
}
]
Expand Down