-
Notifications
You must be signed in to change notification settings - Fork 385
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 all 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,118 @@ | ||||||
/** | ||||||
* WordPress dependencies | ||||||
*/ | ||||||
import { withDispatch } from '@wordpress/data'; | ||||||
import { createHigherOrderComponent } from '@wordpress/compose'; | ||||||
import { UP, DOWN, RIGHT, LEFT } from '@wordpress/keycodes'; | ||||||
import { KeyboardShortcuts } from '@wordpress/components'; | ||||||
/** | ||||||
* 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 selectedBlock = getSelectedBlock(); | ||||||
|
||||||
const onMoveBlock = ( event ) => { | ||||||
const { keyCode, target } = event; | ||||||
const { classList } = target; | ||||||
|
||||||
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; | ||||||
default: | ||||||
break; | ||||||
} | ||||||
|
||||||
if ( ALLOWED_MOVABLE_BLOCKS.includes( selectedBlock.name ) && ( left || top ) ) { | ||||||
event.preventDefault(); | ||||||
const newPositionTop = selectedBlock.attributes.positionTop + top; | ||||||
const newPositionLeft = selectedBlock.attributes.positionLeft + left; | ||||||
updateBlockAttributes( selectedBlock.clientId, { | ||||||
positionTop: newPositionTop, | ||||||
positionLeft: newPositionLeft, | ||||||
} ); | ||||||
} | ||||||
}; | ||||||
|
||||||
const deleteSelectedBlocks = ( event ) => { | ||||||
const { target } = event; | ||||||
const { classList } = target; | ||||||
if ( ! selectedBlock ) { | ||||||
return; | ||||||
} | ||||||
if ( classList.contains( 'editor-rich-text__editable' ) && classList.contains( 'is-selected' ) ) { | ||||||
return; | ||||||
} | ||||||
event.preventDefault(); | ||||||
removeBlock( selectedBlock.clientId ); | ||||||
}; | ||||||
|
||||||
return { | ||||||
isReordering, | ||||||
onMoveBlock, | ||||||
deleteSelectedBlocks, | ||||||
}; | ||||||
} ); | ||||||
|
||||||
/** | ||||||
* 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, onMoveBlock, isReordering, deleteSelectedBlocks } = 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 } />; | ||||||
} | ||||||
|
||||||
const shortcuts = { | ||||||
up: onMoveBlock, | ||||||
right: onMoveBlock, | ||||||
down: onMoveBlock, | ||||||
left: onMoveBlock, | ||||||
backspace: deleteSelectedBlocks, | ||||||
del: deleteSelectedBlocks, | ||||||
}; | ||||||
|
||||||
return ( | ||||||
<KeyboardShortcuts shortcuts={ shortcuts } event="keyup"> | ||||||
<BlockEdit { ...props } /> | ||||||
</KeyboardShortcuts> | ||||||
); | ||||||
} ); | ||||||
}, | ||||||
'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