Skip to content

Commit

Permalink
Blocks: Reselect the current block if we click again on it
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Apr 28, 2017
1 parent 92be486 commit d95c03c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
8 changes: 3 additions & 5 deletions editor/modes/visual-editor/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,13 @@ class VisualEditorBlock extends wp.element.Component {
wrapperProps = settings.getEditWrapperProps( block.attributes );
}

// Disable reason: Each block can receive focus but must be able to contain
// block children. Tab keyboard navigation enabled by tabIndex assignment.
// Disable reason: Each block can be selected by clicking on it

/* eslint-disable jsx-a11y/no-static-element-interactions */
/* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role */
return (
<div
ref={ this.bindBlockNode }
tabIndex="0"
onFocus={ onSelect }
onClick={ onSelect }
onBlur={ this.maybeDeselect }
onKeyDown={ onStartTyping }
onMouseEnter={ onHover }
Expand Down
8 changes: 6 additions & 2 deletions editor/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,13 @@ export function selectedBlock( state = {}, action ) {
if ( ! action.selected ) {
return state.uid === action.uid ? {} : state;
}
return action.uid === state.uid
return action.uid === state.uid && ! state.typing
? state
: { uid: action.uid, typing: false, focus: {} };
: {
uid: action.uid,
typing: false,
focus: action.uid === state.uid ? state.focus : {}
};

case 'MOVE_BLOCK_UP':
case 'MOVE_BLOCK_DOWN':
Expand Down
19 changes: 17 additions & 2 deletions editor/test/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ describe( 'state', () => {
expect( state ).to.eql( { uid: 'kumquat', typing: false, focus: {} } );
} );

it( 'should not update the state if already selected', () => {
const original = deepFreeze( { uid: 'kumquat', typing: true, focus: {} } );
it( 'should not update the state if already selected and not typing', () => {
const original = deepFreeze( { uid: 'kumquat', typing: false, focus: {} } );
const state = selectedBlock( original, {
type: 'TOGGLE_BLOCK_SELECTED',
uid: 'kumquat',
Expand All @@ -269,6 +269,21 @@ describe( 'state', () => {
expect( state ).to.equal( original );
} );

it( 'should update the state if already selected and typing', () => {
const original = deepFreeze( { uid: 'kumquat', typing: true, focus: { editable: 'content' } } );
const state = selectedBlock( original, {
type: 'TOGGLE_BLOCK_SELECTED',
uid: 'kumquat',
selected: true
} );

expect( state ).to.eql( {
uid: 'kumquat',
typing: false,
focus: { editable: 'content' }
} );
} );

it( 'should unselect the block if currently selected', () => {
const original = deepFreeze( { uid: 'kumquat', typing: true, focus: {} } );
const state = selectedBlock( original, {
Expand Down

0 comments on commit d95c03c

Please sign in to comment.