Skip to content

Commit

Permalink
remove block name check and search top down for places to insert if a…
Browse files Browse the repository at this point in the history
…mbiguous selection
  • Loading branch information
draganescu committed May 29, 2024
1 parent c2fbc8f commit a8b51dc
Showing 1 changed file with 10 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function useInsertionPoint( {
getBlockIndex,
getBlockRootClientId,
getBlockOrder,
getBlocks,
canInsertBlockType,
} = useSelect( blockEditorStore );
const { destinationRootClientId, destinationIndex } = useSelect(
Expand Down Expand Up @@ -135,8 +136,10 @@ function useInsertionPoint( {
_destinationRootClientId
)
);
if ( ! canInsertBlocks && normalizedBlocks.length === 1 ) {
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
const rootBlocks = getBlocks();
const currentRootBlock = 0;
while (
! canInsertBlockType(
normalizedBlocks[ 0 ].name,
Expand All @@ -160,28 +163,12 @@ function useInsertionPoint( {
_previousDestinationRootClientId ===
_destinationRootClientId
) {
break;
}
}
}

while ( ! canInsertBlocks() ) {
_destinationIndex =
getBlockIndex( _destinationRootClientId ) + 1;
_destinationRootClientId = getBlockRootClientId(
_destinationRootClientId
);

if ( _destinationRootClientId === null ) {
// 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;
if ( currentRootBlock === rootBlocks.length ) {
break;
}
// also try to find an insertable block top down
const rootBlock = rootBlocks[ currentRootBlock ];
_destinationRootClientId = rootBlock.clientId;
}
}
}
Expand Down

0 comments on commit a8b51dc

Please sign in to comment.