Skip to content

Commit

Permalink
Accessibility: Avoid Keyboard trap on the editor content
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Jun 22, 2017
1 parent 68ea8cc commit 401e683
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions editor/modes/visual-editor/block-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
* External dependencies
*/
import { connect } from 'react-redux';
import { throttle, reduce } from 'lodash';
import { throttle, reduce, noop } from 'lodash';

/**
* WordPress dependencies
*/
import { __ } from 'i18n';
import { Component } from 'element';
import { serialize, getDefaultBlock, createBlock } from 'blocks';
import { ENTER } from 'utils/keycodes';

/**
* Internal dependencies
Expand Down Expand Up @@ -40,6 +41,7 @@ class VisualEditorBlockList extends Component {
this.setBlockRef = this.setBlockRef.bind( this );
this.appendDefaultBlock = this.appendDefaultBlock.bind( this );
this.onPointerMove = throttle( this.onPointerMove.bind( this ), 250 );
this.onPlaceholderKeyDown = this.onPlaceholderKeyDown.bind( this );
// Browser does not fire `*move` event when the pointer position changes
// relative to the document, so fire it with the last known position.
this.onScroll = () => this.onPointerMove( { clientY: this.lastClientY } );
Expand Down Expand Up @@ -155,6 +157,12 @@ class VisualEditorBlockList extends Component {
window.removeEventListener( 'touchend', this.onSelectionEnd );
}

onPlaceholderKeyDown( event ) {
if ( event.keyCode === ENTER ) {
this.appendDefaultBlock();
}
}

appendDefaultBlock() {
const newBlock = createBlock( getDefaultBlock() );
this.props.onInsertBlock( newBlock );
Expand Down Expand Up @@ -199,7 +207,9 @@ class VisualEditorBlockList extends Component {
readOnly
className="editor-visual-editor__placeholder"
value={ ! blocks.length ? __( 'Write your story' ) : __( 'Write' ) }
onFocus={ this.appendDefaultBlock }
onFocus={ ! blocks.length ? this.appendDefaultBlock : noop }
onClick={ !! blocks.length ? this.appendDefaultBlock : noop }
onKeyDown={ !! blocks.length ? this.onPlaceholderKeyDown : noop }
/>
</div>
);
Expand Down

0 comments on commit 401e683

Please sign in to comment.