-
Notifications
You must be signed in to change notification settings - Fork 384
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
Add keyboard options to blocks #3400
Changes from 3 commits
7f3ae2e
0e9bbe3
3e3fff0
314c4d1
0d0698e
d23e72f
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 | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,96 @@ | ||||||
/** | ||||||
* WordPress dependencies | ||||||
*/ | ||||||
import { withDispatch } from '@wordpress/data'; | ||||||
import { createHigherOrderComponent } from '@wordpress/compose'; | ||||||
import { UP, DOWN, RIGHT, LEFT, DELETE } from '@wordpress/keycodes'; | ||||||
/** | ||||||
* Internal dependencies | ||||||
*/ | ||||||
import { ALLOWED_CHILD_BLOCKS, ALLOWED_MOVABLE_BLOCKS } from '../../constants'; | ||||||
|
||||||
const applyWithDispatch = withDispatch( ( dispatch, props, { select } ) => { | ||||||
const { isReordering } = select( 'amp/story' ); | ||||||
const { getSelectedBlock } = select( 'core/block-editor' ); | ||||||
const { updateBlockAttributes, removeBlock } = dispatch( 'core/block-editor' ); | ||||||
|
||||||
const onKeyPress = ( event ) => { | ||||||
const { keyCode, target } = event; | ||||||
const { classList } = target; | ||||||
const selectedBlock = getSelectedBlock(); | ||||||
|
||||||
if ( ! selectedBlock ) { | ||||||
return; | ||||||
} | ||||||
|
||||||
if ( classList.contains( 'editor-rich-text__editable' ) && classList.contains( 'is-selected' ) ) { | ||||||
return; | ||||||
} | ||||||
|
||||||
let top = 0; | ||||||
let left = 0; | ||||||
switch ( keyCode ) { | ||||||
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. Needs some testing, but it would be nice if one could move in bigger steps when holding down an additional key, e.g. ctrl, alt, or cmd. If I am not mistaken this kind of behavior is common in other applications when moving elements with the keyboard |
||||||
case UP: | ||||||
top = -1; | ||||||
break; | ||||||
case DOWN: | ||||||
top = 1; | ||||||
break; | ||||||
case RIGHT: | ||||||
left = 1; | ||||||
break; | ||||||
case LEFT: | ||||||
left = -1; | ||||||
break; | ||||||
case DELETE: | ||||||
removeBlock( selectedBlock.clientId ); | ||||||
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. What about the existing keyboard shortcut for removing blocks? 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. See #3280 |
||||||
return; | ||||||
default: | ||||||
return; | ||||||
} | ||||||
event.preventDefault(); | ||||||
if ( ALLOWED_MOVABLE_BLOCKS.includes( selectedBlock.name ) ) { | ||||||
const newPositionTop = selectedBlock.attributes.positionTop + top; | ||||||
const newPositionLeft = selectedBlock.attributes.positionLeft + left; | ||||||
updateBlockAttributes( selectedBlock.clientId, { | ||||||
positionTop: newPositionTop, | ||||||
positionLeft: newPositionLeft, | ||||||
} ); | ||||||
} | ||||||
}; | ||||||
|
||||||
return { | ||||||
isReordering, | ||||||
onKeyPress, | ||||||
}; | ||||||
} ); | ||||||
|
||||||
/** | ||||||
* Higher-order component that adds right click handler to each inner block. | ||||||
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.
Suggested change
|
||||||
* | ||||||
* @return {Function} Higher-order component. | ||||||
*/ | ||||||
export default createHigherOrderComponent( | ||||||
( BlockEdit ) => { | ||||||
return applyWithDispatch( ( props ) => { | ||||||
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. Could use hooks here |
||||||
const { name, onKeyPress, isReordering } = props; | ||||||
const isPageBlock = 'amp/amp-story-page' === name; | ||||||
// Add for page block and inner blocks. | ||||||
if ( ! isPageBlock && ! ALLOWED_CHILD_BLOCKS.includes( name ) ) { | ||||||
return <BlockEdit { ...props } />; | ||||||
} | ||||||
|
||||||
// Not relevant for reordering. | ||||||
if ( isReordering() ) { | ||||||
return <BlockEdit { ...props } />; | ||||||
} | ||||||
|
||||||
return ( | ||||||
<div onKeyDown={ onKeyPress }> | ||||||
<BlockEdit { ...props } /> | ||||||
</div> | ||||||
); | ||||||
} ); | ||||||
}, | ||||||
'withKeyboardNavigationHandler' | ||||||
); |
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.
Isn't there a
isSelected
prop?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.
Not that I can see
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.
Odd 🤔
I thought it does:
https://github.com/WordPress/gutenberg/blob/29ad4d8740b04b5bf094b6901cccd71552767cb3/packages/block-editor/src/components/block-list/block.js#L459-L470
https://github.com/WordPress/gutenberg/blob/29ad4d8740b04b5bf094b6901cccd71552767cb3/packages/block-editor/src/components/block-edit/index.js#L34-L43
https://github.com/WordPress/gutenberg/blob/29ad4d8740b04b5bf094b6901cccd71552767cb3/packages/block-editor/src/components/block-edit/edit.js#L12-L34