2015-06-07 22:00:28 +02: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 22:07:24 +02:00
|
|
|
( function( tinymce, setTimeout ) {
|
|
|
|
tinymce.PluginManager.add( 'wptextpattern', function( editor ) {
|
2015-07-28 13:41:25 +02:00
|
|
|
var VK = tinymce.util.VK,
|
2015-07-28 01:16:25 +02:00
|
|
|
canUndo = false,
|
|
|
|
spacePatterns = [
|
|
|
|
{ regExp: /^[*-]\s/, cmd: 'InsertUnorderedList' },
|
|
|
|
{ regExp: /^1[.)]\s/, cmd: 'InsertOrderedList' }
|
|
|
|
],
|
|
|
|
enterPatterns = [
|
|
|
|
{ start: '##', format: 'h2' },
|
|
|
|
{ start: '###', format: 'h3' },
|
|
|
|
{ start: '####', format: 'h4' },
|
|
|
|
{ start: '#####', format: 'h5' },
|
|
|
|
{ start: '######', format: 'h6' },
|
|
|
|
{ start: '>', format: 'blockquote' }
|
2015-07-30 00:49:25 +02:00
|
|
|
],
|
|
|
|
refNode, refPattern;
|
2015-06-16 15:52:25 +02:00
|
|
|
|
2015-06-06 22:07:24 +02:00
|
|
|
editor.on( 'selectionchange', function() {
|
|
|
|
canUndo = false;
|
|
|
|
} );
|
|
|
|
|
|
|
|
editor.on( 'keydown', function( event ) {
|
2015-06-18 14:59:28 +02:00
|
|
|
if ( canUndo && ( event.keyCode === VK.BACKSPACE || event.keyCode === 27 /* ESCAPE */ ) ) {
|
2015-06-06 22:07:24 +02:00
|
|
|
editor.undoManager.undo();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2015-07-28 01:16:25 +02:00
|
|
|
if ( event.keyCode === VK.ENTER && ! VK.modifierPressed( event ) ) {
|
2015-07-30 00:49:25 +02:00
|
|
|
watchEnter();
|
2015-06-06 22:07:24 +02:00
|
|
|
}
|
2015-07-28 01:16:25 +02:00
|
|
|
}, true );
|
2015-06-06 22:07:24 +02:00
|
|
|
|
2015-07-28 01:16:25 +02:00
|
|
|
editor.on( 'keyup', function( event ) {
|
2015-07-30 00:49:25 +02:00
|
|
|
if ( ! VK.modifierPressed( event ) ) {
|
|
|
|
if ( event.keyCode === VK.SPACEBAR ) {
|
|
|
|
space();
|
|
|
|
} else if ( event.keyCode === VK.ENTER ) {
|
|
|
|
enter();
|
|
|
|
}
|
2015-06-06 22:07:24 +02:00
|
|
|
}
|
2015-07-28 01:16:25 +02:00
|
|
|
} );
|
2015-06-06 22:07:24 +02:00
|
|
|
|
2015-07-28 01:16:25 +02:00
|
|
|
function firstNode( node ) {
|
|
|
|
var parent = editor.dom.getParent( node, 'p' ),
|
|
|
|
child;
|
2015-06-06 22:07:24 +02:00
|
|
|
|
|
|
|
if ( ! parent ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( child = parent.firstChild ) {
|
|
|
|
if ( child.nodeType !== 3 ) {
|
|
|
|
parent = child;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-27 01:19:24 +02:00
|
|
|
if ( ! child ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-28 01:16:25 +02:00
|
|
|
if ( ! child.data ) {
|
2015-06-18 13:34:25 +02:00
|
|
|
child = child.nextSibling;
|
|
|
|
}
|
|
|
|
|
2015-07-28 01:16:25 +02:00
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
function space() {
|
|
|
|
var rng = editor.selection.getRng(),
|
|
|
|
node = rng.startContainer,
|
2015-07-28 13:41:25 +02:00
|
|
|
parent,
|
2015-07-28 01:16:25 +02:00
|
|
|
text;
|
|
|
|
|
2015-07-28 13:41:25 +02:00
|
|
|
if ( ! node || firstNode( node ) !== node ) {
|
2015-06-06 22:07:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-28 13:41:25 +02:00
|
|
|
parent = node.parentNode;
|
2015-07-28 01:16:25 +02:00
|
|
|
text = node.data;
|
|
|
|
|
|
|
|
tinymce.each( spacePatterns, function( pattern ) {
|
2015-07-28 13:41:25 +02:00
|
|
|
var match = text.match( pattern.regExp );
|
2015-06-06 22:07:24 +02:00
|
|
|
|
2015-07-28 13:41:25 +02:00
|
|
|
if ( ! match || rng.startOffset !== match[0].length ) {
|
2015-06-06 22:07:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
editor.undoManager.add();
|
|
|
|
|
|
|
|
editor.undoManager.transact( function() {
|
2015-07-28 13:41:25 +02:00
|
|
|
node.deleteData( 0, match[0].length );
|
2015-06-18 13:34:25 +02:00
|
|
|
|
2015-07-28 13:41:25 +02:00
|
|
|
if ( ! parent.innerHTML ) {
|
|
|
|
parent.appendChild( document.createElement( 'br' ) );
|
2015-06-06 22:07:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
editor.selection.setCursorLocation( parent );
|
2015-07-28 01:16:25 +02:00
|
|
|
editor.execCommand( pattern.cmd );
|
2015-06-06 22:07:24 +02:00
|
|
|
} );
|
|
|
|
|
|
|
|
// We need to wait for native events to be triggered.
|
|
|
|
setTimeout( function() {
|
|
|
|
canUndo = true;
|
|
|
|
} );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} );
|
2015-07-28 01:16:25 +02:00
|
|
|
}
|
|
|
|
|
2015-07-30 00:49:25 +02:00
|
|
|
function watchEnter() {
|
2015-07-28 01:16:25 +02:00
|
|
|
var selection = editor.selection,
|
|
|
|
rng = selection.getRng(),
|
|
|
|
offset = rng.startOffset,
|
|
|
|
start = rng.startContainer,
|
|
|
|
node = firstNode( start ),
|
|
|
|
i = enterPatterns.length,
|
|
|
|
text, pattern;
|
|
|
|
|
|
|
|
if ( ! node ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
text = node.data;
|
|
|
|
|
|
|
|
while ( i-- ) {
|
|
|
|
if ( text.indexOf( enterPatterns[ i ].start ) === 0 ) {
|
|
|
|
pattern = enterPatterns[ i ];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! pattern ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( node === start ) {
|
|
|
|
if ( tinymce.trim( text ) === pattern.start ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = Math.max( 0, offset - pattern.start.length );
|
|
|
|
}
|
|
|
|
|
2015-07-30 00:49:25 +02:00
|
|
|
refNode = node;
|
|
|
|
refPattern = pattern;
|
|
|
|
}
|
2015-07-28 01:16:25 +02:00
|
|
|
|
2015-07-30 00:49:25 +02:00
|
|
|
function enter() {
|
|
|
|
if ( refNode ) {
|
|
|
|
editor.undoManager.add();
|
2015-07-28 01:16:25 +02:00
|
|
|
|
2015-07-30 00:49:25 +02:00
|
|
|
editor.undoManager.transact( function() {
|
|
|
|
editor.formatter.apply( refPattern.format, {}, refNode );
|
|
|
|
refNode.deleteData( 0, refPattern.start.length );
|
|
|
|
} );
|
|
|
|
}
|
2015-07-28 01:16:25 +02:00
|
|
|
|
2015-07-30 00:49:25 +02:00
|
|
|
refNode = null;
|
|
|
|
refPattern = null;
|
2015-07-28 01:16:25 +02:00
|
|
|
}
|
2015-06-06 22:07:24 +02:00
|
|
|
} );
|
|
|
|
} )( window.tinymce, window.setTimeout );
|