-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Block editor: add a keyboard shortcut to create group from the selected blocks #46972
Changes from 9 commits
f329fa4
b9a333d
e942a9d
0133889
7b16ecd
cb958e5
ff9aea7
8cf7f34
4b69935
20866f8
31d9f7f
170be3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,11 @@ import classnames from 'classnames'; | |
/** | ||
* WordPress dependencies | ||
*/ | ||
import { hasBlockSupport } from '@wordpress/blocks'; | ||
import { | ||
hasBlockSupport, | ||
switchToBlockType, | ||
store as blocksStore, | ||
} from '@wordpress/blocks'; | ||
import { | ||
__experimentalTreeGridCell as TreeGridCell, | ||
__experimentalTreeGridItem as TreeGridItem, | ||
|
@@ -25,6 +29,7 @@ import { __ } from '@wordpress/i18n'; | |
import { BACKSPACE, DELETE } from '@wordpress/keycodes'; | ||
import isShallowEqual from '@wordpress/is-shallow-equal'; | ||
import { __unstableUseShortcutEventMatch as useShortcutEventMatch } from '@wordpress/keyboard-shortcuts'; | ||
import { speak } from '@wordpress/a11y'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -85,6 +90,7 @@ function ListViewBlock( { | |
toggleBlockHighlight, | ||
duplicateBlocks, | ||
multiSelect, | ||
replaceBlocks, | ||
removeBlocks, | ||
insertAfterBlock, | ||
insertBeforeBlock, | ||
|
@@ -100,7 +106,9 @@ function ListViewBlock( { | |
getBlockParents, | ||
getBlocksByClientId, | ||
canRemoveBlocks, | ||
isGroupable, | ||
} = useSelect( blockEditorStore ); | ||
const { getGroupingBlockName } = useSelect( blocksStore ); | ||
|
||
const blockInformation = useBlockDisplayInformation( clientId ); | ||
|
||
|
@@ -324,6 +332,19 @@ function ListViewBlock( { | |
collapseAll(); | ||
// Expand all parents of the current block. | ||
expand( blockParents ); | ||
} else if ( isMatch( 'core/block-editor/group', event ) ) { | ||
const clientIds = getSelectedBlockClientIds(); | ||
if ( clientIds.length > 1 && isGroupable( clientIds ) ) { | ||
event.preventDefault(); | ||
const blocks = getBlocksByClientId( clientIds ); | ||
const groupingBlockName = getGroupingBlockName(); | ||
const newBlocks = switchToBlockType( | ||
blocks, | ||
groupingBlockName | ||
); | ||
replaceBlocks( clientIds, newBlocks ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To fix the issue of the canvas stealing focus that Alex mentioned, we might be able to use a similar approach as in the insert before and after keyboard shortcuts above, where we grab the selected blocks after calling
|
||
speak( __( 'Selected blocks are grouped.' ) ); | ||
} | ||
} | ||
} | ||
|
||
|
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.
In the List View, rather than using the selected block client ids, I think it's generally better to use
const { blocksToUpdate } = getBlocksToUpdate();
as used in the insert before and after keyboard shortcuts above. This will ensure that if the user is focused on a block that isn't part of the selection, then the action is performed on the currently focused block. If the focused block is part of the selection, then the whole selection is used.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.
I just realised this might not be particularly clear as to why it matters. Here's a quick example video of the behaviour as it is currently, where if I move up the list view to outside the selection, the keyboard shortcut acts on the selected blocks instead of the focused one:
2024-05-08.16.12.33.mp4