Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
7c9af07
Rename block.json field to __experimentalBlockHooks
ockham Sep 4, 2023
8dd9ed6
Update comment
ockham Sep 4, 2023
4325bef
Rename window. flag for experiment
ockham Sep 4, 2023
7b2e498
Rename Gutenberg experiment
ockham Sep 4, 2023
24e5119
Update GB experiment title and label
ockham Sep 4, 2023
33b84b2
Change classname
ockham Sep 4, 2023
a2ea218
Rename component
ockham Sep 4, 2023
5fb3202
Rename two variables
ockham Sep 4, 2023
89bb600
Rename more variables
ockham Sep 4, 2023
2bcf644
Comment wording
ockham Sep 4, 2023
8e7193e
Change hooked blocks block hook name :dizzy:
ockham Sep 4, 2023
282909a
Change HOC name
ockham Sep 4, 2023
6fa67be
s/gutenberg_register_auto_inserted_block/gutenberg_add_hooked_block/g
ockham Sep 4, 2023
ac87ee2
s/gutenberg_register_auto_inserted_blocks/gutenberg_add_hooked_blocks/g
ockham Sep 4, 2023
0d2cf6a
s/gutenberg_auto_insert_block/gutenberg_insert_hooked_block/g
ockham Sep 4, 2023
130b5d8
Change blocks controller field name
ockham Sep 4, 2023
bad8e9b
Change server block settings field name
ockham Sep 4, 2023
da434a2
s/gutenberg_register_auto_insert_rest_field/gutenberg_register_block_…
ockham Sep 4, 2023
2773ff8
Change variable name
ockham Sep 4, 2023
5943b7e
Update _doing_it_wrong message
ockham Sep 4, 2023
2d1d808
Update PHPDoc for gutenberg_add_hooked_block
ockham Sep 4, 2023
a48ddca
Update file's PHPDoc
ockham Sep 4, 2023
4139d17
Update arg and var names in gutenberg_add_hooked_block
ockham Sep 4, 2023
8173a09
Update PHPDoc for gutenberg_add_hooked_blocks
ockham Sep 4, 2023
bce1ad5
Tweak PHPDoc for gutenberg_add_hooked_block
ockham Sep 4, 2023
0d1667a
More updates to gutenberg_add_hooked_block inline PHPDoc
ockham Sep 4, 2023
b7a83ec
Update PHPDoc for gutenberg_insert_hooked_block
ockham Sep 4, 2023
d8663a0
Move helper functions below public API functions
ockham Sep 4, 2023
540b966
Change PHP file name
ockham Sep 4, 2023
3fa0ceb
Rename block property
ockham Sep 4, 2023
59c7055
Comment wording
ockham Sep 4, 2023
e14313e
Rename JS file
ockham Sep 4, 2023
4d05faf
Rename SCSS file
ockham Sep 4, 2023
09f7938
Update comment
ockham Sep 4, 2023
f6302a2
Typo
ockham Sep 4, 2023
c3622d6
Another typo
ockham Sep 4, 2023
ca5331b
Streamline names in JS file
ockham Sep 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename two variables
  • Loading branch information
ockham committed Sep 6, 2023
commit 5fb320293423677dbf083fd3ee117ecab50c7881
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 @@ -194,8 +190,7 @@ function BlocksHooksControl( props ) {
{ groupedAutoInsertedBlocks[ vendor ].map(
( block ) => {
const checked =
block.name in
autoInsertedBlockClientIds;
block.name in hookedBlockClientIds;

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

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