Skip to content
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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' ) ) {
Copy link
Collaborator

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?

Copy link
Contributor Author

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

attributes: {ordered: false, values: "", addedAttributes: 0, opacity: 100, positionTop: 30, …}
clientId: "b1540913-690e-4e23-8a0a-8f531c77301e"
innerBlocks: []
isValid: true
name: "core/list"

Copy link
Collaborator

return;
}

let top = 0;
let left = 0;
switch ( keyCode ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Higher-order component that adds right click handler to each inner block.
* Higher-order component that adds a keyboard navigation handler to each inner block.

*
* @return {Function} Higher-order component.
*/
export default createHigherOrderComponent(
( BlockEdit ) => {
return applyWithDispatch( ( props ) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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'
);
1 change: 1 addition & 0 deletions assets/src/stories-editor/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export { default as withCroppedFeaturedImage } from './with-cropped-featured-ima
export { default as withHasSelectedInnerBlock } from './higher-order/with-has-selected-inner-block';
export { default as withPageNumber } from './higher-order/with-page-number';
export { default as withRightClickHandler } from './higher-order/with-right-click-handler';
export { default as withKeyboardNavigation } from './higher-order/with-keyboard-navigation-handler';
export { default as withStoryFeaturedImageNotice } from './higher-order/with-story-featured-image-notice';
export { default as withEditFeaturedImage } from './with-edit-featured-image';
export { default as withCustomVideoBlockEdit } from './with-custom-video-block-edit';
Expand Down
2 changes: 2 additions & 0 deletions assets/src/stories-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
withCallToActionValidation,
withCroppedFeaturedImage,
withRightClickHandler,
withKeyboardNavigation,
} from './components';
import {
maybeEnqueueFontStyle,
Expand Down Expand Up @@ -317,6 +318,7 @@ addFilter( 'blocks.registerBlockType', 'ampStoryEditorBlocks/deprecateCoreBlocks
addFilter( 'editor.BlockEdit', 'ampStoryEditorBlocks/addStorySettings', withAmpStorySettings );
addFilter( 'editor.BlockEdit', 'ampStoryEditorBlocks/addPageNumber', withPageNumber );
addFilter( 'editor.BlockEdit', 'ampStoryEditorBlocks/rightClickHandler', withRightClickHandler );
addFilter( 'editor.BlockEdit', 'ampStoryEditorBlocks/keyboardHandler', withKeyboardNavigation );
addFilter( 'editor.BlockEdit', 'ampStoryEditorBlocks/addEditFeaturedImage', withEditFeaturedImage );
addFilter( 'editor.BlockEdit', 'ampEditorBlocks/addVideoBlockPreview', withCustomVideoBlockEdit, 9 );
addFilter( 'editor.PostFeaturedImage', 'ampStoryEditorBlocks/addFeaturedImageNotice', withStoryFeaturedImageNotice );
Expand Down