From 37fda5a8b95fd2b5f983a45248f9e35615a0b0a1 Mon Sep 17 00:00:00 2001 From: Ella Iseulde Van Dorpe Date: Fri, 26 Feb 2016 00:51:01 +0000 Subject: [PATCH] TinyMCE: textpattern: clean up * Use editor.once instead of storing into variables. * Add pattern for hr. See #33300. git-svn-id: https://develop.svn.wordpress.org/trunk@36719 602fd350-edb4-49c9-b593-d223f7449a82 --- .../tinymce/plugins/wptextpattern/plugin.js | 76 +++++++++---------- .../tinymce/plugins/wptextpattern/plugin.js | 10 ++- 2 files changed, 46 insertions(+), 40 deletions(-) diff --git a/src/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js b/src/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js index f861dae822..375dd849e2 100644 --- a/src/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js +++ b/src/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js @@ -25,7 +25,8 @@ { start: '####', format: 'h4' }, { start: '#####', format: 'h5' }, { start: '######', format: 'h6' }, - { start: '>', format: 'blockquote' } + { start: '>', format: 'blockquote' }, + { regExp: /^\s*(?:(?:\* ?){3,}|(?:_ ?){3,}|(?:- ?){3,})\s*$/, element: 'hr' } ]; var inlinePatterns = [ @@ -37,10 +38,7 @@ ]; var canUndo; - var refNode; - var refPattern; var chars = []; - var zeroWidthSpaceNode; tinymce.each( inlinePatterns, function( pattern ) { tinymce.each( ( pattern.start + pattern.end ).split( '' ), function( c ) { @@ -51,17 +49,7 @@ } ); editor.on( 'selectionchange', function() { - var offset; - canUndo = null; - - if ( zeroWidthSpaceNode ) { - offset = zeroWidthSpaceNode.data.indexOf( '\u200b' ); - - if ( offset !== -1 ) { - zeroWidthSpaceNode.deleteData( offset, offset + 1 ); - } - } } ); editor.on( 'keydown', function( event ) { @@ -72,15 +60,13 @@ } if ( event.keyCode === VK.ENTER && ! VK.modifierPressed( event ) ) { - watchEnter(); + enter(); } }, true ); editor.on( 'keyup', function( event ) { if ( event.keyCode === VK.SPACEBAR && ! event.ctrlKey && ! event.metaKey && ! event.altKey ) { space(); - } else if ( event.keyCode === VK.ENTER && ! VK.modifierPressed( event ) ) { - enter(); } else if ( event.keyCode > 47 && ! ( event.keyCode >= 91 && event.keyCode <= 93 ) ) { inline(); } @@ -155,7 +141,18 @@ // We need to wait for native events to be triggered. setTimeout( function() { canUndo = 'space'; - zeroWidthSpaceNode = zero; + + editor.once( 'selectionchange', function() { + var offset; + + if ( zero ) { + offset = zero.data.indexOf( '\u200b' ); + + if ( offset !== -1 ) { + zero.deleteData( offset, offset + 1 ); + } + } + } ); } ); } } @@ -233,7 +230,7 @@ } ); } - function watchEnter() { + function enter() { var rng = editor.selection.getRng(), start = rng.startContainer, node = firstTextNode( start ), @@ -247,10 +244,17 @@ text = node.data; while ( i-- ) { - if ( text.indexOf( enterPatterns[ i ].start ) === 0 ) { - pattern = enterPatterns[ i ]; - break; - } + if ( enterPatterns[ i ].start ) { + if ( text.indexOf( enterPatterns[ i ].start ) === 0 ) { + pattern = enterPatterns[ i ]; + break; + } + } else if ( enterPatterns[ i ].regExp ) { + if ( enterPatterns[ i ].regExp.test( text ) ) { + pattern = enterPatterns[ i ]; + break; + } + } } if ( ! pattern ) { @@ -261,31 +265,27 @@ return; } - refNode = node; - refPattern = pattern; - } - - function ltrim( text ) { - return text ? text.replace( /^\s+/, '' ) : ''; - } - - function enter() { - if ( refNode ) { + editor.once( 'keyup', function() { editor.undoManager.add(); editor.undoManager.transact( function() { - editor.formatter.apply( refPattern.format, {}, refNode ); - refNode.replaceData( 0, refNode.data.length, ltrim( refNode.data.slice( refPattern.start.length ) ) ); + if ( pattern.format ) { + editor.formatter.apply( pattern.format, {}, node ); + node.replaceData( 0, node.data.length, ltrim( node.data.slice( pattern.start.length ) ) ); + } else if ( pattern.element ) { + editor.getBody().replaceChild( document.createElement( pattern.element ), node.parentNode ); + } } ); // We need to wait for native events to be triggered. setTimeout( function() { canUndo = 'enter'; } ); - } + } ); + } - refNode = null; - refPattern = null; + function ltrim( text ) { + return text ? text.replace( /^\s+/, '' ) : ''; } } ); } )( window.tinymce, window.setTimeout ); diff --git a/tests/qunit/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js b/tests/qunit/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js index 933369c5cc..aa929ff96e 100644 --- a/tests/qunit/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js +++ b/tests/qunit/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js @@ -306,8 +306,14 @@ } ); QUnit.test( 'Inline: no change.', function( assert ) { - type( '******', function() { - assert.equal( editor.getContent(), '

******

' ); + type( 'test ******', function() { + assert.equal( editor.getContent(), '

test ******

' ); + }, assert.async() ); + } ); + + QUnit.test( 'Horizontal Rule', function( assert ) { + type( ' --- \n', function() { + assert.equal( editor.getContent(), '
\n

 

' ); }, assert.async() ); } ); } )( window.jQuery, window.QUnit, window.tinymce, window.setTimeout );