2015-06-07 16:00:28 -04:00
|
|
|
/**
|
|
|
|
* Text pattern plugin for TinyMCE
|
|
|
|
*
|
|
|
|
* @since 4.3.0
|
|
|
|
*
|
|
|
|
* This plugin can automatically format text patterns as you type. It includes two patterns:
|
|
|
|
* - Unordered list (`* ` 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.
|
|
|
|
*/
|
2015-06-06 16:07:24 -04:00
|
|
|
( function( tinymce, setTimeout ) {
|
|
|
|
tinymce.PluginManager.add( 'wptextpattern', function( editor ) {
|
|
|
|
var $$ = editor.$,
|
2015-06-18 08:59:28 -04:00
|
|
|
VK = tinymce.util.VK,
|
2015-06-06 16:07:24 -04:00
|
|
|
patterns = [],
|
|
|
|
canUndo = false;
|
|
|
|
|
2015-06-07 16:00:28 -04:00
|
|
|
/**
|
|
|
|
* Add a pattern to format with a callback.
|
|
|
|
*
|
|
|
|
* @since 4.3.0
|
|
|
|
*
|
2015-07-13 14:09:24 -04:00
|
|
|
* @param {RegExp} regExp RegEx pattern.
|
|
|
|
* @param {Function} callback Callback.
|
2015-06-07 16:00:28 -04:00
|
|
|
*/
|
2015-06-06 16:07:24 -04:00
|
|
|
function add( regExp, callback ) {
|
|
|
|
patterns.push( {
|
|
|
|
regExp: regExp,
|
|
|
|
callback: callback
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
add( /^[*-]\s/, function() {
|
|
|
|
this.execCommand( 'InsertUnorderedList' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
add( /^1[.)]\s/, function() {
|
|
|
|
this.execCommand( 'InsertOrderedList' );
|
|
|
|
} );
|
|
|
|
|
2015-06-16 09:52:25 -04:00
|
|
|
add( /^>\s/, function() {
|
|
|
|
this.formatter.toggle( 'blockquote' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
add( /^(#{2,6})\s/, function() {
|
|
|
|
this.formatter.toggle( 'h' + arguments[1].length );
|
|
|
|
} );
|
|
|
|
|
2015-06-06 16:07:24 -04:00
|
|
|
editor.on( 'selectionchange', function() {
|
|
|
|
canUndo = false;
|
|
|
|
} );
|
|
|
|
|
|
|
|
editor.on( 'keydown', function( event ) {
|
2015-06-18 08:59:28 -04:00
|
|
|
if ( canUndo && ( event.keyCode === VK.BACKSPACE || event.keyCode === 27 /* ESCAPE */ ) ) {
|
2015-06-06 16:07:24 -04:00
|
|
|
editor.undoManager.undo();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
editor.on( 'keyup', function( event ) {
|
|
|
|
var rng, node, text, parent, child;
|
|
|
|
|
2015-06-18 08:59:28 -04:00
|
|
|
if ( event.keyCode !== VK.SPACEBAR ) {
|
2015-06-06 16:07:24 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rng = editor.selection.getRng();
|
|
|
|
node = rng.startContainer;
|
|
|
|
|
2015-06-06 18:38:28 -04:00
|
|
|
if ( ! node || node.nodeType !== 3 ) {
|
2015-06-06 16:07:24 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-06 18:38:28 -04:00
|
|
|
text = node.nodeValue;
|
2015-06-06 16:07:24 -04:00
|
|
|
parent = editor.dom.getParent( node, 'p' );
|
|
|
|
|
|
|
|
if ( ! parent ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( child = parent.firstChild ) {
|
|
|
|
if ( child.nodeType !== 3 ) {
|
|
|
|
parent = child;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-26 19:19:24 -04:00
|
|
|
if ( ! child ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-18 07:34:25 -04:00
|
|
|
if ( ! child.nodeValue ) {
|
|
|
|
child = child.nextSibling;
|
|
|
|
}
|
|
|
|
|
2015-06-06 16:07:24 -04:00
|
|
|
if ( child !== node ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tinymce.each( patterns, function( pattern ) {
|
2015-06-16 09:52:25 -04:00
|
|
|
var args,
|
|
|
|
replace = text.replace( pattern.regExp, function() {
|
|
|
|
args = arguments;
|
|
|
|
return '';
|
|
|
|
} );
|
2015-06-06 16:07:24 -04:00
|
|
|
|
|
|
|
if ( text === replace ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( rng.startOffset !== text.length - replace.length ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
editor.undoManager.add();
|
|
|
|
|
|
|
|
editor.undoManager.transact( function() {
|
2015-06-18 07:34:25 -04:00
|
|
|
var $$parent;
|
|
|
|
|
2015-06-06 16:07:24 -04:00
|
|
|
if ( replace ) {
|
|
|
|
$$( node ).replaceWith( document.createTextNode( replace ) );
|
|
|
|
} else {
|
2015-06-18 07:34:25 -04:00
|
|
|
$$parent = $$( node.parentNode );
|
|
|
|
|
|
|
|
$$( node ).remove();
|
|
|
|
|
|
|
|
if ( ! $$parent.html() ) {
|
|
|
|
$$parent.append( '<br>' );
|
|
|
|
}
|
2015-06-06 16:07:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
editor.selection.setCursorLocation( parent );
|
|
|
|
|
2015-06-16 09:52:25 -04:00
|
|
|
pattern.callback.apply( editor, args );
|
2015-06-06 16:07:24 -04:00
|
|
|
} );
|
|
|
|
|
|
|
|
// We need to wait for native events to be triggered.
|
|
|
|
setTimeout( function() {
|
|
|
|
canUndo = true;
|
|
|
|
} );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} )( window.tinymce, window.setTimeout );
|