Skip to content

Commit

Permalink
Re-arrange code a bit more
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Sep 4, 2023
1 parent 1f4495a commit 5f72af4
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions packages/block-editor/src/hooks/auto-inserting-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,25 @@ function AutoInsertingBlocksControl( props ) {
[ blockTypes, props.blockName ]
);

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

const _rootClientId = getBlockRootClientId( props.clientId );
return {
blockIndex: getBlockIndex( props.clientId ),
innerBlocksLength: getBlock( props.clientId )?.innerBlocks
?.length,
rootClientId: getBlockRootClientId( props.clientId ),
};
},
[ props.clientId ]
);

const autoInsertedBlockClientIds = useSelect(
( select ) => {
const { getBlock, getGlobalBlockCount } =
select( blockEditorStore );

const _autoInsertedBlockClientIds =
autoInsertedBlocksForCurrentBlock.reduce(
Expand All @@ -64,7 +77,7 @@ function AutoInsertingBlocksControl( props ) {
// as an auto-inserted block (inserted `before` or `after` the current one),
// as the block might've been auto-inserted and then moved around a bit by the user.
candidates =
getBlock( _rootClientId )?.innerBlocks;
getBlock( rootClientId )?.innerBlocks;
break;

case 'first_child':
Expand Down Expand Up @@ -109,22 +122,12 @@ function AutoInsertingBlocksControl( props ) {

return EMPTY_OBJECT;
},
[ autoInsertedBlocksForCurrentBlock, props.blockName, props.clientId ]
);

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

return {
blockIndex: getBlockIndex( props.clientId ),
innerBlocksLength: getBlock( props.clientId )?.innerBlocks
?.length,
rootClientId: getBlockRootClientId( props.clientId ),
};
},
[ props.clientId ]
[
autoInsertedBlocksForCurrentBlock,
props.blockName,
props.clientId,
rootClientId,
]
);

const { insertBlock, removeBlock } = useDispatch( blockEditorStore );
Expand Down

0 comments on commit 5f72af4

Please sign in to comment.