Skip to content

Commit

Permalink
More changes.
Browse files Browse the repository at this point in the history
Adding some minor changes.  Maintainability improvements, and cleaning
up unused code.  Fix an error condition where the list of blocks is
empty. Fix arrow key behavior in search field so that left and right
arrow keys can move the cursor properly.
  • Loading branch information
BE-Webdesign committed May 4, 2017
1 parent 153adb2 commit 1e9551a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
1 change: 0 additions & 1 deletion editor/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import IconButton from 'components/icon-button';
class Inserter extends wp.element.Component {
constructor() {
super( ...arguments );
this.nodes = {};
this.toggle = this.toggle.bind( this );
this.close = this.close.bind( this );
this.state = {
Expand Down
56 changes: 43 additions & 13 deletions editor/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ class InserterMenu extends wp.element.Component {

isNextKeydown( keydown ) {
return keydown.code === 'ArrowDown'
|| keydown.code === 'ArrowRight'
|| ( keydown.code === 'Tab' && keydown.shiftKey === false );
}

isArrowRight( keydown ) {
return keydown.code === 'ArrowRight';
}

isArrowLeft( keydown ) {
return keydown.code === 'ArrowLeft';
}

isPreviousKeydown( keydown ) {
return keydown.code === 'ArrowUp'
|| keydown.code === 'ArrowLeft'
|| ( keydown.code === 'Tab' && keydown.shiftKey === true );
}

Expand Down Expand Up @@ -115,10 +121,10 @@ class InserterMenu extends wp.element.Component {

findNext( currentBlock, blockTypes ) {
/**
* 'search' or null are the values that will trigger iterating back to
* null is the value that will trigger iterating back to
* the top of the list of block types.
*/
if ( null === currentBlock || 'search' === currentBlock ) {
if ( null === currentBlock ) {
return blockTypes[ 0 ].slug;
}

Expand Down Expand Up @@ -179,14 +185,15 @@ class InserterMenu extends wp.element.Component {
this.getVisibleBlocks,
this.sortBlocksByCategory,
)( component.blockTypes );
const currentBlock = component.state.currentFocus;

let nextBlock = this.findNext( currentBlock, sortedByCategory );

if ( nextBlock === null ) {
nextBlock = 'search';
// If the block list is empty return early.
if ( ! sortedByCategory.length ) {
return;
}

const currentBlock = component.state.currentFocus;

const nextBlock = this.findNext( currentBlock, sortedByCategory );
this.changeMenuSelection( nextBlock );
}

Expand All @@ -197,12 +204,12 @@ class InserterMenu extends wp.element.Component {
)( component.blockTypes );
const currentBlock = component.state.currentFocus;

let nextBlock = this.findPrevious( currentBlock, sortedByCategory );

if ( nextBlock === null ) {
nextBlock = 'search';
// If the block list is empty return early.
if ( ! sortedByCategory.length ) {
return;
}

const nextBlock = this.findPrevious( currentBlock, sortedByCategory );
this.changeMenuSelection( nextBlock );
}

Expand All @@ -217,13 +224,35 @@ class InserterMenu extends wp.element.Component {
this.focusPrevious( this );
}

/**
* Left and right arrow keys need to be handled seperately so that
* default cursor behavior can be handled in the search field.
*/
if ( this.isArrowRight( keydown ) ) {
if ( this.state.currentFocus === 'search' ) {
return;
}
this.focusNext( this );
}

if ( this.isArrowLeft( keydown ) ) {
if ( this.state.currentFocus === 'search' ) {
return;
}
this.focusPrevious( this );
}

if ( this.isEscapeKey( keydown ) ) {
keydown.preventDefault();
this.props.closeMenu();
}
}

changeMenuSelection( refName ) {
if ( refName === null ) {
refName = 'search';
}

this.setState( {
currentFocus: refName
} );
Expand Down Expand Up @@ -291,6 +320,7 @@ class InserterMenu extends wp.element.Component {
onChange={ this.filter }
onClick={ this.setSearchFocus }
ref={ this.bindReferenceNode( 'search' ) }
tabIndex="-1"
/>
</div>
);
Expand Down

0 comments on commit 1e9551a

Please sign in to comment.