Skip to content

Commit

Permalink
Rename two variables
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Sep 4, 2023
1 parent 10ab5c2 commit 2e4749f
Showing 1 changed file with 59 additions and 64 deletions.
123 changes: 59 additions & 64 deletions packages/block-editor/src/hooks/auto-inserting-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function BlocksHooksControl( props ) {
[]
);

const autoInsertedBlocksForCurrentBlock = useMemo(
const hookedBlocksForCurrentBlock = useMemo(
() =>
blockTypes?.filter(
( { autoInsert } ) =>
Expand All @@ -51,79 +51,75 @@ function BlocksHooksControl( props ) {
[ props.clientId ]
);

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

const _autoInsertedBlockClientIds =
autoInsertedBlocksForCurrentBlock.reduce(
( clientIds, block ) => {
// If the block doesn't exist anywhere in the block tree,
// we know that we have to display the toggle for it, and set
// it to disabled.
if ( getGlobalBlockCount( block.name ) === 0 ) {
return clientIds;
}
const _hookedBlockClientIds = hookedBlocksForCurrentBlock.reduce(
( clientIds, block ) => {
// If the block doesn't exist anywhere in the block tree,
// we know that we have to display the toggle for it, and set
// it to disabled.
if ( getGlobalBlockCount( block.name ) === 0 ) {
return clientIds;
}

const relativePosition =
block?.autoInsert?.[ props.blockName ];
let candidates;
const relativePosition =
block?.autoInsert?.[ props.blockName ];
let candidates;

switch ( relativePosition ) {
case 'before':
case 'after':
// Any of the current block's siblings (with the right block type) qualifies
// 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;
break;
switch ( relativePosition ) {
case 'before':
case 'after':
// Any of the current block's siblings (with the right block type) qualifies
// 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;
break;

case 'first_child':
case 'last_child':
// Any of the current block's child blocks (with the right block type) qualifies
// as an auto-inserted first or last child block, as the block might've been
// auto-inserted and then moved around a bit by the user.
candidates = getBlock(
props.clientId
).innerBlocks;
break;
}
case 'first_child':
case 'last_child':
// Any of the current block's child blocks (with the right block type) qualifies
// as an auto-inserted first or last child block, as the block might've been
// auto-inserted and then moved around a bit by the user.
candidates = getBlock( props.clientId ).innerBlocks;
break;
}

const autoInsertedBlock = candidates?.find(
( { name } ) => name === block.name
);

// If the block exists in the designated location, we consider it auto-inserted
// and show the toggle as enabled.
if ( autoInsertedBlock ) {
return {
...clientIds,
[ block.name ]: autoInsertedBlock.clientId,
};
}
const autoInsertedBlock = candidates?.find(
( { name } ) => name === block.name
);

// If no auto-inserted block was found in any of its designated locations,
// but it exists elsewhere in the block tree, we consider it manually inserted.
// In this case, we take note and will remove the corresponding toggle from the
// block inspector panel.
// If the block exists in the designated location, we consider it auto-inserted
// and show the toggle as enabled.
if ( autoInsertedBlock ) {
return {
...clientIds,
[ block.name ]: false,
[ block.name ]: autoInsertedBlock.clientId,
};
},
{}
);
}

// If no auto-inserted block was found in any of its designated locations,
// but it exists elsewhere in the block tree, we consider it manually inserted.
// In this case, we take note and will remove the corresponding toggle from the
// block inspector panel.
return {
...clientIds,
[ block.name ]: false,
};
},
{}
);

if ( Object.values( _autoInsertedBlockClientIds ).length > 0 ) {
return _autoInsertedBlockClientIds;
if ( Object.values( _hookedBlockClientIds ).length > 0 ) {
return _hookedBlockClientIds;
}

return EMPTY_OBJECT;
},
[
autoInsertedBlocksForCurrentBlock,
hookedBlocksForCurrentBlock,
props.blockName,
props.clientId,
rootClientId,
Expand All @@ -133,17 +129,17 @@ function BlocksHooksControl( props ) {
const { insertBlock, removeBlock } = useDispatch( blockEditorStore );

// Remove toggle if block isn't present in the designated location but elsewhere in the block tree.
const autoInsertedBlocksForCurrentBlockIfNotPresentElsewhere =
autoInsertedBlocksForCurrentBlock?.filter(
( block ) => autoInsertedBlockClientIds?.[ block.name ] !== false
const hookedBlocksForCurrentBlockIfNotPresentElsewhere =
hookedBlocksForCurrentBlock?.filter(
( block ) => hookedBlockClientIds?.[ block.name ] !== false
);

if ( ! autoInsertedBlocksForCurrentBlockIfNotPresentElsewhere.length ) {
if ( ! hookedBlocksForCurrentBlockIfNotPresentElsewhere.length ) {
return null;
}

// Group by block namespace (i.e. prefix before the slash).
const groupedAutoInsertedBlocks = autoInsertedBlocksForCurrentBlock.reduce(
const groupedAutoInsertedBlocks = hookedBlocksForCurrentBlock.reduce(
( groups, block ) => {
const [ namespace ] = block.name.split( '/' );
if ( ! groups[ namespace ] ) {
Expand Down Expand Up @@ -197,8 +193,7 @@ function BlocksHooksControl( props ) {
// <BlockIcon icon={ block.icon } />

const checked =
block.name in
autoInsertedBlockClientIds;
block.name in hookedBlockClientIds;

return (
<ToggleControl
Expand Down Expand Up @@ -230,7 +225,7 @@ function BlocksHooksControl( props ) {

// Remove block.
const clientId =
autoInsertedBlockClientIds[
hookedBlockClientIds[
block.name
];
removeBlock( clientId, false );
Expand Down

0 comments on commit 2e4749f

Please sign in to comment.