Skip to content

Commit

Permalink
List v2: merge with nested items (#42551)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Jul 20, 2022
1 parent b5fce3f commit 4ed1c11
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/block-library/src/list-item/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
useIndentListItem,
useOutdentListItem,
useSplit,
useMerge,
} from './hooks';
import { convertToListItems } from './utils';

Expand Down Expand Up @@ -57,7 +58,6 @@ function IndentUI( { clientId } ) {
export default function ListItemEdit( {
attributes,
setAttributes,
mergeBlocks,
onReplace,
clientId,
} ) {
Expand All @@ -70,6 +70,7 @@ export default function ListItemEdit( {
const useBackspaceRef = useBackspace( { clientId } );
const useSpaceRef = useSpace( clientId );
const onSplit = useSplit( clientId );
const onMerge = useMerge( clientId );
return (
<>
<li { ...innerBlocksProps }>
Expand All @@ -88,7 +89,7 @@ export default function ListItemEdit( {
aria-label={ __( 'List text' ) }
placeholder={ placeholder || __( 'List' ) }
onSplit={ onSplit }
onMerge={ mergeBlocks }
onMerge={ onMerge }
onReplace={ ( blocks, ...args ) => {
onReplace( convertToListItems( blocks ), ...args );
} }
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/list-item/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { default as useEnter } from './use-enter';
export { default as useBackspace } from './use-backspace';
export { default as useSpace } from './use-space';
export { default as useSplit } from './use-split';
export { default as useMerge } from './use-merge';
72 changes: 72 additions & 0 deletions packages/block-library/src/list-item/hooks/use-merge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* WordPress dependencies
*/
import { useRegistry, useDispatch, useSelect } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';

export default function useMerge( clientId ) {
const registry = useRegistry();
const {
getPreviousBlockClientId,
getNextBlockClientId,
getBlockOrder,
getBlockRootClientId,
} = useSelect( blockEditorStore );
const { mergeBlocks, moveBlocksToPosition } =
useDispatch( blockEditorStore );

function getTrailing( id ) {
const order = getBlockOrder( id );

if ( ! order.length ) {
return id;
}

return getTrailing( order[ order.length - 1 ] );
}

function getNext( id ) {
return (
getNextBlockClientId( id ) || getNext( getBlockRootClientId( id ) )
);
}

return ( forward ) => {
if ( forward ) {
// Forward "merging" (forward delete) should be equal pressing
// Backspace from the next item. If the next item is not at the top
// level, the item should be outdented instead of merged.
if ( getBlockOrder( clientId ).length ) {
// Should be implemented in `./use-backspace`.
return;
}

const nextBlockClientId = getNext( clientId );
if ( nextBlockClientId ) {
registry.batch( () => {
moveBlocksToPosition(
getBlockOrder( nextBlockClientId ),
nextBlockClientId,
getPreviousBlockClientId( nextBlockClientId )
);
mergeBlocks( clientId, nextBlockClientId );
} );
}
} else {
// Merging is only done from the top level. For lowel levels, the
// list item is outdented instead.
const previousBlockClientId = getPreviousBlockClientId( clientId );
if ( previousBlockClientId ) {
const trailingClientId = getTrailing( previousBlockClientId );
registry.batch( () => {
moveBlocksToPosition(
getBlockOrder( clientId ),
clientId,
previousBlockClientId
);
mergeBlocks( trailingClientId, clientId );
} );
}
}
};
}

0 comments on commit 4ed1c11

Please sign in to comment.