Skip to content

Commit

Permalink
Insert a block on start for some post formats. (#4554)
Browse files Browse the repository at this point in the history
If the post is empty and we select a format e.g: image the corresponding expected block is automatically added.
If a default post format is set when opening the editor the corresponding block for that format is automatically added.
  • Loading branch information
jorgefilipecosta authored Feb 22, 2018
1 parent 230277b commit ab55c97
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions blocks/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export {
getUnknownTypeHandlerName,
setDefaultBlockName,
getDefaultBlockName,
getDefaultBlockForPostFormat,
getBlockType,
getBlockTypes,
getBlockSupport,
Expand Down
29 changes: 28 additions & 1 deletion blocks/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ let unknownTypeHandlerName;
*/
let defaultBlockName;

/**
* Constant mapping post formats to the expected default block.
*
* @type {Object}
*/
const POST_FORMAT_BLOCK_MAP = {
audio: 'core/audio',
gallery: 'core/gallery',
image: 'core/image',
quote: 'core/quote',
video: 'core/video',
};

/**
* 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 @@ -202,12 +215,26 @@ export function setDefaultBlockName( name ) {
/**
* Retrieves the default block name.
*
* @return {?string} Blog name.
* @return {?string} Block name.
*/
export function getDefaultBlockName() {
return defaultBlockName;
}

/**
* Retrieves the expected default block for the post format.
*
* @param {string} postFormat Post format
* @return {string} Block name.
*/
export function getDefaultBlockForPostFormat( postFormat ) {
const blockName = POST_FORMAT_BLOCK_MAP[ postFormat ];
if ( blockName && getBlockType( blockName ) ) {
return blockName;
}
return null;
}

/**
* Returns a registered block type.
*
Expand Down
14 changes: 14 additions & 0 deletions editor/store/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
createReusableBlock,
isReusableBlock,
getDefaultBlockName,
getDefaultBlockForPostFormat,
} from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';
import { speak } from '@wordpress/a11y';
Expand Down Expand Up @@ -50,6 +51,7 @@ import {
isEditedPostNew,
isEditedPostSaveable,
getBlock,
getBlockCount,
getBlocks,
getReusableBlock,
getMetaBoxes,
Expand Down Expand Up @@ -300,6 +302,8 @@ export default {
} );
};
blocks = createBlocksFromTemplate( settings.template );
} else if ( getDefaultBlockForPostFormat( post.format ) ) {
blocks = [ createBlock( getDefaultBlockForPostFormat( post.format ) ) ];
} else {
blocks = [];
}
Expand Down Expand Up @@ -519,4 +523,14 @@ export default {
window.fetch( window._wpMetaBoxUrl, fetchOptions )
.then( () => store.dispatch( metaBoxUpdatesSuccess() ) );
},
EDIT_POST( action, { getState } ) {
const format = get( action, 'edits.format' );
if ( ! format ) {
return;
}
const blockName = getDefaultBlockForPostFormat( format );
if ( blockName && getBlockCount( getState() ) === 0 ) {
return insertBlock( createBlock( blockName ) );
}
},
};

0 comments on commit ab55c97

Please sign in to comment.