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
This commit is contained in:
Ella Iseulde Van Dorpe 2016-02-26 00:51:01 +00:00
parent 918da6f8c2
commit 37fda5a8b9
2 changed files with 46 additions and 40 deletions

View File

@ -25,7 +25,8 @@
{ start: '####', format: 'h4' }, { start: '####', format: 'h4' },
{ start: '#####', format: 'h5' }, { start: '#####', format: 'h5' },
{ start: '######', format: 'h6' }, { start: '######', format: 'h6' },
{ start: '>', format: 'blockquote' } { start: '>', format: 'blockquote' },
{ regExp: /^\s*(?:(?:\* ?){3,}|(?:_ ?){3,}|(?:- ?){3,})\s*$/, element: 'hr' }
]; ];
var inlinePatterns = [ var inlinePatterns = [
@ -37,10 +38,7 @@
]; ];
var canUndo; var canUndo;
var refNode;
var refPattern;
var chars = []; var chars = [];
var zeroWidthSpaceNode;
tinymce.each( inlinePatterns, function( pattern ) { tinymce.each( inlinePatterns, function( pattern ) {
tinymce.each( ( pattern.start + pattern.end ).split( '' ), function( c ) { tinymce.each( ( pattern.start + pattern.end ).split( '' ), function( c ) {
@ -51,17 +49,7 @@
} ); } );
editor.on( 'selectionchange', function() { editor.on( 'selectionchange', function() {
var offset;
canUndo = null; canUndo = null;
if ( zeroWidthSpaceNode ) {
offset = zeroWidthSpaceNode.data.indexOf( '\u200b' );
if ( offset !== -1 ) {
zeroWidthSpaceNode.deleteData( offset, offset + 1 );
}
}
} ); } );
editor.on( 'keydown', function( event ) { editor.on( 'keydown', function( event ) {
@ -72,15 +60,13 @@
} }
if ( event.keyCode === VK.ENTER && ! VK.modifierPressed( event ) ) { if ( event.keyCode === VK.ENTER && ! VK.modifierPressed( event ) ) {
watchEnter(); enter();
} }
}, true ); }, true );
editor.on( 'keyup', function( event ) { editor.on( 'keyup', function( event ) {
if ( event.keyCode === VK.SPACEBAR && ! event.ctrlKey && ! event.metaKey && ! event.altKey ) { if ( event.keyCode === VK.SPACEBAR && ! event.ctrlKey && ! event.metaKey && ! event.altKey ) {
space(); space();
} else if ( event.keyCode === VK.ENTER && ! VK.modifierPressed( event ) ) {
enter();
} else if ( event.keyCode > 47 && ! ( event.keyCode >= 91 && event.keyCode <= 93 ) ) { } else if ( event.keyCode > 47 && ! ( event.keyCode >= 91 && event.keyCode <= 93 ) ) {
inline(); inline();
} }
@ -155,7 +141,18 @@
// We need to wait for native events to be triggered. // We need to wait for native events to be triggered.
setTimeout( function() { setTimeout( function() {
canUndo = 'space'; 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(), var rng = editor.selection.getRng(),
start = rng.startContainer, start = rng.startContainer,
node = firstTextNode( start ), node = firstTextNode( start ),
@ -247,10 +244,17 @@
text = node.data; text = node.data;
while ( i-- ) { while ( i-- ) {
if ( enterPatterns[ i ].start ) {
if ( text.indexOf( enterPatterns[ i ].start ) === 0 ) { if ( text.indexOf( enterPatterns[ i ].start ) === 0 ) {
pattern = enterPatterns[ i ]; pattern = enterPatterns[ i ];
break; break;
} }
} else if ( enterPatterns[ i ].regExp ) {
if ( enterPatterns[ i ].regExp.test( text ) ) {
pattern = enterPatterns[ i ];
break;
}
}
} }
if ( ! pattern ) { if ( ! pattern ) {
@ -261,31 +265,27 @@
return; return;
} }
refNode = node; editor.once( 'keyup', function() {
refPattern = pattern;
}
function ltrim( text ) {
return text ? text.replace( /^\s+/, '' ) : '';
}
function enter() {
if ( refNode ) {
editor.undoManager.add(); editor.undoManager.add();
editor.undoManager.transact( function() { editor.undoManager.transact( function() {
editor.formatter.apply( refPattern.format, {}, refNode ); if ( pattern.format ) {
refNode.replaceData( 0, refNode.data.length, ltrim( refNode.data.slice( refPattern.start.length ) ) ); 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. // We need to wait for native events to be triggered.
setTimeout( function() { setTimeout( function() {
canUndo = 'enter'; canUndo = 'enter';
} ); } );
} );
} }
refNode = null; function ltrim( text ) {
refPattern = null; return text ? text.replace( /^\s+/, '' ) : '';
} }
} ); } );
} )( window.tinymce, window.setTimeout ); } )( window.tinymce, window.setTimeout );

View File

@ -306,8 +306,14 @@
} ); } );
QUnit.test( 'Inline: no change.', function( assert ) { QUnit.test( 'Inline: no change.', function( assert ) {
type( '******', function() { type( 'test ******', function() {
assert.equal( editor.getContent(), '<p>******</p>' ); assert.equal( editor.getContent(), '<p>test ******</p>' );
}, assert.async() );
} );
QUnit.test( 'Horizontal Rule', function( assert ) {
type( ' --- \n', function() {
assert.equal( editor.getContent(), '<hr />\n<p>&nbsp;</p>' );
}, assert.async() ); }, assert.async() );
} ); } );
} )( window.jQuery, window.QUnit, window.tinymce, window.setTimeout ); } )( window.jQuery, window.QUnit, window.tinymce, window.setTimeout );