From b522ead52b154e467851a95d84c30ed863df6b41 Mon Sep 17 00:00:00 2001 From: Nik Tsekouras Date: Thu, 30 Jul 2020 16:14:09 +0300 Subject: [PATCH 1/5] Fix block insertion place after clicking Browse All in the inline inserter (#24285) * try for insertion point fix * use getPreviousBlockClientId selector --- .../src/components/inserter/quick-inserter.js | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/components/inserter/quick-inserter.js b/packages/block-editor/src/components/inserter/quick-inserter.js index 6131ffe69cec9..e667a3e183ec2 100644 --- a/packages/block-editor/src/components/inserter/quick-inserter.js +++ b/packages/block-editor/src/components/inserter/quick-inserter.js @@ -13,7 +13,7 @@ import { Button, withSpokenMessages, } from '@wordpress/components'; -import { useSelect } from '@wordpress/data'; +import { useSelect, useDispatch } from '@wordpress/data'; import { LEFT, RIGHT, UP, DOWN, BACKSPACE, ENTER } from '@wordpress/keycodes'; /** @@ -160,12 +160,20 @@ function QuickInserter( { [] ); + const previousBlockClientId = useSelect( + ( select ) => + select( 'core/block-editor' ).getPreviousBlockClientId( clientId ), + [ clientId ] + ); + useEffect( () => { if ( setInsererIsOpened ) { setInsererIsOpened( false ); } }, [ setInsererIsOpened ] ); + const { selectBlock } = useDispatch( 'core/block-editor' ); + // Announce search results on change useEffect( () => { if ( ! filterValue ) { @@ -180,6 +188,15 @@ function QuickInserter( { debouncedSpeak( resultsFoundMessage ); }, [ filterValue, debouncedSpeak ] ); + // When clicking Browse All select the appropriate block so as + // the insertion point can work as expected + const onBrowseAll = () => { + // We have to select the previous block because the menu inserter + // inserts the new block after the selected one. + selectBlock( previousBlockClientId ); + setInsererIsOpened( true ); + }; + // Disable reason (no-autofocus): The inserter menu is a modal display, not one which // is always visible, and one which already incurs this behavior of autoFocus via // Popover's focusOnMount. @@ -212,7 +229,7 @@ function QuickInserter( { { setInsererIsOpened && (