Skip to content

Commit

Permalink
Split enter and space patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Aug 4, 2017
1 parent e946b71 commit c21f4b8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
23 changes: 11 additions & 12 deletions blocks/editable/patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import tinymce from 'tinymce';
import { endsWith, find, get, escapeRegExp, partition, drop } from 'lodash';
import { find, get, escapeRegExp, groupBy, drop } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -28,16 +28,15 @@ export default function( editor ) {
const VK = tinymce.util.VK;
const settings = editor.settings.wptextpattern || {};

const patterns = getBlockTypes().reduce( ( acc, blockType ) => {
const {
paste: pastePatterns,
enter: enterPatterns,
undefined: spacePatterns,
} = groupBy( getBlockTypes().reduce( ( acc, blockType ) => {
const transformsFrom = get( blockType, 'transforms.from', [] );
const transforms = transformsFrom.filter( ( { type } ) => type === 'pattern' );
return [ ...acc, ...transforms ];
}, [] );

const [ enterPatterns, spacePatterns ] = partition(
patterns,
( { regExp } ) => endsWith( regExp.source, '$' ),
);
}, [] ), 'trigger' );

const inlinePatterns = settings.inline || [
{ delimiter: '`', format: 'code' },
Expand All @@ -50,7 +49,7 @@ export default function( editor ) {
} );

editor.on( 'pastepostprocess', () => {
setTimeout( space );
setTimeout( () => searchFirstText( pastePatterns ) );
} );

editor.on( 'keydown', function( event ) {
Expand All @@ -70,7 +69,7 @@ export default function( editor ) {
enter();
// Wait for the browser to insert the character.
} else if ( keyCode === SPACE ) {
setTimeout( space );
setTimeout( () => searchFirstText( spacePatterns ) );
} else if ( keyCode > 47 && ! ( keyCode >= 91 && keyCode <= 93 ) ) {
setTimeout( inline );
}
Expand Down Expand Up @@ -176,7 +175,7 @@ export default function( editor ) {
} );
}

function space() {
function searchFirstText( patterns ) {
if ( ! onReplace ) {
return;
}
Expand All @@ -192,7 +191,7 @@ export default function( editor ) {

const firstText = content[ 0 ];

const { result, pattern } = spacePatterns.reduce( ( acc, item ) => {
const { result, pattern } = patterns.reduce( ( acc, item ) => {
return acc.result ? acc : {
result: item.regExp.exec( firstText ),
pattern: item,
Expand Down
1 change: 1 addition & 0 deletions blocks/library/code/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ registerBlockType( 'core/code', {
from: [
{
type: 'pattern',
trigger: 'enter',
regExp: /^```$/,
transform: () => createBlock( 'core/code' ),
},
Expand Down
1 change: 1 addition & 0 deletions blocks/library/separator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ registerBlockType( 'core/separator', {
from: [
{
type: 'pattern',
trigger: 'enter',
regExp: /^-{3,}$/,
transform: () => createBlock( 'core/separator' ),
},
Expand Down

0 comments on commit c21f4b8

Please sign in to comment.