Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List v2: add forward delete #42564

Merged
merged 5 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 3 additions & 9 deletions packages/block-library/src/list-item/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { useMergeRefs } from '@wordpress/compose';
*/
import {
useEnter,
useBackspace,
useSpace,
useIndentListItem,
useOutdentListItem,
Expand All @@ -42,14 +41,14 @@ function IndentUI( { clientId } ) {
title={ __( 'Outdent' ) }
describedBy={ __( 'Outdent list item' ) }
disabled={ ! canOutdent }
onClick={ outdentListItem }
onClick={ () => outdentListItem() }
/>
<ToolbarButton
icon={ isRTL() ? formatIndentRTL : formatIndent }
title={ __( 'Indent' ) }
describedBy={ __( 'Indent list item' ) }
isDisabled={ ! canIndent }
onClick={ indentListItem }
onClick={ () => indentListItem() }
/>
</>
);
Expand All @@ -67,19 +66,14 @@ export default function ListItemEdit( {
allowedBlocks: [ 'core/list' ],
} );
const useEnterRef = useEnter( { content, clientId } );
const useBackspaceRef = useBackspace( { clientId } );
const useSpaceRef = useSpace( clientId );
const onSplit = useSplit( clientId );
const onMerge = useMerge( clientId );
return (
<>
<li { ...innerBlocksProps }>
<RichText
ref={ useMergeRefs( [
useEnterRef,
useBackspaceRef,
useSpaceRef,
] ) }
ref={ useMergeRefs( [ useEnterRef, useSpaceRef ] ) }
identifier="content"
tagName="div"
onChange={ ( nextContent ) =>
Expand Down
1 change: 0 additions & 1 deletion packages/block-library/src/list-item/hooks/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export { default as useOutdentListItem } from './use-outdent-list-item';
export { default as useIndentListItem } from './use-indent-list-item';
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';
51 changes: 0 additions & 51 deletions packages/block-library/src/list-item/hooks/use-backspace.js

This file was deleted.

69 changes: 56 additions & 13 deletions packages/block-library/src/list-item/hooks/use-merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@
import { useRegistry, useDispatch, useSelect } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import useOutdentListItem from './use-outdent-list-item';

import { name as listItemName } from '../block.json';

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

function getTrailing( id ) {
const order = getBlockOrder( id );
Expand All @@ -25,24 +34,56 @@ export default function useMerge( clientId ) {
return getTrailing( order[ order.length - 1 ] );
}

function getParentListItem( id ) {
const listId = getBlockRootClientId( id );
const parentListItem = getBlockRootClientId( listId );
if ( ! parentListItem ) return;
if ( getBlockName( parentListItem ) !== listItemName ) return;
return parentListItem;
}

/**
* Return the next list item with respect to the given list item. If none,
* return the next list item of the parent list item if it exists.
*
* @param {string} id A list item client ID.
* @return {string?} The client ID of the next list item.
*/
function _getNext( id ) {
const next = getNextBlockClientId( id );
if ( next ) return next;
const parentListItem = getParentListItem( id );
if ( ! parentListItem ) return;
return _getNext( parentListItem );
}

/**
* Given a client ID, return the client ID of the list item on the next
* line, regardless of indentation level.
*
* @param {string} id The client ID of the current list item.
* @return {string?} The client ID of the next list item.
*/
function getNext( id ) {
return (
getNextBlockClientId( id ) || getNext( getBlockRootClientId( id ) )
);
const order = getBlockOrder( id );

// If the list item does not have a nested list, return the next list
// item.
if ( ! order.length ) {
return _getNext( id );
}

// Get the first list item in the nested list.
return getBlockOrder( order[ 0 ] )[ 0 ];
}

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 ) {
if ( ! nextBlockClientId ) return;
if ( getParentListItem( nextBlockClientId ) ) {
outdentListItem( nextBlockClientId );
} else {
registry.batch( () => {
moveBlocksToPosition(
getBlockOrder( nextBlockClientId ),
Expand All @@ -56,7 +97,9 @@ export default function useMerge( clientId ) {
// Merging is only done from the top level. For lowel levels, the
// list item is outdented instead.
const previousBlockClientId = getPreviousBlockClientId( clientId );
if ( previousBlockClientId ) {
if ( getParentListItem( clientId ) ) {
outdentListItem( clientId );
} else if ( previousBlockClientId ) {
const trailingClientId = getTrailing( previousBlockClientId );
registry.batch( () => {
moveBlocksToPosition(
Expand Down
Loading