TinyMCE: wptextpattern: fix typo and make it faster
See #31441. git-svn-id: https://develop.svn.wordpress.org/trunk@33458 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
ecd1ceccdc
commit
4393f478ea
@ -12,8 +12,7 @@
|
|||||||
*/
|
*/
|
||||||
( function( tinymce, setTimeout ) {
|
( function( tinymce, setTimeout ) {
|
||||||
tinymce.PluginManager.add( 'wptextpattern', function( editor ) {
|
tinymce.PluginManager.add( 'wptextpattern', function( editor ) {
|
||||||
var $$ = editor.$,
|
var VK = tinymce.util.VK,
|
||||||
VK = tinymce.util.VK,
|
|
||||||
canUndo = false,
|
canUndo = false,
|
||||||
spacePatterns = [
|
spacePatterns = [
|
||||||
{ regExp: /^[*-]\s/, cmd: 'InsertUnorderedList' },
|
{ regExp: /^[*-]\s/, cmd: 'InsertUnorderedList' },
|
||||||
@ -44,7 +43,7 @@
|
|||||||
}, true );
|
}, true );
|
||||||
|
|
||||||
editor.on( 'keyup', function( event ) {
|
editor.on( 'keyup', function( event ) {
|
||||||
if ( event.keyCode === VK.SPACEBAR || ! VK.modifierPressed( event ) ) {
|
if ( event.keyCode === VK.SPACEBAR && ! VK.modifierPressed( event ) ) {
|
||||||
space();
|
space();
|
||||||
}
|
}
|
||||||
} );
|
} );
|
||||||
@ -79,41 +78,30 @@
|
|||||||
function space() {
|
function space() {
|
||||||
var rng = editor.selection.getRng(),
|
var rng = editor.selection.getRng(),
|
||||||
node = rng.startContainer,
|
node = rng.startContainer,
|
||||||
|
parent,
|
||||||
text;
|
text;
|
||||||
|
|
||||||
if ( firstNode( node ) !== node ) {
|
if ( ! node || firstNode( node ) !== node ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parent = node.parentNode;
|
||||||
text = node.data;
|
text = node.data;
|
||||||
|
|
||||||
tinymce.each( spacePatterns, function( pattern ) {
|
tinymce.each( spacePatterns, function( pattern ) {
|
||||||
var replace = text.replace( pattern.regExp, '' );
|
var match = text.match( pattern.regExp );
|
||||||
|
|
||||||
if ( text === replace ) {
|
if ( ! match || rng.startOffset !== match[0].length ) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( rng.startOffset !== text.length - replace.length ) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
editor.undoManager.add();
|
editor.undoManager.add();
|
||||||
|
|
||||||
editor.undoManager.transact( function() {
|
editor.undoManager.transact( function() {
|
||||||
var parent = node.parentNode,
|
node.deleteData( 0, match[0].length );
|
||||||
$$parent;
|
|
||||||
|
|
||||||
if ( replace ) {
|
if ( ! parent.innerHTML ) {
|
||||||
$$( node ).replaceWith( document.createTextNode( replace ) );
|
parent.appendChild( document.createElement( 'br' ) );
|
||||||
} else {
|
|
||||||
$$parent = $$( parent );
|
|
||||||
|
|
||||||
$$( node ).remove();
|
|
||||||
|
|
||||||
if ( ! $$parent.html() ) {
|
|
||||||
$$parent.append( '<br>' );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
editor.selection.setCursorLocation( parent );
|
editor.selection.setCursorLocation( parent );
|
||||||
|
Loading…
Reference in New Issue
Block a user