Skip to content

Commit

Permalink
List block v2: Fixes an issue where pressing enter deletes innerblock…
Browse files Browse the repository at this point in the history
…s. (#41109)
  • Loading branch information
jorgefilipecosta authored May 17, 2022
1 parent 90bc0ec commit f83dd10
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
11 changes: 3 additions & 8 deletions packages/block-library/src/list-item/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
BlockControls,
} from '@wordpress/block-editor';
import { isRTL, __ } from '@wordpress/i18n';
import { createBlock } from '@wordpress/blocks';
import { ToolbarButton } from '@wordpress/components';
import {
formatOutdent,
Expand All @@ -27,6 +26,7 @@ import {
useSpace,
useIndentListItem,
useOutdentListItem,
useSplit,
} from './hooks';

function IndentUI( { clientId } ) {
Expand Down Expand Up @@ -54,7 +54,6 @@ function IndentUI( { clientId } ) {
}

export default function ListItemEdit( {
name,
attributes,
setAttributes,
mergeBlocks,
Expand All @@ -69,6 +68,7 @@ export default function ListItemEdit( {
const useEnterRef = useEnter( { content, clientId } );
const useBackspaceRef = useBackspace( { clientId } );
const useSpaceRef = useSpace( clientId );
const onSplit = useSplit( clientId );
return (
<>
<li { ...innerBlocksProps }>
Expand All @@ -86,12 +86,7 @@ export default function ListItemEdit( {
value={ content }
aria-label={ __( 'List text' ) }
placeholder={ placeholder || __( 'List' ) }
onSplit={ ( value ) => {
return createBlock( name, {
...attributes,
content: value,
} );
} }
onSplit={ onSplit }
onMerge={ mergeBlocks }
onReplace={ onReplace }
/>
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 @@ -3,3 +3,4 @@ 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';
24 changes: 24 additions & 0 deletions packages/block-library/src/list-item/hooks/use-split.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* WordPress dependencies
*/
import { useCallback } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { cloneBlock } from '@wordpress/blocks';

export default function useSplit( clientId ) {
const { getBlock } = useSelect( blockEditorStore );
return useCallback(
( value, isAfterOriginal ) => {
const block = getBlock( clientId );
return cloneBlock(
block,
{
content: value,
},
isAfterOriginal ? [] : block.innerBlocks
);
},
[ clientId, getBlock ]
);
}

0 comments on commit f83dd10

Please sign in to comment.