From f0dbc37e2189e1722e651e1a35beedc5c3b7dcd8 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Fri, 5 May 2017 14:17:14 -0400 Subject: [PATCH] Include block default attributes in created block --- blocks/api/factory.js | 12 +++++++++++- blocks/api/test/factory.js | 6 ++++++ 2 files changed, 17 insertions(+), 1 deletion(-) 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' );