From 1899810ff12acfdbf8e3ca31f4595420f79eb7e7 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 21 Oct 2024 09:39:27 +0100 Subject: [PATCH] Fix empty Patterns inserter when selecting section root when editing Pages in Site Editor (#66214) * If selected block is the root then set as destination * Set correct destination index * Scope to Zoom Out only for 6.7 --- .../inserter/hooks/use-insertion-point.js | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 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 24074ec5004565..97a6bb4fb74abe 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 @@ -79,7 +79,9 @@ function useInsertionPoint( { getBlockRootClientId, getBlockIndex, getBlockOrder, - } = select( blockEditorStore ); + getSectionRootClientId, + __unstableGetEditorMode, + } = unlock( select( blockEditorStore ) ); const selectedBlockClientId = getSelectedBlockClientId(); let _destinationRootClientId = rootClientId; @@ -92,10 +94,26 @@ function useInsertionPoint( { // Insert after a specific client ID. _destinationIndex = getBlockIndex( clientId ); } else if ( ! isAppender && selectedBlockClientId ) { - _destinationRootClientId = getBlockRootClientId( - selectedBlockClientId - ); - _destinationIndex = getBlockIndex( selectedBlockClientId ) + 1; + const sectionRootClientId = getSectionRootClientId(); + + // Avoids empty inserter when the selected block is acting + // as the "root". + // See https://github.com/WordPress/gutenberg/pull/66214. + if ( + __unstableGetEditorMode() === 'zoom-out' && + sectionRootClientId === selectedBlockClientId + ) { + _destinationRootClientId = sectionRootClientId; + _destinationIndex = getBlockOrder( + _destinationRootClientId + ).length; + } else { + _destinationRootClientId = getBlockRootClientId( + selectedBlockClientId + ); + _destinationIndex = + getBlockIndex( selectedBlockClientId ) + 1; + } } else { // Insert at the end of the list. _destinationIndex = getBlockOrder(