Skip to content

Commit

Permalink
Merge pull request #686 from WordPress/fix/create-block-defaults
Browse files Browse the repository at this point in the history
Include block default attributes in created block
  • Loading branch information
aduth committed May 9, 2017
2 parents 00bd33b + f0dbc37 commit 90cc25b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
12 changes: 11 additions & 1 deletion blocks/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@ import { getBlockSettings } from './registration';
* @return {Object} Block object
*/
export function createBlock( blockType, attributes = {} ) {
const blockSettings = getBlockSettings( blockType );

let defaultAttributes;
if ( blockSettings ) {
defaultAttributes = blockSettings.defaultAttributes;
}

return {
uid: uuid(),
blockType,
attributes
attributes: {
...defaultAttributes,
...attributes
}
};
}

Expand Down
6 changes: 6 additions & 0 deletions blocks/api/test/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ describe( 'block factory', () => {

describe( 'createBlock()', () => {
it( 'should create a block given its blockType and attributes', () => {
registerBlock( 'core/test-block', {
defaultAttributes: {
includesDefault: true
}
} );
const block = createBlock( 'core/test-block', {
align: 'left'
} );

expect( block.blockType ).to.eql( 'core/test-block' );
expect( block.attributes ).to.eql( {
includesDefault: true,
align: 'left'
} );
expect( block.uid ).to.be.a( 'string' );
Expand Down

0 comments on commit 90cc25b

Please sign in to comment.