TinyMCE: add wptextpattern plugin

This plugin can automatically format text patterns as you type. It includes two patterns: unordered (`* ` and `- `) and ordered list (`1. ` and `1) `). If the transformation in unwanted, the user can undo the change by pressing backspace, using the undo shortcut, or the undo button in the toolbar.

This is the first TinyMCE plugin that has unit tests and there's some good groundwork for adding tests to existing plugins in the future.

First run. See #31441.

Built from https://develop.svn.wordpress.org/trunk@32699


git-svn-id: http://core.svn.wordpress.org/trunk@32669 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Ella Iseulde Van Dorpe 2015-06-06 20:07:24 +00:00
parent ff17bd95fe
commit 356fd0ff81
5 changed files with 104 additions and 2 deletions

View File

@ -368,7 +368,8 @@ final class _WP_Editors {
'wpgallery', 'wpgallery',
'wplink', 'wplink',
'wpdialogs', 'wpdialogs',
'wpview', 'wptextpattern',
'wpview'
); );
if ( ! self::$has_medialib ) { if ( ! self::$has_medialib ) {

View File

@ -0,0 +1,100 @@
( 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;
text = node.nodeValue;
if ( node.nodeType !== 3 ) {
return;
}
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 );

View File

@ -0,0 +1 @@
!function(a,b){a.PluginManager.add("wptextpattern",function(c){function d(a,b){f.push({regExp:a,callback:b})}var e=c.$,f=[],g=!1;d(/^[*-]\s/,function(){this.execCommand("InsertUnorderedList")}),d(/^1[.)]\s/,function(){this.execCommand("InsertOrderedList")}),c.on("selectionchange",function(){g=!1}),c.on("keydown",function(b){g&&b.keyCode===a.util.VK.BACKSPACE&&(c.undoManager.undo(),b.preventDefault())}),c.on("keyup",function(d){var h,i,j,k,l;if(d.keyCode===a.util.VK.SPACEBAR&&(h=c.selection.getRng(),i=h.startContainer,j=i.nodeValue,3===i.nodeType&&(k=c.dom.getParent(i,"p")))){for(;(l=k.firstChild)&&3!==l.nodeType;)k=l;l===i&&a.each(f,function(a){var d=j.replace(a.regExp,"");if(j!==d&&h.startOffset===j.length-d.length)return c.undoManager.add(),c.undoManager.transact(function(){d?e(i).replaceWith(document.createTextNode(d)):e(i.parentNode).empty().append("<br>"),c.selection.setCursorLocation(k),a.callback.apply(c)}),b(function(){g=!0}),!1})}})})}(window.tinymce,window.setTimeout);

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.3-alpha-32698'; $wp_version = '4.3-alpha-32699';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.