Skip to content
Merged
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
1 change: 1 addition & 0 deletions packages/block-library/src/column/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
}
},
"supports": {
"__experimentalOnEnter": true,
"anchor": true,
"reusable": false,
"html": false,
Expand Down
52 changes: 41 additions & 11 deletions packages/block-library/src/paragraph/use-enter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { useRefEffect } from '@wordpress/compose';
import { ENTER } from '@wordpress/keycodes';
import { useSelect, useDispatch, useRegistry } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { hasBlockSupport, createBlock } from '@wordpress/blocks';
import {
hasBlockSupport,
createBlock,
getDefaultBlockName,
} from '@wordpress/blocks';

export function useOnEnter( props ) {
const { batch } = useRegistry();
Expand All @@ -23,6 +27,7 @@ export function useOnEnter( props ) {
getBlockName,
getBlock,
getNextBlockClientId,
canInsertBlockType,
} = useSelect( blockEditorStore );
const propsRef = useRef( props );
propsRef.current = props;
Expand Down Expand Up @@ -56,22 +61,47 @@ export function useOnEnter( props ) {
}

const order = getBlockOrder( wrapperClientId );

event.preventDefault();

const position = order.indexOf( clientId );

// If it is the last block, exit.
if ( position === order.length - 1 ) {
moveBlocksToPosition(
[ clientId ],
wrapperClientId,
getBlockRootClientId( wrapperClientId ),
getBlockIndex( wrapperClientId ) + 1
);
let newWrapperClientId = wrapperClientId;

while (
! canInsertBlockType(
getBlockName( clientId ),
getBlockRootClientId( newWrapperClientId )
)
) {
newWrapperClientId =
getBlockRootClientId( newWrapperClientId );
}

if ( typeof newWrapperClientId === 'string' ) {
event.preventDefault();
moveBlocksToPosition(
[ clientId ],
wrapperClientId,
getBlockRootClientId( newWrapperClientId ),
getBlockIndex( newWrapperClientId ) + 1
);
}
return;
}

const defaultBlockName = getDefaultBlockName();

if (
! canInsertBlockType(
defaultBlockName,
getBlockRootClientId( wrapperClientId )
)
) {
return;
}

event.preventDefault();

// If it is in the middle, split the block in two.
const wrapperBlock = getBlock( wrapperClientId );
batch( () => {
Expand All @@ -87,7 +117,7 @@ export function useOnEnter( props ) {
wrapperBlock.innerBlocks.slice( position + 1 )
);
insertBlock(
createBlock( 'core/paragraph' ),
createBlock( defaultBlockName ),
blockIndex + 1,
getBlockRootClientId( wrapperClientId ),
true
Expand Down
118 changes: 118 additions & 0 deletions test/e2e/specs/editor/blocks/columns.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,122 @@ test.describe( 'Columns', () => {
},
] );
} );

test( 'can exit on Enter', async ( { editor, page } ) => {
await editor.insertBlock( {
name: 'core/columns',
innerBlocks: [
{
name: 'core/column',
innerBlocks: [
{
name: 'core/paragraph',
attributes: { content: '1' },
},
],
},
{
name: 'core/column',
},
],
} );

await editor.selectBlocks(
editor.canvas.locator( 'role=document[name="Paragraph block"i]' )
);
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.press( 'Enter' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( '2' );

await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/columns',
innerBlocks: [
{
name: 'core/column',
innerBlocks: [
{
name: 'core/paragraph',
attributes: { content: '1' },
},
],
},
{
name: 'core/column',
},
],
},
{
name: 'core/paragraph',
attributes: { content: '2' },
},
] );
} );

test( 'should not split in middle', async ( { editor, page } ) => {
await editor.insertBlock( {
name: 'core/columns',
innerBlocks: [
{
name: 'core/column',
innerBlocks: [
{
name: 'core/paragraph',
attributes: { content: '1' },
},
{
name: 'core/paragraph',
attributes: { content: '2' },
},
],
},
{
name: 'core/column',
},
],
} );

await editor.selectBlocks(
editor.canvas.locator(
'role=document[name="Paragraph block"i] >> text="1"'
)
);
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.press( 'Enter' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( '3' );

await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/columns',
innerBlocks: [
{
name: 'core/column',
innerBlocks: [
{
name: 'core/paragraph',
attributes: { content: '1' },
},
{
name: 'core/paragraph',
attributes: { content: '' },
},
{
name: 'core/paragraph',
attributes: { content: '3' },
},
{
name: 'core/paragraph',
attributes: { content: '2' },
},
],
},
{
name: 'core/column',
},
],
},
] );
} );
} );