-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[List v2]: Handle Enter in empty list items (#39858)
* [List v2]: Handle Enter in empty list items * separate hooks in different files
- Loading branch information
1 parent
b79e164
commit bd6853d
Showing
6 changed files
with
309 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
createBlock, | ||
getDefaultBlockName, | ||
cloneBlock, | ||
} from '@wordpress/blocks'; | ||
import { useRef } from '@wordpress/element'; | ||
import { useRefEffect } from '@wordpress/compose'; | ||
import { ENTER } from '@wordpress/keycodes'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useOutdentListItem from './use-indent-list-item'; | ||
|
||
export default function useEnter( props ) { | ||
const { replaceBlocks } = useDispatch( blockEditorStore ); | ||
const { | ||
getBlock, | ||
getBlockRootClientId, | ||
getBlockParents, | ||
getBlockIndex, | ||
} = useSelect( blockEditorStore ); | ||
const propsRef = useRef( props ); | ||
propsRef.current = props; | ||
const [ canOutdent, outdentListItem ] = useOutdentListItem( | ||
propsRef.current.clientId | ||
); | ||
return useRefEffect( | ||
( element ) => { | ||
function onKeyDown( event ) { | ||
if ( event.defaultPrevented || event.keyCode !== ENTER ) { | ||
return; | ||
} | ||
const { content, clientId } = propsRef.current; | ||
if ( content.length ) { | ||
return; | ||
} | ||
event.preventDefault(); | ||
if ( canOutdent ) { | ||
outdentListItem(); | ||
return; | ||
} | ||
// Here we are in top level list so we need to split. | ||
const blockRootClientId = getBlockRootClientId( clientId ); | ||
const blockParents = getBlockParents( clientId ); | ||
const topParentListBlockClientId = blockParents[ 0 ]; | ||
const topParentListBlock = getBlock( | ||
topParentListBlockClientId | ||
); | ||
const blockIndex = getBlockIndex( clientId ); | ||
const head = cloneBlock( { | ||
...topParentListBlock, | ||
innerBlocks: topParentListBlock.innerBlocks.slice( | ||
0, | ||
blockIndex | ||
), | ||
} ); | ||
const middle = createBlock( getDefaultBlockName() ); | ||
// Last list item might contain a `list` block innerBlock | ||
// In that case append remaining innerBlocks blocks. | ||
const after = [ | ||
...( topParentListBlock.innerBlocks[ blockIndex ] | ||
.innerBlocks[ 0 ]?.innerBlocks || [] ), | ||
...topParentListBlock.innerBlocks.slice( blockIndex + 1 ), | ||
]; | ||
const tail = after.length | ||
? [ | ||
cloneBlock( { | ||
...topParentListBlock, | ||
innerBlocks: after, | ||
} ), | ||
] | ||
: []; | ||
replaceBlocks( | ||
blockRootClientId, | ||
[ head, middle, ...tail ], | ||
1, | ||
0 | ||
); | ||
} | ||
|
||
element.addEventListener( 'keydown', onKeyDown ); | ||
return () => { | ||
element.removeEventListener( 'keydown', onKeyDown ); | ||
}; | ||
}, | ||
[ canOutdent ] | ||
); | ||
} |
Oops, something went wrong.