Skip to content

Commit

Permalink
BlockListAppender: with custom appender, don't react to nested list s…
Browse files Browse the repository at this point in the history
…ettings changes
  • Loading branch information
jsnajdr committed Dec 20, 2022
1 parent 4cc4c3c commit d99c051
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 88 deletions.
119 changes: 65 additions & 54 deletions packages/block-editor/src/components/block-list-appender/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,67 +16,78 @@ import DefaultBlockAppender from '../default-block-appender';
import ButtonBlockAppender from '../button-block-appender';
import { store as blockEditorStore } from '../../store';

function BlockListAppender( {
rootClientId,
renderAppender: CustomAppender,
className,
tagName: TagName = 'div',
} ) {
const { hideInserter, canInsertDefaultBlock, selectedBlockClientId } =
useSelect(
( select ) => {
const {
canInsertBlockType,
getTemplateLock,
getSelectedBlockClientId,
__unstableGetEditorMode,
} = select( blockEditorStore );

return {
hideInserter:
!! getTemplateLock( rootClientId ) ||
__unstableGetEditorMode() === 'zoom-out',
canInsertDefaultBlock: canInsertBlockType(
getDefaultBlockName(),
rootClientId
),
selectedBlockClientId: getSelectedBlockClientId(),
};
},
[ rootClientId ]
);
function DefaultAppender( { rootClientId } ) {
const canInsertDefaultBlock = useSelect( ( select ) =>
select( blockEditorStore ).canInsertBlockType(
getDefaultBlockName(),
rootClientId
)
);

if ( canInsertDefaultBlock ) {
// Render the default block appender if the context supports use
// of the default appender.
return <DefaultBlockAppender rootClientId={ rootClientId } />;
}

// Fallback in case the default block can't be inserted.
return (
<ButtonBlockAppender
rootClientId={ rootClientId }
className="block-list-appender__toggle"
/>
);
}

function useAppender( rootClientId, CustomAppender ) {
const { hideInserter, isParentSelected } = useSelect(
( select ) => {
const {
getTemplateLock,
getSelectedBlockClientId,
__unstableGetEditorMode,
} = select( blockEditorStore );

const selectedBlockClientId = getSelectedBlockClientId();

return {
hideInserter:
!! getTemplateLock( rootClientId ) ||
__unstableGetEditorMode() === 'zoom-out',
isParentSelected:
rootClientId === selectedBlockClientId ||
( rootClientId === '' && ! selectedBlockClientId ),
};
},
[ rootClientId ]
);

if ( hideInserter || CustomAppender === false ) {
return null;
}

let appender;
if ( CustomAppender ) {
// Prefer custom render prop if provided.
appender = <CustomAppender />;
} else {
const isParentSelected =
selectedBlockClientId === rootClientId ||
( ! rootClientId && ! selectedBlockClientId );

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 } />;
} else {
// Fallback in the case no renderAppender has been provided and the
// default block can't be inserted.
appender = (
<ButtonBlockAppender
rootClientId={ rootClientId }
className="block-list-appender__toggle"
/>
);
}
return <CustomAppender />;
}

if ( ! isParentSelected ) {
return null;
}

return <DefaultAppender rootClientId={ rootClientId } />;
}

function BlockListAppender( {
rootClientId,
renderAppender,
className,
tagName: TagName = 'div',
} ) {
const appender = useAppender( rootClientId, renderAppender );

if ( ! appender ) {
return null;
}

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { withSelect } from '@wordpress/data';
import { useSelect } from '@wordpress/data';
import { getDefaultBlockName } from '@wordpress/blocks';

/**
Expand All @@ -11,14 +11,48 @@ import DefaultBlockAppender from '../default-block-appender';
import styles from './style.scss';
import { store as blockEditorStore } from '../../store';

function BlockListAppender( {
blockClientIds,
function DefaultAppender( { rootClientId, showSeparator } ) {
const { blockClientIds, canInsertDefaultBlock } = useSelect(
( select ) => {
const { getBlockOrder, canInsertBlockType } =
select( blockEditorStore );
return {
blockClientIds: getBlockOrder( rootClientId ),
canInsertDefaultBlock: canInsertBlockType(
getDefaultBlockName(),
rootClientId
),
};
},
[ rootClientId ]
);

if ( ! canInsertDefaultBlock ) {
return null;
}

return (
<DefaultBlockAppender
rootClientId={ rootClientId }
lastBlockClientId={ blockClientIds[ blockClientIds.length - 1 ] }
containerStyle={ styles.blockListAppender }
placeholder={ blockClientIds.length > 0 ? '' : null }
showSeparator={ showSeparator }
/>
);
}

export default function BlockListAppender( {
rootClientId,
canInsertDefaultBlock,
isLocked,
renderAppender: CustomAppender,
showSeparator,
} ) {
const isLocked = useSelect(
( select ) =>
!! select( blockEditorStore ).getTemplateLock( rootClientId ),
[ rootClientId ]
);

if ( isLocked ) {
return null;
}
Expand All @@ -27,33 +61,10 @@ function BlockListAppender( {
return <CustomAppender showSeparator={ showSeparator } />;
}

if ( canInsertDefaultBlock ) {
return (
<DefaultBlockAppender
rootClientId={ rootClientId }
lastBlockClientId={
blockClientIds[ blockClientIds.length - 1 ]
}
containerStyle={ styles.blockListAppender }
placeholder={ blockClientIds.length > 0 ? '' : null }
showSeparator={ showSeparator }
/>
);
}

return null;
return (
<DefaultAppender
rootClientId={ rootClientId }
showSeparator={ showSeparator }
/>
);
}

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

return {
isLocked: !! getTemplateLock( rootClientId ),
blockClientIds: getBlockOrder( rootClientId ),
canInsertDefaultBlock: canInsertBlockType(
getDefaultBlockName(),
rootClientId
),
};
} )( BlockListAppender );

0 comments on commit d99c051

Please sign in to comment.