Skip to content

Commit

Permalink
Framework: Set/Get default block name
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Jun 15, 2017
1 parent 90d7ea1 commit c80a365
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
2 changes: 2 additions & 0 deletions blocks/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export {
unregisterBlockType,
setUnknownTypeHandler,
getUnknownTypeHandler,
setDefaultBlock,
getDefaultBlock,
getBlockType,
getBlockTypes,
} from './registration';
25 changes: 25 additions & 0 deletions blocks/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ const blocks = {};
*/
let unknownTypeHandler;

/**
* Name of the default block.
*
* @type {?string}
*/
let defaultBlockName;

/**
* Registers a new block provided a unique name and an object defining its
* behavior. Once registered, the block is made available as an option to any
Expand Down Expand Up @@ -86,6 +93,24 @@ export function getUnknownTypeHandler() {
return unknownTypeHandler;
}

/**
* Assigns the default block name
*
* @param {string} name Block name
*/
export function setDefaultBlock( name ) {
defaultBlockName = name;
}

/**
* Retrieves the default block name
*
* @return {?string} Blog name
*/
export function getDefaultBlock() {
return defaultBlockName;
}

/**
* Returns a registered block type.
*
Expand Down
17 changes: 17 additions & 0 deletions blocks/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
unregisterBlockType,
setUnknownTypeHandler,
getUnknownTypeHandler,
setDefaultBlock,
getDefaultBlock,
getBlockType,
getBlockTypes,
} from '../registration';
Expand All @@ -29,6 +31,7 @@ describe( 'blocks', () => {
unregisterBlockType( block.name );
} );
setUnknownTypeHandler( undefined );
setDefaultBlock( undefined );
console.error.restore();
} );

Expand Down Expand Up @@ -108,6 +111,20 @@ describe( 'blocks', () => {
} );
} );

describe( 'setDefaultBlock()', () => {
it( 'assigns default block name', () => {
setDefaultBlock( 'core/test-block' );

expect( getDefaultBlock() ).to.equal( 'core/test-block' );
} );
} );

describe( 'getDefaultBlock()', () => {
it( 'defaults to undefined', () => {
expect( getDefaultBlock() ).to.be.undefined();
} );
} );

describe( 'getBlockType()', () => {
it( 'should return { name } for blocks with no settings', () => {
registerBlockType( 'core/test-block' );
Expand Down
4 changes: 3 additions & 1 deletion blocks/library/text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Children, cloneElement } from 'element';
/**
* Internal dependencies
*/
import { registerBlockType, createBlock, query } from '../../api';
import { registerBlockType, createBlock, query, setDefaultBlock } from '../../api';
import AlignmentToolbar from '../../alignment-toolbar';
import BlockControls from '../../block-controls';
import Editable from '../../editable';
Expand Down Expand Up @@ -78,3 +78,5 @@ registerBlockType( 'core/text', {
) );
},
} );

setDefaultBlock( 'core/text' );

0 comments on commit c80a365

Please sign in to comment.