From f85571e44684f1f69fe2d9c6247280a8bf4b6246 Mon Sep 17 00:00:00 2001 From: Andrei Draganescu Date: Sat, 3 Aug 2019 23:06:07 +0300 Subject: [PATCH] refactoring and named block insertion --- .../src/components/inserter/index.js | 33 +++++++++++-------- packages/block-editor/src/store/selectors.js | 32 ++++++++++++++++++ 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/packages/block-editor/src/components/inserter/index.js b/packages/block-editor/src/components/inserter/index.js index 6868f42e2be22b..cdd8f380e9e47e 100644 --- a/packages/block-editor/src/components/inserter/index.js +++ b/packages/block-editor/src/components/inserter/index.js @@ -21,18 +21,21 @@ import { */ import InserterMenu from './menu'; -const defaultRenderToggle = ( { onToggle, disabled, isOpen } ) => ( - -); +const defaultRenderToggle = ( { onToggle, disabled, isOpen, blockName = 'block' } ) => { + const label = `${ __( 'Add' ) } ${ blockName }`; + return ( + + ); +}; class Inserter extends Component { constructor() { @@ -66,6 +69,7 @@ class Inserter extends Component { const { disabled, hasOneAllowedItem, + blockName, createIfOne, renderToggle = defaultRenderToggle, } = this.props; @@ -74,7 +78,7 @@ class Inserter extends Component { onToggle = createIfOne; } - return renderToggle( { onToggle, isOpen, disabled } ); + return renderToggle( { onToggle, isOpen, disabled, blockName } ); } /** @@ -120,11 +124,12 @@ class Inserter extends Component { export default compose( [ withSelect( ( select, { rootClientId } ) => { - const { hasInserterItems, hasOneAllowedItem } = select( 'core/block-editor' ); + const { hasInserterItems, hasOneAllowedItem, getOneAllowedItemName } = select( 'core/block-editor' ); return { hasItems: hasInserterItems( rootClientId ), hasOneAllowedItem: hasOneAllowedItem( rootClientId ), + blockName: getOneAllowedItemName( rootClientId ), }; } ), withDispatch( ( dispatch, ownProps, { select } ) => { diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 6bc620669e0d08..1e4e2ffe8e8090 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -1292,6 +1292,38 @@ export const hasOneAllowedItem = ( state, rootClientId = null ) => { return false; }; +/** + * Determines whether there is only one item that may be inserted. + * @param {Object} state Editor state. + * @param {?string} rootClientId Optional root client ID of block list. + * + * @return {string} The name of the allowed block. + */ +export const getOneAllowedItemName = ( state, rootClientId = null ) => { + if ( rootClientId ) { + const parentBlockListSettings = getBlockListSettings( state, rootClientId ); + + if ( get( parentBlockListSettings, [ 'allowedBlocks', 'length' ], 0 ) === 1 ) { + let name = get( parentBlockListSettings, [ 'allowedBlocks' ] )[ 0 ]; + name = name.split( '/' )[ 1 ].replace( '-', ' ' ); + return name; + } + + return false; + } + + return false; +}; + +/** + * Determines whether there is only one item that may be inserted. + * @param {Object} state Editor state. + * @param {?string} clientId Block client ID. + * @param {?string} destinationRootClientId Root client ID of block list. + * @param {boolean} isAppender Determines if the block is added to a set of existing + * blocks in a list. + * @return {number} The insertion index. + */ export function getInsertionIndex( state, clientId, destinationRootClientId, isAppender ) { // If the clientId is defined, we insert at the position of the block. if ( clientId ) {