Skip to content

Commit

Permalink
Refactor: useBlockTools cleanup (#59450)
Browse files Browse the repository at this point in the history
selectedBlockClientIds() does quite a bit more work, when we only need one of the selected block ids. Generally, this will only be one block id. In the case of multiples, the previous method would get the topmost block in the selection in the array. getBlockSelectionStart gets the block where selection starts, which is a slightly different concept. Functionally, I think the outcome is identical, and getBlockSelectionStart should be faster.

Co-authored-by: Andrei Draganescu <andrei.draganescu@automattic.com>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
  • Loading branch information
3 people authored Apr 5, 2024
1 parent 217aefe commit 7232cc4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ export function useHasBlockToolbar() {
const {
getBlockEditingMode,
getBlockName,
getSelectedBlockClientIds,
getBlockSelectionStart,
} = select( blockEditorStore );

const selectedBlockClientIds = getSelectedBlockClientIds();
const selectedBlockClientId = selectedBlockClientIds[ 0 ];
// we only care about the 1st selected block
// for the toolbar, so we use getBlockSelectionStart
// instead of getSelectedBlockClientIds
const selectedBlockClientId = getBlockSelectionStart();
const isDefaultEditingMode =
getBlockEditingMode( selectedBlockClientId ) === 'default';
const blockType =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,15 @@ export function useShowBlockTools() {
getSelectedBlockClientId() ||
getFirstMultiSelectedBlockClientId();

const { name = '', attributes = {} } = getBlock( clientId ) || {};
const block = getBlock( clientId ) || { name: '', attributes: {} };
const editorMode = __unstableGetEditorMode();
const hasSelectedBlock = clientId && name;
const isEmptyDefaultBlock = isUnmodifiedDefaultBlock( {
name,
attributes,
} );
const hasSelectedBlock = clientId && block?.name;
const isEmptyDefaultBlock = isUnmodifiedDefaultBlock( block );
const _showEmptyBlockSideInserter =
clientId &&
! isTyping() &&
editorMode === 'edit' &&
isUnmodifiedDefaultBlock( { name, attributes } );
isEmptyDefaultBlock;
const maybeShowBreadcrumb =
hasSelectedBlock &&
! hasMultiSelection() &&
Expand Down

0 comments on commit 7232cc4

Please sign in to comment.