Skip to content

Commit

Permalink
Block Insertion: Clear the insertion point when selecting a different…
Browse files Browse the repository at this point in the history
… block or clearing block selection (#64048)

* Clear the insertion point when selecting a different block or clearing block selection

* Use the block insertion point in the inserter, not the the one from the editor package

* use block insertion point index

* Don't change the insertion point when clearing block selection

* Also set the insertion point in the quick inserter

Co-authored-by: scruffian <scruffian@git.wordpress.org>
Co-authored-by: MaggieCabrera <onemaggie@git.wordpress.org>
  • Loading branch information
3 people authored Aug 12, 2024
1 parent 9f5ffd2 commit c73aa36
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import clsx from 'clsx';
import { useState, useEffect } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Button, SearchControl } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -82,6 +82,8 @@ export default function QuickInserter( {
}
}, [ setInserterIsOpened ] );

const { showInsertionPoint } = useDispatch( blockEditorStore );

// When clicking Browse All select the appropriate block so as
// the insertion point can work as expected.
const onBrowseAll = () => {
Expand All @@ -91,6 +93,7 @@ export default function QuickInserter( {
filterValue,
onSelect,
} );
showInsertionPoint( rootClientId, insertionIndex );
};

let maxBlockPatterns = 0;
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,8 @@ export function insertionPoint( state = null, action ) {
}

case 'HIDE_INSERTION_POINT':
case 'CLEAR_SELECTED_BLOCK':
case 'SELECT_BLOCK':
return null;
}

Expand Down
14 changes: 10 additions & 4 deletions packages/editor/src/components/inserter-sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const { PrivateInserterLibrary } = unlock( blockEditorPrivateApis );

export default function InserterSidebar() {
const {
blockInsertionPoint,
blockSectionRootClientId,
inserterSidebarToggleRef,
insertionPoint,
Expand All @@ -33,8 +34,12 @@ export default function InserterSidebar() {
getInsertionPoint,
isPublishSidebarOpened,
} = unlock( select( editorStore ) );
const { getBlockRootClientId, __unstableGetEditorMode, getSettings } =
select( blockEditorStore );
const {
getBlockInsertionPoint,
getBlockRootClientId,
__unstableGetEditorMode,
getSettings,
} = select( blockEditorStore );
const { get } = select( preferencesStore );
const { getActiveComplementaryArea } = select( interfaceStore );
const getBlockSectionRootClientId = () => {
Expand All @@ -47,6 +52,7 @@ export default function InserterSidebar() {
return getBlockRootClientId();
};
return {
blockInsertionPoint: getBlockInsertionPoint(),
inserterSidebarToggleRef: getInserterSidebarToggleRef(),
insertionPoint: getInsertionPoint(),
showMostUsedBlocks: get( 'core', 'mostUsedBlocks' ),
Expand Down Expand Up @@ -85,9 +91,9 @@ export default function InserterSidebar() {
showInserterHelpPanel
shouldFocusBlock={ isMobileViewport }
rootClientId={
blockSectionRootClientId ?? insertionPoint.rootClientId
blockSectionRootClientId ?? blockInsertionPoint.rootClientId
}
__experimentalInsertionIndex={ insertionPoint.insertionIndex }
__experimentalInsertionIndex={ blockInsertionPoint.index }
onSelect={ insertionPoint.onSelect }
__experimentalInitialTab={ insertionPoint.tab }
__experimentalInitialCategory={ insertionPoint.category }
Expand Down

0 comments on commit c73aa36

Please sign in to comment.