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

Include block default attributes in created block #686

Merged
merged 1 commit into from
May 9, 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
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use blockSettings.defaultAttributes directly here. You showed me that Object.assign handles the undefined value properly :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use blockSettings.defaultAttributes directly here. You showed me that Object.assign handles the undefined value properly :)

It'll handle an undefined defaultAttributes here, but if blockSettings doesn't exist and we try to access the defaultAttributes property on it, an error would be thrown.

...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