Skip to content

Commit

Permalink
refactoring and named block insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
draganescu committed Sep 25, 2019
1 parent 254b848 commit 58d98c5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
33 changes: 19 additions & 14 deletions packages/block-editor/src/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ import {
*/
import InserterMenu from './menu';

const defaultRenderToggle = ( { onToggle, disabled, isOpen } ) => (
<IconButton
icon="insert"
label={ __( 'Add block' ) }
labelPosition="bottom"
onClick={ onToggle }
className="editor-inserter__toggle block-editor-inserter__toggle"
aria-haspopup="true"
aria-expanded={ isOpen }
disabled={ disabled }
/>
);
const defaultRenderToggle = ( { onToggle, disabled, isOpen, blockName = 'block' } ) => {
const label = `${ __( 'Add' ) } ${ blockName }`;
return (
<IconButton
icon="insert"
label={ label }
labelPosition="bottom"
onClick={ onToggle }
className="editor-inserter__toggle block-editor-inserter__toggle"
aria-haspopup="true"
aria-expanded={ isOpen }
disabled={ disabled }
/>
);
};

class Inserter extends Component {
constructor() {
Expand Down Expand Up @@ -66,6 +69,7 @@ class Inserter extends Component {
const {
disabled,
hasOneAllowedItem,
blockName,
createIfOne,
renderToggle = defaultRenderToggle,
} = this.props;
Expand All @@ -74,7 +78,7 @@ class Inserter extends Component {
onToggle = createIfOne;
}

return renderToggle( { onToggle, isOpen, disabled } );
return renderToggle( { onToggle, isOpen, disabled, blockName } );
}

/**
Expand Down Expand Up @@ -121,11 +125,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 } ) => {
Expand Down
32 changes: 32 additions & 0 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down

0 comments on commit 58d98c5

Please sign in to comment.