Skip to content

Commit

Permalink
Add ability to cancel adding link using Escape
Browse files Browse the repository at this point in the history
Add a keydown listener on component, which if you hit Escape while you
are editing the link will stop without canceling out of the block. If
you hit escape again, it will cancel the block.
  • Loading branch information
mkaz committed Jun 12, 2017
1 parent fbe8a63 commit 0e01839
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions blocks/editable/format-toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { IconButton, Toolbar } from 'components';
import { ESCAPE } from 'utils/keycodes';

const FORMATTING_CONTROLS = [
{
Expand Down Expand Up @@ -36,6 +37,11 @@ class FormatToolbar extends wp.element.Component {
this.dropLink = this.dropLink.bind( this );
this.submitLink = this.submitLink.bind( this );
this.updateLinkValue = this.updateLinkValue.bind( this );
this.onKeyDown = this.onKeyDown.bind( this );
}

componentDidMount() {
document.addEventListener( 'keydown', this.onKeyDown );
}

componentWillUnmout() {
Expand All @@ -44,6 +50,19 @@ class FormatToolbar extends wp.element.Component {
}
}

onKeyDown( keydown ) {
switch ( keydown.keyCode ) {
case ESCAPE:
if ( this.state.isEditingLink ) {
keydown.preventDefault();
this.dropLink();
}
break;
default :
break;
}
}

componentWillReceiveProps( nextProps ) {
const newState = {
linkValue: nextProps.formats.link ? nextProps.formats.link.value : '',
Expand Down

0 comments on commit 0e01839

Please sign in to comment.