Skip to content

Commit

Permalink
Allow inserting blocks to post-content block on page
Browse files Browse the repository at this point in the history
  • Loading branch information
jeryj committed May 23, 2024
1 parent f897cce commit 10ba0a5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
23 changes: 12 additions & 11 deletions packages/block-editor/src/components/inserter/block-types-tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ export function BlockTypesTab(
{ rootClientId, onInsert, onHover, showMostUsedBlocks },
ref
) {
const [ items, categories, collections, onSelectItem ] = useBlockTypesState(
rootClientId,
onInsert
);
// Find the root of what we _can_ insert into for this block.
// const { rootContentBlockId } = useSelect( ( select ) => {
// // Try the empty root first.
// const { getAllowedBlockTypes } = select( 'core/block-editor' );

// select( 'core/block-editor' ).getInserterItems( rootClientId );

// TODO: Split this into its own component that has all items?
const [ allItems, allCategories ] = useBlockTypesState( '', () => {} );
// }, [] );

const [ allItems, items, categories, collections, onSelectItem ] =
useBlockTypesState( rootClientId, onInsert );

const allItemsPerCategory = useMemo( () => {
return pipe(
Expand Down Expand Up @@ -122,10 +126,6 @@ export function BlockTypesTab(
didRenderAllCategories ? collectionEntries : EMPTY_ARRAY
);

if ( ! items.length ) {
return <InserterNoResults />;
}

const availableCategories = categories.filter( ( category ) => {
const categoryItems = itemsPerCategory[ category.slug ];
if ( ! categoryItems || ! categoryItems.length ) {
Expand All @@ -137,6 +137,7 @@ export function BlockTypesTab(
return (
<InserterListbox>
<div ref={ ref }>
{ ! items.length && ! allItems.length && <InserterNoResults /> }
{ showMostUsedBlocks && !! suggestedItems.length && (
<InserterPanel title={ _x( 'Most used', 'blocks' ) }>
<BlockTypesList
Expand Down Expand Up @@ -214,7 +215,7 @@ export function BlockTypesTab(
{ ( items.length === 0 ||
availableCategories.length === 1 ) && (
<div className="block-editor-inserter__all-blocks">
{ allCategories.map( ( category ) => {
{ categories.map( ( category ) => {
const categoryItems =
allItemsPerCategory[ category.slug ];
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ import { store as blockEditorStore } from '../../../store';
* @return {Array} Returns the block types state. (block types, categories, collections, onSelect handler)
*/
const useBlockTypesState = ( rootClientId, onInsert ) => {
const [ allItems ] = useSelect( ( select ) => {
const { getBlocksByName, getInserterItems } =
select( blockEditorStore );

// Try the empty root first.
const rootInserterItems = getInserterItems( '' );
if ( rootInserterItems.length ) {
return [ rootInserterItems ];
}

const postContentBlock = getBlocksByName( 'core/post-content' );

if ( postContentBlock.length ) {
return [ getInserterItems( postContentBlock[ 0 ] ) ];
}

// Failsafe
return [ [] ];
}, [] );

const [ items ] = useSelect(
( select ) => [
select( blockEditorStore ).getInserterItems( rootClientId ),
Expand Down Expand Up @@ -57,7 +77,7 @@ const useBlockTypesState = ( rootClientId, onInsert ) => {
[ onInsert ]
);

return [ items, categories, collections, onSelectItem ];
return [ allItems, items, categories, collections, onSelectItem ];
};

export default useBlockTypesState;
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function useInsertionPoint( {
} ) {
const {
getSelectedBlock,
getBlocksByName,
getBlockIndex,
getBlockRootClientId,
getBlockOrder,
Expand Down Expand Up @@ -142,9 +143,17 @@ function useInsertionPoint( {
_destinationRootClientId
);

// If we are at the root, there's nothing more to check
if ( _destinationRootClientId === null ) {
break;
// If we are at the root, try to find a post content block
const postContentBlock =
getBlocksByName( 'core/post-content' );
if ( postContentBlock.length ) {
_destinationRootClientId = postContentBlock[ 0 ];
// Insert at the end of the post content block.
_destinationIndex = getBlockOrder(
postContentBlock[ 0 ]
).length;
}
}
}

Expand Down Expand Up @@ -181,6 +190,10 @@ function useInsertionPoint( {
onSelect,
shouldFocusBlock,
selectBlockOnInsert,
setLastFocus,
canInsertBlockType,
getBlockOrder,
getBlocksByName,
]
);

Expand Down

0 comments on commit 10ba0a5

Please sign in to comment.