From a8b51dce587a16d7156bf5cd87bd42b8868cd27e Mon Sep 17 00:00:00 2001 From: Andrei Draganescu Date: Tue, 28 May 2024 11:32:40 +0300 Subject: [PATCH] remove block name check and search top down for places to insert if ambiguous selection --- .../inserter/hooks/use-insertion-point.js | 33 ++++++------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/packages/block-editor/src/components/inserter/hooks/use-insertion-point.js b/packages/block-editor/src/components/inserter/hooks/use-insertion-point.js index ad655ec5b597f..f58f5c458f706 100644 --- a/packages/block-editor/src/components/inserter/hooks/use-insertion-point.js +++ b/packages/block-editor/src/components/inserter/hooks/use-insertion-point.js @@ -48,6 +48,7 @@ function useInsertionPoint( { getBlockIndex, getBlockRootClientId, getBlockOrder, + getBlocks, canInsertBlockType, } = useSelect( blockEditorStore ); const { destinationRootClientId, destinationIndex } = useSelect( @@ -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, @@ -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; } } }