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

Writing flow: select next block on Enter key #63484

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
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
@@ -1,10 +1,10 @@
/**
* WordPress dependencies
*/
import { isTextField } from '@wordpress/dom';
import { ENTER, BACKSPACE, DELETE } from '@wordpress/keycodes';
import { useSelect, useDispatch } from '@wordpress/data';
import { useRefEffect } from '@wordpress/compose';
import { getDefaultBlockName } from '@wordpress/blocks';

/**
* Internal dependencies
Expand All @@ -20,9 +20,16 @@ import { store as blockEditorStore } from '../../../store';
* @param {string} clientId Block client ID.
*/
export function useEventHandlers( { clientId, isSelected } ) {
const { getBlockRootClientId, getBlockIndex } =
useSelect( blockEditorStore );
const { insertAfterBlock, removeBlock } = useDispatch( blockEditorStore );
const {
getBlockRootClientId,
getBlockIndex,
canInsertBlockType,
getNextBlockClientId,
getBlockOrder,
getBlockEditingMode,
} = useSelect( blockEditorStore );
const { insertAfterBlock, removeBlock, selectBlock } =
useDispatch( blockEditorStore );

return useRefEffect(
( node ) => {
Expand All @@ -42,6 +49,10 @@ export function useEventHandlers( { clientId, isSelected } ) {
function onKeyDown( event ) {
const { keyCode, target } = event;

if ( event.defaultPrevented ) {
return;
}

if (
keyCode !== ENTER &&
keyCode !== BACKSPACE &&
Expand All @@ -50,14 +61,50 @@ export function useEventHandlers( { clientId, isSelected } ) {
return;
}

if ( target !== node || isTextField( target ) ) {
if ( target !== node ) {
return;
}

event.preventDefault();

if ( keyCode === ENTER ) {
insertAfterBlock( clientId );
const rootClientId = getBlockRootClientId( clientId );
if (
canInsertBlockType(
getDefaultBlockName(),
rootClientId
)
) {
insertAfterBlock( clientId );
} else {
function getNextClientId( id ) {
let nextClientId = null;

while (
typeof id === 'string' &&
! ( nextClientId = getNextBlockClientId( id ) )
) {
id = getBlockRootClientId( id );
}

return nextClientId;
}

let nextClientId =
getBlockOrder( clientId )[ 0 ] ??
getNextClientId( clientId );

while (
nextClientId &&
getBlockEditingMode( nextClientId ) === 'disabled'
) {
nextClientId = getNextClientId( nextClientId );
}

if ( nextClientId ) {
selectBlock( nextClientId );
}
}
} else {
removeBlock( clientId );
}
Expand Down
10 changes: 1 addition & 9 deletions packages/block-library/src/site-title/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@ import {
HeadingLevelDropdown,
} from '@wordpress/block-editor';
import { ToggleControl, PanelBody } from '@wordpress/components';
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
import { decodeEntities } from '@wordpress/html-entities';

const HEADING_LEVELS = [ 0, 1, 2, 3, 4, 5, 6 ];

export default function SiteTitleEdit( {
attributes,
setAttributes,
insertBlocksAfter,
} ) {
export default function SiteTitleEdit( { attributes, setAttributes } ) {
const { level, textAlign, isLink, linkTarget } = attributes;
const { canUserEdit, title } = useSelect( ( select ) => {
const { canUser, getEntityRecord, getEditedEntityRecord } =
Expand Down Expand Up @@ -67,9 +62,6 @@ export default function SiteTitleEdit( {
onChange={ setTitle }
allowedFormats={ [] }
disableLineBreaks
__unstableOnSplitAtEnd={ () =>
insertBlocksAfter( createBlock( getDefaultBlockName() ) )
}
/>
</TagName>
) : (
Expand Down
Loading