Skip to content

Commit

Permalink
make finding inserter blocks for template locked independent of block…
Browse files Browse the repository at this point in the history
… name
  • Loading branch information
draganescu committed May 29, 2024
1 parent 7145856 commit c2fbc8f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,23 @@ import { store as blockEditorStore } from '../../../store';
*/
const useBlockTypesState = ( rootClientId, onInsert ) => {
const [ allItems ] = useSelect( ( select ) => {
const { getBlocksByName, getInserterItems } =
select( blockEditorStore );
let availableItems = select( blockEditorStore ).getInserterItems( '' );

// Try the empty root first.
const rootInserterItems = getInserterItems( '' );
if ( rootInserterItems.length ) {
return [ rootInserterItems ];
// use current selection as root for situations like
// template locked mode
const rootBlocks = select( blockEditorStore ).getBlocks();
while ( availableItems.length === 0 ) {
for ( const block of rootBlocks ) {
availableItems = select( blockEditorStore ).getInserterItems(
block.clientId
);
if ( availableItems.length ) {
break;
}
}
}

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

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

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

const [ items ] = useSelect(
( select ) => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,35 @@ function useInsertionPoint( {
_destinationRootClientId
)
);
if ( ! canInsertBlocks && normalizedBlocks.length === 1 ) {
// If it's not possible to insert the block in the current location, try to find a new location up the tree
while (
! canInsertBlockType(
normalizedBlocks[ 0 ].name,
_destinationRootClientId
)
) {
// save the index of the previous destination root client ID
const _previousDestinationRootClientId =
_destinationRootClientId;
_destinationIndex =
getBlockIndex( _previousDestinationRootClientId ) +
1;

// get the parent of the current destination root client ID
_destinationRootClientId = getBlockRootClientId(
_destinationRootClientId
);

// Break the loop if we searched the whole tree
if (
_previousDestinationRootClientId ===
_destinationRootClientId
) {
break;
}
}
}

while ( ! canInsertBlocks() ) {
_destinationIndex =
Expand Down

0 comments on commit c2fbc8f

Please sign in to comment.