-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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]: Handle Enter in empty list items #39858
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be indent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? It's working as expected in the video, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me it was indenting for some reason instead of outdenting.