Skip to content

Commit

Permalink
adds selection persistence on sidebar tab switch
Browse files Browse the repository at this point in the history
  • Loading branch information
draganescu committed May 21, 2019
1 parent 8960509 commit 9e43f15
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
11 changes: 11 additions & 0 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,17 @@ export function clearSelectedBlock() {
};
}

/**
* Returns an action object used in restoring the previously cleared selected blocks.
*
* @return {Object} Action object.
*/
export function restoreSelectedBlock() {
return {
type: 'RESTORE_SELECTED_BLOCK',
};
}

/**
* Returns an action object that enables or disables block selection.
*
Expand Down
15 changes: 14 additions & 1 deletion packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,20 @@ const BLOCK_SELECTION_INITIAL_STATE = {
export function blockSelection( state = BLOCK_SELECTION_INITIAL_STATE, action ) {
switch ( action.type ) {
case 'CLEAR_SELECTED_BLOCK':
return BLOCK_SELECTION_INITIAL_STATE;
return {
...BLOCK_SELECTION_INITIAL_STATE,
previousSelection: {
start: state.start,
end: state.end,
isMultiSelecting: state.isMultiSelecting,
isEnabled: state.isEnabled,
initialPosition: state.initialPosition,
},
};
case 'RESTORE_SELECTED_BLOCK':
return {
...state.previousSelection,
};
case 'START_MULTI_SELECT':
if ( state.isMultiSelecting ) {
return state;
Expand Down
31 changes: 31 additions & 0 deletions packages/e2e-tests/specs/a11y.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import {
clickBlockAppender,
createNewPost,
pressKeyWithModifier,
} from '@wordpress/e2e-test-utils';
Expand Down Expand Up @@ -29,6 +30,36 @@ describe( 'a11y', () => {
expect( isFocusedToggle ).toBe( true );
} );

it( 'checks persistent selection', async () => {
await clickBlockAppender();
await page.keyboard.type( 'Testing editor selection persistence' );

await page.keyboard.down( 'Control' );
await page.keyboard.down( 'Shift' );
await page.keyboard.press( '`' );
await page.keyboard.press( '`' );
await page.keyboard.up( 'Control' );
await page.keyboard.up( 'Shift' );

await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Space' );

let isFocusedParagraphBlock = await page.$eval( ':focus', ( focusedElement ) => {
return focusedElement.classList.contains( 'block-editor-rich-text__editable' );
} );

expect( isFocusedParagraphBlock ).toBe( false );

await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Space' );

isFocusedParagraphBlock = await page.$eval( ':focus', ( focusedElement ) => {
return focusedElement.classList.contains( 'block-editor-rich-text__editable' );
} );

expect( isFocusedParagraphBlock ).toBe( true );
} );

it( 'constrains focus to a modal when tabbing', async () => {
// Open keyboard help modal.
await pressKeyWithModifier( 'access', 'h' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ const SettingsHeader = ( { openDocumentSettings, openBlockSettings, sidebarName

export default withDispatch( ( dispatch ) => {
const { openGeneralSidebar } = dispatch( 'core/edit-post' );
const { clearSelectedBlock } = dispatch( 'core/block-editor' );
const { clearSelectedBlock, restoreSelectedBlock } = dispatch( 'core/block-editor' );

return {
openDocumentSettings() {
openGeneralSidebar( 'edit-post/document' );
clearSelectedBlock();
},
openBlockSettings() {
openGeneralSidebar( 'edit-post/block' );
restoreSelectedBlock();
},
};
} )( SettingsHeader );

0 comments on commit 9e43f15

Please sign in to comment.