| 1 | ( function( tinymce ) { |
| 2 | tinymce.PluginManager.add( 'wptextpattern', function( editor ) { |
| 3 | var patterns = []; |
| 4 | |
| 5 | function add( regExp, callback ) { |
| 6 | patterns.push( { |
| 7 | regExp: regExp, |
| 8 | callback: callback |
| 9 | } ); |
| 10 | } |
| 11 | |
| 12 | add( /^[*-]\s/, function() { |
| 13 | this.execCommand( 'InsertUnorderedList' ); |
| 14 | } ); |
| 15 | |
| 16 | add( /^1[.)]\s/, function() { |
| 17 | this.execCommand( 'InsertOrderedList' ); |
| 18 | } ); |
| 19 | |
| 20 | add( /^>\s/, function() { |
| 21 | this.execCommand( 'mceBlockQuote' ); |
| 22 | } ); |
| 23 | |
| 24 | editor.on( 'keyup', function( event ) { |
| 25 | var node; |
| 26 | |
| 27 | if ( event.keyCode !== tinymce.util.VK.SPACEBAR ) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | node = editor.selection.getRng().startContainer; |
| 32 | |
| 33 | if ( node.nodeType !== 3 ) { |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | tinymce.each( patterns, function( pattern ) { |
| 38 | var text = node.nodeValue, |
| 39 | replace; |
| 40 | |
| 41 | if ( node.parentNode.firstChild !== node ) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | replace = text.replace( pattern.regExp, '' ); |
| 46 | |
| 47 | if ( text === replace ) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | editor.undoManager.transact( function() { |
| 52 | node.parentNode.replaceChild( |
| 53 | replace ? document.createTextNode( replace ) : document.createElement( 'BR' ), |
| 54 | node |
| 55 | ); |
| 56 | |
| 57 | pattern.callback.apply( editor ); |
| 58 | } ); |
| 59 | |
| 60 | return false; |
| 61 | } ); |
| 62 | } ); |
| 63 | } ); |
| 64 | } )( window.tinymce ); |