Skip to content

Commit

Permalink
Embrace lodash and utils/keycodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Jul 8, 2017
1 parent 55a2c18 commit 7a0be8e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
34 changes: 13 additions & 21 deletions blocks/editable/patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
* External dependencies
*/
import tinymce from 'tinymce';
import { find, get } from 'lodash';
import { find, get, escapeRegExp, trimStart } from 'lodash';

/**
* WordPress dependencies
*/
import { ESCAPE, ENTER, SPACE, BACKSPACE } from 'utils/keycodes';

/**
* Internal dependencies
Expand All @@ -16,17 +21,6 @@ import { getBlockTypes } from '../api/registration';
*/
const { setTimeout } = window;

/**
* Escapes characters for use in a Regular Expression.
*
* @param {String} string Characters to escape
*
* @return {String} Escaped characters
*/
function escapeRegExp( string ) {
return string.replace( /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&' );
}

export default function( editor ) {
const getContent = this.getContent.bind( this );
const { onReplace } = this.props;
Expand Down Expand Up @@ -61,7 +55,9 @@ export default function( editor ) {
} );

editor.on( 'keydown', function( event ) {
if ( ( canUndo && event.keyCode === 27 /* ESCAPE */ ) || ( canUndo === 'space' && event.keyCode === VK.BACKSPACE ) ) {
const { keyCode } = event;

if ( ( canUndo && keyCode === ESCAPE ) || ( canUndo === 'space' && keyCode === BACKSPACE ) ) {
editor.undoManager.undo();
event.preventDefault();
event.stopImmediatePropagation();
Expand All @@ -71,12 +67,12 @@ export default function( editor ) {
return;
}

if ( event.keyCode === VK.ENTER ) {
if ( keyCode === ENTER ) {
enter();
// Wait for the browser to insert the character.
} else if ( event.keyCode === VK.SPACEBAR ) {
} else if ( keyCode === SPACE ) {
setTimeout( space );
} else if ( event.keyCode > 47 && ! ( event.keyCode >= 91 && event.keyCode <= 93 ) ) {
} else if ( keyCode > 47 && ! ( keyCode >= 91 && keyCode <= 93 ) ) {
setTimeout( inline );
}
}, true );
Expand Down Expand Up @@ -287,7 +283,7 @@ export default function( editor ) {
editor.undoManager.transact( function() {
if ( pattern.format ) {
editor.formatter.apply( pattern.format, {}, node );
node.replaceData( 0, node.data.length, ltrim( node.data.slice( pattern.start.length ) ) );
node.replaceData( 0, node.data.length, trimStart( node.data.slice( pattern.start.length ) ) );
} else if ( pattern.element ) {
parent = node.parentNode && node.parentNode.parentNode;

Expand All @@ -303,8 +299,4 @@ export default function( editor ) {
} );
} );
}

function ltrim( text ) {
return text ? text.replace( /^\s+/, '' ) : '';
}
}
1 change: 1 addition & 0 deletions utils/keycodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const BACKSPACE = 8;
export const TAB = 9;
export const ENTER = 13;
export const ESCAPE = 27;
export const SPACE = 32;
export const LEFT = 37;
export const UP = 38;
export const RIGHT = 39;
Expand Down

0 comments on commit 7a0be8e

Please sign in to comment.