Skip to content

Commit

Permalink
Move the default/fallback blocktypes to the blocks store
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed May 10, 2018
1 parent d1981c2 commit e55bfb2
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 34 deletions.
22 changes: 4 additions & 18 deletions blocks/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,6 @@ import { select, dispatch } from '@wordpress/data';
* interacted with in an editor.
*/

/**
* Name of block handling unknown types.
*
* @type {?string}
*/
let unknownTypeHandlerName;

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

/**
* Constant mapping post formats to the expected default block.
*
Expand Down Expand Up @@ -182,7 +168,7 @@ export function unregisterBlockType( name ) {
* @param {string} name Block name.
*/
export function setUnknownTypeHandlerName( name ) {
unknownTypeHandlerName = name;
dispatch( 'core/blocks' ).setFallbackBlockType( name );
}

/**
Expand All @@ -192,7 +178,7 @@ export function setUnknownTypeHandlerName( name ) {
* @return {?string} Blog name.
*/
export function getUnknownTypeHandlerName() {
return unknownTypeHandlerName;
return select( 'core/blocks' ).getFallbackBlockTypeName();
}

/**
Expand All @@ -201,7 +187,7 @@ export function getUnknownTypeHandlerName() {
* @param {string} name Block name.
*/
export function setDefaultBlockName( name ) {
defaultBlockName = name;
dispatch( 'core/blocks' ).setDefaultBlockType( name );
}

/**
Expand All @@ -210,7 +196,7 @@ export function setDefaultBlockName( name ) {
* @return {?string} Block name.
*/
export function getDefaultBlockName() {
return defaultBlockName;
return select( 'core/blocks' ).getDefaultBlockTypeName();
}

/**
Expand Down
28 changes: 28 additions & 0 deletions blocks/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,31 @@ export function removeBlockTypes( names ) {
names: castArray( names ),
};
}

/**
* Returns an action object used to set the default block type.
*
* @param {string} name Block name.
*
* @return {Object} Action object.
*/
export function setDefaultBlockType( name ) {
return {
type: 'SET_DEFAULT_BLOCK_TYPE',
name,
};
}

/**
* Returns an action object used to set the fallback block type.
*
* @param {string} name Block name.
*
* @return {Object} Action object.
*/
export function setFallbackBlockType( name ) {
return {
type: 'SET_FALLBACK_BLOCK_TYPE',
name,
};
}
29 changes: 25 additions & 4 deletions blocks/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,35 @@ export const DEFAULT_CATEGORIES = [
*
* @return {Object} Updated state.
*/
export function blockTypes( state = [], action ) {
export function blockTypes( state = { types: [] }, action ) {
switch ( action.type ) {
case 'ADD_BLOCK_TYPES':
const addedNames = map( action.blockTypes, ( blockType ) => blockType.name );
const previousState = filter( state, ( blockType ) => addedNames.indexOf( blockType.name ) === -1 );
return [ ...previousState, ...action.blockTypes ];
const previousState = filter(
state.types,
( blockType ) => addedNames.indexOf( blockType.name ) === -1
);
return {
...state,
types: [ ...previousState, ...action.blockTypes ],
};
case 'REMOVE_BLOCK_TYPES':
return filter( state, ( blockType ) => action.names.indexOf( blockType.name ) === -1 );
return {
...state,
types: filter( state.types, ( blockType ) => action.names.indexOf( blockType.name ) === -1 ),
defaultBlockType: action.names.indexOf( state.defaultBlockType ) !== -1 ? undefined : state.defaultBlockType,
fallbackBlockType: action.names.indexOf( state.fallbackBlockType ) !== -1 ? undefined : state.fallbackBlockType,
};
case 'SET_DEFAULT_BLOCK_TYPE':
return {
...state,
defaultBlockType: action.name,
};
case 'SET_FALLBACK_BLOCK_TYPE':
return {
...state,
fallbackBlockType: action.name,
};
}

return state;
Expand Down
26 changes: 24 additions & 2 deletions blocks/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import { find } from 'lodash';
* @return {Array} Block Types.
*/
export function getBlockTypes( state ) {
return state.blockTypes;
return state.blockTypes.types;
}

export function getBlockType( state, name ) {
return find( state.blockTypes, { name } );
return find( state.blockTypes.types, { name } );
}

/**
Expand All @@ -28,3 +28,25 @@ export function getBlockType( state, name ) {
export function getCategories( state ) {
return state.categories;
}

/**
* Returns the name of the default block type
*
* @param {Object} state Data state.
*
* @return {string?} Default block type name.
*/
export function getDefaultBlockTypeName( state ) {
return state.blockTypes.defaultBlockType;
}

/**
* Returns the name of the fallback block type
*
* @param {Object} state Data state.
*
* @return {string?} Fallback block type name.
*/
export function getFallbackBlockTypeName( state ) {
return state.blockTypes.fallbackBlockType;
}
54 changes: 44 additions & 10 deletions blocks/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,74 @@ import { blockTypes, categories, DEFAULT_CATEGORIES } from '../reducer';

describe( 'blockTypes', () => {
it( 'should return an empty array as default state', () => {
expect( blockTypes( undefined, {} ) ).toEqual( [] );
expect( blockTypes( undefined, {} ) ).toEqual( { types: [] } );
} );

it( 'should add add a new block type', () => {
const original = deepFreeze( [
{ name: 'core/paragraph' },
] );
const original = deepFreeze( {
types: [
{ name: 'core/paragraph' },
],
} );

const state = blockTypes( original, {
type: 'ADD_BLOCK_TYPES',
blockTypes: [ { name: 'core/code' } ],
} );

expect( state ).toEqual( [
expect( state.types ).toEqual( [
{ name: 'core/paragraph' },
{ name: 'core/code' },
] );
} );

it( 'should remove block types', () => {
const original = deepFreeze( [
{ name: 'core/paragraph' },
{ name: 'core/code' },
] );
const original = deepFreeze( {
types: [
{ name: 'core/paragraph' },
{ name: 'core/code' },
],
} );

const state = blockTypes( original, {
type: 'REMOVE_BLOCK_TYPES',
names: [ 'core/code' ],
} );

expect( state ).toEqual( [
expect( state.types ).toEqual( [
{ name: 'core/paragraph' },
] );
} );

it( 'should set the default block type', () => {
const original = deepFreeze( {
types: [
{ name: 'core/paragraph' },
],
} );

const state = blockTypes( original, {
type: 'SET_DEFAULT_BLOCK_TYPE',
name: 'core/paragraph',
} );

expect( state.defaultBlockType ).toBe( 'core/paragraph' );
} );

it( 'should set the fallback block type', () => {
const original = deepFreeze( {
types: [
{ name: 'core/paragraph' },
],
} );

const state = blockTypes( original, {
type: 'SET_FALLBACK_BLOCK_TYPE',
name: 'core/paragraph',
} );

expect( state.fallbackBlockType ).toBe( 'core/paragraph' );
} );
} );

describe( 'categories', () => {
Expand Down

0 comments on commit e55bfb2

Please sign in to comment.