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

Escape as Select/Edit mode Toggle #58637

Merged
merged 4 commits into from
Feb 5, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ function BlockSelectionButton( { clientId, rootClientId } ) {
const isEnter = keyCode === ENTER;
const isSpace = keyCode === SPACE;
const isShift = event.shiftKey;
if ( isEscape && editorMode === 'navigation' ) {
setNavigationMode( false );
event.preventDefault();
return;
}

if ( keyCode === BACKSPACE || keyCode === DELETE ) {
removeBlock( clientId );
Expand Down
14 changes: 2 additions & 12 deletions packages/block-editor/src/components/block-tools/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export default function BlockTools( {
removeBlocks,
insertAfterBlock,
insertBeforeBlock,
clearSelectedBlock,
selectBlock,
moveBlocksUp,
moveBlocksDown,
Expand Down Expand Up @@ -157,21 +156,12 @@ export default function BlockTools( {
}

const clientIds = getSelectedBlockClientIds();
if ( clientIds.length ) {
if ( clientIds.length > 1 ) {
event.preventDefault();

// If there is more than one block selected, select the first
// block so that focus is directed back to the beginning of the selection.
// In effect, to the user this feels like deselecting the multi-selection.
if ( clientIds.length > 1 ) {
selectBlock( clientIds[ 0 ] );
} else {
clearSelectedBlock();
}
event.target.ownerDocument.defaultView
.getSelection()
.removeAllRanges();
__unstableContentRef?.current.focus();
selectBlock( clientIds[ 0 ] );
}
}
}
Expand Down
40 changes: 5 additions & 35 deletions test/e2e/specs/editor/various/multi-block-selection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ test.describe( 'Multi-block selection', () => {
pageUtils,
multiBlockSelectionUtils,
} ) => {
for ( let i = 1; i <= 2; i += 1 ) {
for ( let i = 1; i <= 3; i += 1 ) {
await editor.insertBlock( {
name: 'core/paragraph',
attributes: { content: `${ i }` },
Expand All @@ -232,14 +232,13 @@ test.describe( 'Multi-block selection', () => {

await expect
.poll( multiBlockSelectionUtils.getSelectedFlatIndices )
.toEqual( [ 1, 2 ] );
.toEqual( [ 1, 2, 3 ] );

await page.keyboard.press( 'Escape' );

// FIXME: This doesn't seem to work anymore.
// await expect
// .poll( multiBlockSelectionUtils.getSelectedFlatIndices )
// .toEqual( [] );
await expect
.poll( multiBlockSelectionUtils.getSelectedFlatIndices )
.toEqual( [ 1 ] );
} );

test( 'should select with shift + click', async ( {
Expand Down Expand Up @@ -877,35 +876,6 @@ test.describe( 'Multi-block selection', () => {
] );
} );

test( 'should select all from empty selection', async ( {
page,
editor,
pageUtils,
multiBlockSelectionUtils,
} ) => {
for ( let i = 1; i <= 2; i += 1 ) {
await editor.insertBlock( {
name: 'core/paragraph',
attributes: { content: `${ i }` },
} );
}

// Clear the selected block.
await page.keyboard.press( 'Escape' );
await page.keyboard.press( 'Escape' );

await expect
.poll( multiBlockSelectionUtils.getSelectedBlocks )
.toEqual( [] );

await pageUtils.pressKeys( 'primary+a' );

await page.keyboard.press( 'Backspace' );

// Expect both paragraphs to be deleted.
await expect.poll( editor.getBlocks ).toEqual( [] );
} );

test( 'should select title if the cursor is on title', async ( {
editor,
pageUtils,
Expand Down
20 changes: 12 additions & 8 deletions test/e2e/specs/editor/various/writing-flow.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -927,28 +927,32 @@ test.describe( 'Writing Flow (@firefox, @webkit)', () => {
<!-- /wp:table -->` );
} );

test( 'should unselect all blocks when hitting double escape', async ( {
test( 'escape should toggle between edit and navigation modes', async ( {
page,
writingFlowUtils,
} ) => {
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'Random Paragraph' );

await expect
.poll( writingFlowUtils.getActiveBlockName )
.toBe( 'core/paragraph' );

// First escape enters navigaiton mode.
// First escape enters navigation mode.
await page.keyboard.press( 'Escape' );
const navigationButton = page.getByLabel(
'Paragraph Block. Row 1. Random Paragraph'
);
await expect( navigationButton ).toBeVisible();
await expect
.poll( writingFlowUtils.getActiveBlockName )
.toBe( 'core/paragraph' );

// Second escape unselects the blocks.
// Second escape Toggles back to Edit Mode
await page.keyboard.press( 'Escape' );
await expect( navigationButton ).toBeHidden();
const blockToolbar = page.getByLabel( 'Block tools' );

await expect( blockToolbar ).toBeVisible();
await expect
.poll( writingFlowUtils.getActiveBlockName )
.toBe( undefined );
.toBe( 'core/paragraph' );
} );

// Checks for regressions of https://github.com/WordPress/gutenberg/issues/40091.
Expand Down
Loading