diff --git a/blocks/api/factory.js b/blocks/api/factory.js index be4668bfe6dbe6..07a292d7b5f1e5 100644 --- a/blocks/api/factory.js +++ b/blocks/api/factory.js @@ -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 + } }; } diff --git a/blocks/api/test/factory.js b/blocks/api/test/factory.js index cf59fbc34f8911..0e30dda9989438 100644 --- a/blocks/api/test/factory.js +++ b/blocks/api/test/factory.js @@ -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' );