Skip to content

Commit

Permalink
Block Hooks: Optimize selectors in the control component (#63141)
Browse files Browse the repository at this point in the history
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: ellatrix <[email protected]>
  • Loading branch information
3 people authored Jul 4, 2024
1 parent 3d618eb commit 7dda8fd
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions packages/block-editor/src/hooks/block-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,12 @@ function BlockHooksControlPure( {
[ blockTypes, name, ignoredHookedBlocks ]
);

const { blockIndex, rootClientId, innerBlocksLength } = useSelect(
( select ) => {
const { getBlocks, getBlockIndex, getBlockRootClientId } =
select( blockEditorStore );

return {
blockIndex: getBlockIndex( clientId ),
innerBlocksLength: getBlocks( clientId )?.length,
rootClientId: getBlockRootClientId( clientId ),
};
},
[ clientId ]
);

const hookedBlockClientIds = useSelect(
( select ) => {
const { getBlocks, getGlobalBlockCount } =
const { getBlocks, getBlockRootClientId, getGlobalBlockCount } =
select( blockEditorStore );

const rootClientId = getBlockRootClientId( clientId );
const _hookedBlockClientIds = hookedBlocksForCurrentBlock.reduce(
( clientIds, block ) => {
// If the block doesn't exist anywhere in the block tree,
Expand Down Expand Up @@ -127,9 +114,11 @@ function BlockHooksControlPure( {

return EMPTY_OBJECT;
},
[ hookedBlocksForCurrentBlock, name, clientId, rootClientId ]
[ hookedBlocksForCurrentBlock, name, clientId ]
);

const { getBlockIndex, getBlockCount, getBlockRootClientId } =
useSelect( blockEditorStore );
const { insertBlock, removeBlock } = useDispatch( blockEditorStore );

if ( ! hookedBlocksForCurrentBlock.length ) {
Expand All @@ -150,6 +139,10 @@ function BlockHooksControlPure( {
);

const insertBlockIntoDesignatedLocation = ( block, relativePosition ) => {
const blockIndex = getBlockIndex( clientId );
const innerBlocksLength = getBlockCount( clientId );
const rootClientId = getBlockRootClientId( clientId );

switch ( relativePosition ) {
case 'before':
case 'after':
Expand Down

0 comments on commit 7dda8fd

Please sign in to comment.