2 * Text pattern plugin for TinyMCE
6 * This plugin can automatically format text patterns as you type. It includes two patterns:
7 * - Unordered list (`* ` and `- `).
8 * - Ordered list (`1. ` and `1) `).
10 * If the transformation in unwanted, the user can undo the change by pressing backspace,
11 * using the undo shortcut, or the undo button in the toolbar.
13 ( function( tinymce, setTimeout ) {
14 tinymce.PluginManager.add( 'wptextpattern', function( editor ) {
15 var VK = tinymce.util.VK,
17 { regExp: /^[*-]\s/, cmd: 'InsertUnorderedList' },
18 { regExp: /^1[.)]\s/, cmd: 'InsertOrderedList' }
21 { start: '##', format: 'h2' },
22 { start: '###', format: 'h3' },
23 { start: '####', format: 'h4' },
24 { start: '#####', format: 'h5' },
25 { start: '######', format: 'h6' },
26 { start: '>', format: 'blockquote' }
28 canUndo, refNode, refPattern;
30 editor.on( 'selectionchange', function() {
34 editor.on( 'keydown', function( event ) {
35 if ( ( canUndo && event.keyCode === 27 /* ESCAPE */ ) || ( canUndo === 'space' && event.keyCode === VK.BACKSPACE ) ) {
36 editor.undoManager.undo();
37 event.preventDefault();
38 event.stopImmediatePropagation();
41 if ( event.keyCode === VK.ENTER && ! VK.modifierPressed( event ) ) {
46 editor.on( 'keyup', function( event ) {
47 if ( event.keyCode === VK.SPACEBAR && ! event.ctrlKey && ! event.metaKey && ! event.altKey ) {
49 } else if ( event.keyCode === VK.ENTER && ! VK.modifierPressed( event ) ) {
54 function firstTextNode( node ) {
55 var parent = editor.dom.getParent( node, 'p' ),
62 while ( child = parent.firstChild ) {
63 if ( child.nodeType !== 3 ) {
75 if ( child.nextSibling && child.nextSibling.nodeType === 3 ) {
76 child = child.nextSibling;
86 var rng = editor.selection.getRng(),
87 node = rng.startContainer,
91 if ( ! node || firstTextNode( node ) !== node ) {
95 parent = node.parentNode;
98 tinymce.each( spacePatterns, function( pattern ) {
99 var match = text.match( pattern.regExp );
101 if ( ! match || rng.startOffset !== match[0].length ) {
105 editor.undoManager.add();
107 editor.undoManager.transact( function() {
108 node.deleteData( 0, match[0].length );
110 if ( ! parent.innerHTML ) {
111 parent.appendChild( document.createElement( 'br' ) );
114 editor.selection.setCursorLocation( parent );
115 editor.execCommand( pattern.cmd );
118 // We need to wait for native events to be triggered.
119 setTimeout( function() {
127 function watchEnter() {
128 var rng = editor.selection.getRng(),
129 start = rng.startContainer,
130 node = firstTextNode( start ),
131 i = enterPatterns.length,
141 if ( text.indexOf( enterPatterns[ i ].start ) === 0 ) {
142 pattern = enterPatterns[ i ];
151 if ( node === start && tinymce.trim( text ) === pattern.start ) {
156 refPattern = pattern;
161 editor.undoManager.add();
163 editor.undoManager.transact( function() {
164 editor.formatter.apply( refPattern.format, {}, refNode );
165 refNode.replaceData( 0, refNode.data.length, tinymce.trim( refNode.data.slice( refPattern.start.length ) ) );
168 // We need to wait for native events to be triggered.
169 setTimeout( function() {
178 } )( window.tinymce, window.setTimeout );