Skip to content

Commit

Permalink
Button block: support double enter to skip to default block (#56134)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Nov 16, 2023
1 parent 8fa043b commit eb0a6f5
Showing 1 changed file with 70 additions and 4 deletions.
74 changes: 70 additions & 4 deletions packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@ import {
__experimentalGetSpacingClassesAndStyles as useSpacingProps,
__experimentalLinkControl as LinkControl,
__experimentalGetElementClassName,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { displayShortcut, isKeyboardEvent } from '@wordpress/keycodes';
import { displayShortcut, isKeyboardEvent, ENTER } from '@wordpress/keycodes';
import { link, linkOff } from '@wordpress/icons';
import { createBlock } from '@wordpress/blocks';
import { useMergeRefs } from '@wordpress/compose';
import {
createBlock,
cloneBlock,
getDefaultBlockName,
} from '@wordpress/blocks';
import { useMergeRefs, useRefEffect } from '@wordpress/compose';
import { useSelect, useDispatch } from '@wordpress/data';

const LINK_SETTINGS = [
...LinkControl.DEFAULT_LINK_SETTINGS,
Expand All @@ -47,6 +53,62 @@ const LINK_SETTINGS = [
},
];

function useEnter( props ) {
const { replaceBlocks, selectionChange } = useDispatch( blockEditorStore );
const { getBlock, getBlockRootClientId, getBlockIndex } =
useSelect( blockEditorStore );
const propsRef = useRef( props );
propsRef.current = props;
return useRefEffect( ( element ) => {
function onKeyDown( event ) {
if ( event.defaultPrevented || event.keyCode !== ENTER ) {
return;
}
const { content, clientId } = propsRef.current;
if ( content.length ) {
return;
}
event.preventDefault();
const topParentListBlock = getBlock(
getBlockRootClientId( clientId )
);
const blockIndex = getBlockIndex( clientId );
const head = cloneBlock( {
...topParentListBlock,
innerBlocks: topParentListBlock.innerBlocks.slice(
0,
blockIndex
),
} );
const middle = createBlock( getDefaultBlockName() );
const after = topParentListBlock.innerBlocks.slice(
blockIndex + 1
);
const tail = after.length
? [
cloneBlock( {
...topParentListBlock,
innerBlocks: after,
} ),
]
: [];
replaceBlocks(
topParentListBlock.clientId,
[ head, middle, ...tail ],
1
);
// We manually change the selection here because we are replacing
// a different block than the selected one.
selectionChange( middle.clientId );
}

element.addEventListener( 'keydown', onKeyDown );
return () => {
element.removeEventListener( 'keydown', onKeyDown );
};
}, [] );
}

function WidthPanel( { selectedWidth, setAttributes } ) {
function handleChange( newWidth ) {
// Check if we are toggling the width off
Expand Down Expand Up @@ -88,6 +150,7 @@ function ButtonEdit( props ) {
isSelected,
onReplace,
mergeBlocks,
clientId,
} = props;
const {
tagName,
Expand Down Expand Up @@ -164,6 +227,9 @@ function ButtonEdit( props ) {
[ url, opensInNewTab, nofollow ]
);

const useEnterRef = useEnter( { content: text, clientId } );
const mergedRef = useMergeRefs( [ useEnterRef, richTextRef ] );

return (
<>
<div
Expand All @@ -175,7 +241,7 @@ function ButtonEdit( props ) {
} ) }
>
<RichText
ref={ richTextRef }
ref={ mergedRef }
aria-label={ __( 'Button text' ) }
placeholder={ placeholder || __( 'Add text…' ) }
value={ text }
Expand Down

0 comments on commit eb0a6f5

Please sign in to comment.