Skip to content
Open
Changes from 2 commits
Commits
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
34 changes: 34 additions & 0 deletions packages/block-library/src/list-item/hooks/use-merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,29 @@ export default function useMerge( clientId, onMerge ) {
return getBlockOrder( order[ 0 ] )[ 0 ];
}

/**
* Given a list item client ID, walk up through ancestor list items
* and return the client ID of the first block found after any
* ancestor list. This is used for forward merging when there are no
* more list items at any nesting level.
*
* @param {string} id A list item client ID.
Comment thread
mikachan marked this conversation as resolved.
*/
function getNextOuterBlockClientId( id ) {
let parentListItemId = getParentListItemId( id );

while ( parentListItemId ) {
const parentListId = getBlockRootClientId( parentListItemId );
const nextOuterBlockClientId = getNextBlockClientId( parentListId );

if ( nextOuterBlockClientId ) {
return nextOuterBlockClientId;
}

parentListItemId = getParentListItemId( parentListItemId );
}
}

return ( forward ) => {
function mergeWithNested( clientIdA, clientIdB ) {
registry.batch( () => {
Expand Down Expand Up @@ -122,6 +145,17 @@ export default function useMerge( clientId, onMerge ) {
const nextBlockClientId = getNextId( clientId );

if ( ! nextBlockClientId ) {
const nextOuterBlockClientId =
getNextOuterBlockClientId( clientId );

if ( nextOuterBlockClientId ) {
mergeBlocks(
getBlockRootClientId( clientId ),
nextOuterBlockClientId
);
return;
}

onMerge( forward );
return;
}
Expand Down
Loading