From 7a0be8e09c41453f3a9179f7a3d6e181dd884661 Mon Sep 17 00:00:00 2001 From: iseulde Date: Mon, 3 Jul 2017 20:27:36 +0200 Subject: [PATCH] Embrace lodash and utils/keycodes --- blocks/editable/patterns.js | 34 +++++++++++++--------------------- utils/keycodes.js | 1 + 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/blocks/editable/patterns.js b/blocks/editable/patterns.js index 88e39479bef9cc..78774b8d26953e 100644 --- a/blocks/editable/patterns.js +++ b/blocks/editable/patterns.js @@ -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 @@ -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; @@ -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(); @@ -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 ); @@ -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; @@ -303,8 +299,4 @@ export default function( editor ) { } ); } ); } - - function ltrim( text ) { - return text ? text.replace( /^\s+/, '' ) : ''; - } } diff --git a/utils/keycodes.js b/utils/keycodes.js index 3363e4f1ade462..9e4f253ffa4454 100644 --- a/utils/keycodes.js +++ b/utils/keycodes.js @@ -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;