Skip to content

Commit

Permalink
Always show the block appender when its parent is selected
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Nov 19, 2021
1 parent d69d172 commit a3eb290
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 43 deletions.
26 changes: 5 additions & 21 deletions packages/block-editor/src/components/block-list-appender/index.js
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { last } from 'lodash';
import classnames from 'classnames';

/**
Expand All @@ -18,7 +17,6 @@ import ButtonBlockAppender from '../button-block-appender';
import { store as blockEditorStore } from '../../store';

function BlockListAppender( {
blockClientIds,
rootClientId,
canInsertDefaultBlock,
isLocked,
Expand All @@ -36,30 +34,18 @@ function BlockListAppender( {
// Prefer custom render prop if provided.
appender = <CustomAppender />;
} else {
const isDocumentAppender = ! rootClientId;
const isParentSelected = selectedBlockClientId === rootClientId;
const isAnotherDefaultAppenderAlreadyDisplayed =
selectedBlockClientId &&
! blockClientIds.includes( selectedBlockClientId );
const isParentSelected =
selectedBlockClientId === rootClientId ||
( ! rootClientId && ! selectedBlockClientId );

if (
! isDocumentAppender &&
! isParentSelected &&
( ! selectedBlockClientId ||
isAnotherDefaultAppenderAlreadyDisplayed )
) {
if ( ! isParentSelected ) {
return null;
}

if ( canInsertDefaultBlock ) {
// Render the default block appender when renderAppender has not been
// provided and the context supports use of the default appender.
appender = (
<DefaultBlockAppender
rootClientId={ rootClientId }
lastBlockClientId={ last( blockClientIds ) }
/>
);
appender = <DefaultBlockAppender rootClientId={ rootClientId } />;
} else {
// Fallback in the case no renderAppender has been provided and the
// default block can't be inserted.
Expand Down Expand Up @@ -103,15 +89,13 @@ function BlockListAppender( {

export default withSelect( ( select, { rootClientId } ) => {
const {
getBlockOrder,
canInsertBlockType,
getTemplateLock,
getSelectedBlockClientId,
} = select( blockEditorStore );

return {
isLocked: !! getTemplateLock( rootClientId ),
blockClientIds: getBlockOrder( rootClientId ),
canInsertDefaultBlock: canInsertBlockType(
getDefaultBlockName(),
rootClientId
Expand Down
Expand Up @@ -8,7 +8,6 @@ import classnames from 'classnames';
*/
import { __ } from '@wordpress/i18n';
import { compose } from '@wordpress/compose';
import { getDefaultBlockName } from '@wordpress/blocks';
import { decodeEntities } from '@wordpress/html-entities';
import { withSelect, withDispatch } from '@wordpress/data';

Expand All @@ -26,13 +25,12 @@ export const ZWNBSP = '\ufeff';

export function DefaultBlockAppender( {
isLocked,
isVisible,
onAppend,
showPrompt,
placeholder,
rootClientId,
} ) {
if ( isLocked || ! isVisible ) {
if ( isLocked ) {
return null;
}

Expand Down Expand Up @@ -77,23 +75,14 @@ export function DefaultBlockAppender( {

export default compose(
withSelect( ( select, ownProps ) => {
const {
getBlockCount,
getBlockName,
isBlockValid,
getSettings,
getTemplateLock,
} = select( blockEditorStore );
const { getBlockCount, getSettings, getTemplateLock } = select(
blockEditorStore
);

const isEmpty = ! getBlockCount( ownProps.rootClientId );
const isLastBlockDefault =
getBlockName( ownProps.lastBlockClientId ) ===
getDefaultBlockName();
const isLastBlockValid = isBlockValid( ownProps.lastBlockClientId );
const { bodyPlaceholder } = getSettings();

return {
isVisible: isEmpty || ! isLastBlockDefault || ! isLastBlockValid,
showPrompt: isEmpty,
isLocked: !! getTemplateLock( ownProps.rootClientId ),
placeholder: bodyPlaceholder,
Expand Down
Expand Up @@ -16,13 +16,8 @@ import BaseDefaultBlockAppender from '../default-block-appender';
import withClientId from './with-client-id';
import { store as blockEditorStore } from '../../store';

export const DefaultBlockAppender = ( { clientId, lastBlockClientId } ) => {
return (
<BaseDefaultBlockAppender
rootClientId={ clientId }
lastBlockClientId={ lastBlockClientId }
/>
);
export const DefaultBlockAppender = ( { clientId } ) => {
return <BaseDefaultBlockAppender rootClientId={ clientId } />;
};

export default compose( [
Expand Down

0 comments on commit a3eb290

Please sign in to comment.