mirror of
https://github.com/WordPress/WordPress.git
synced 2025-02-28 10:29:25 +00:00
Props azaozz, iseulde. See #31441. Built from https://develop.svn.wordpress.org/trunk@32700 git-svn-id: http://core.svn.wordpress.org/trunk@32670 1a063a9b-81f0-0310-95a4-ce76da25c4cd
101 lines
2.0 KiB
JavaScript
101 lines
2.0 KiB
JavaScript
( function( tinymce, setTimeout ) {
|
|
tinymce.PluginManager.add( 'wptextpattern', function( editor ) {
|
|
var $$ = editor.$,
|
|
patterns = [],
|
|
canUndo = false;
|
|
|
|
function add( regExp, callback ) {
|
|
patterns.push( {
|
|
regExp: regExp,
|
|
callback: callback
|
|
} );
|
|
}
|
|
|
|
add( /^[*-]\s/, function() {
|
|
this.execCommand( 'InsertUnorderedList' );
|
|
} );
|
|
|
|
add( /^1[.)]\s/, function() {
|
|
this.execCommand( 'InsertOrderedList' );
|
|
} );
|
|
|
|
editor.on( 'selectionchange', function() {
|
|
canUndo = false;
|
|
} );
|
|
|
|
editor.on( 'keydown', function( event ) {
|
|
if ( canUndo && event.keyCode === tinymce.util.VK.BACKSPACE ) {
|
|
editor.undoManager.undo();
|
|
event.preventDefault();
|
|
}
|
|
} );
|
|
|
|
editor.on( 'keyup', function( event ) {
|
|
var rng, node, text, parent, child;
|
|
|
|
if ( event.keyCode !== tinymce.util.VK.SPACEBAR ) {
|
|
return;
|
|
}
|
|
|
|
rng = editor.selection.getRng();
|
|
node = rng.startContainer;
|
|
|
|
if ( ! node || node.nodeType !== 3 ) {
|
|
return;
|
|
}
|
|
|
|
text = node.nodeValue;
|
|
parent = editor.dom.getParent( node, 'p' );
|
|
|
|
if ( ! parent ) {
|
|
return;
|
|
}
|
|
|
|
while ( child = parent.firstChild ) {
|
|
if ( child.nodeType !== 3 ) {
|
|
parent = child;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ( child !== node ) {
|
|
return;
|
|
}
|
|
|
|
tinymce.each( patterns, function( pattern ) {
|
|
var replace = text.replace( pattern.regExp, '' );
|
|
|
|
if ( text === replace ) {
|
|
return;
|
|
}
|
|
|
|
if ( rng.startOffset !== text.length - replace.length ) {
|
|
return;
|
|
}
|
|
|
|
editor.undoManager.add();
|
|
|
|
editor.undoManager.transact( function() {
|
|
if ( replace ) {
|
|
$$( node ).replaceWith( document.createTextNode( replace ) );
|
|
} else {
|
|
$$( node.parentNode ).empty().append( '<br>' );
|
|
}
|
|
|
|
editor.selection.setCursorLocation( parent );
|
|
|
|
pattern.callback.apply( editor );
|
|
} );
|
|
|
|
// We need to wait for native events to be triggered.
|
|
setTimeout( function() {
|
|
canUndo = true;
|
|
} );
|
|
|
|
return false;
|
|
} );
|
|
} );
|
|
} );
|
|
} )( window.tinymce, window.setTimeout );
|