Ticket #31441: 31441.22.patch
File 31441.22.patch, 6.3 KB (added by , 10 years ago) |
---|
-
src/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js
14 14 tinymce.PluginManager.add( 'wptextpattern', function( editor ) { 15 15 var $$ = editor.$, 16 16 VK = tinymce.util.VK, 17 patterns = [], 18 canUndo = false; 19 20 /** 21 * Add a pattern to format with a callback. 22 * 23 * @since 4.3.0 24 * 25 * @param {RegExp} regExp RegEx pattern. 26 * @param {Function} callback Callback. 27 */ 28 function add( regExp, callback ) { 29 patterns.push( { 30 regExp: regExp, 31 callback: callback 32 } ); 33 } 34 35 add( /^[*-]\s/, function() { 36 this.execCommand( 'InsertUnorderedList' ); 37 } ); 38 39 add( /^1[.)]\s/, function() { 40 this.execCommand( 'InsertOrderedList' ); 41 } ); 42 43 add( /^>\s/, function() { 44 this.formatter.toggle( 'blockquote' ); 45 } ); 46 47 add( /^(#{2,6})\s/, function() { 48 this.formatter.toggle( 'h' + arguments[1].length ); 49 } ); 17 canUndo = false, 18 spacePatterns = [ 19 { regExp: /^[*-]\s/, cmd: 'InsertUnorderedList' }, 20 { regExp: /^1[.)]\s/, cmd: 'InsertOrderedList' } 21 ], 22 enterPatterns = [ 23 { start: '##', format: 'h2' }, 24 { start: '###', format: 'h3' }, 25 { start: '####', format: 'h4' }, 26 { start: '#####', format: 'h5' }, 27 { start: '######', format: 'h6' }, 28 { start: '>', format: 'blockquote' } 29 ]; 50 30 51 31 editor.on( 'selectionchange', function() { 52 32 canUndo = false; … … 57 37 editor.undoManager.undo(); 58 38 event.preventDefault(); 59 39 } 60 } );61 40 62 editor.on( 'keyup', function( event ) { 63 var rng, node, text, parent, child; 64 65 if ( event.keyCode !== VK.SPACEBAR ) { 66 return; 41 if ( event.keyCode === VK.ENTER && ! VK.modifierPressed( event ) ) { 42 enter(); 67 43 } 44 }, true ); 68 45 69 rng = editor.selection.getRng(); 70 node = rng.startContainer; 71 72 if ( ! node || node.nodeType !== 3 ) { 73 return; 46 editor.on( 'keyup', function( event ) { 47 if ( event.keyCode === VK.SPACEBAR || ! VK.modifierPressed( event ) ) { 48 space(); 74 49 } 50 } ); 75 51 76 text = node.nodeValue; 77 parent = editor.dom.getParent( node, 'p' ); 52 function firstNode( node ) { 53 var parent = editor.dom.getParent( node, 'p' ), 54 child; 78 55 79 56 if ( ! parent ) { 80 57 return; … … 92 69 return; 93 70 } 94 71 95 if ( ! child. nodeValue) {72 if ( ! child.data ) { 96 73 child = child.nextSibling; 97 74 } 98 75 99 if ( child !== node ) { 76 return child; 77 } 78 79 function space() { 80 var rng = editor.selection.getRng(), 81 node = rng.startContainer, 82 text; 83 84 if ( firstNode( node ) !== node ) { 100 85 return; 101 86 } 102 87 103 tinymce.each( patterns, function( pattern ) { 104 var args, 105 replace = text.replace( pattern.regExp, function() { 106 args = arguments; 107 return ''; 108 } ); 88 text = node.nodeValue; 89 90 tinymce.each( spacePatterns, function( pattern ) { 91 var replace = text.replace( pattern.regExp, '' ); 109 92 110 93 if ( text === replace ) { 111 94 return; … … 118 101 editor.undoManager.add(); 119 102 120 103 editor.undoManager.transact( function() { 121 var $$parent; 104 var parent = node.parentNode, 105 $$parent; 122 106 123 107 if ( replace ) { 124 108 $$( node ).replaceWith( document.createTextNode( replace ) ); 125 109 } else { 126 $$parent = $$( node.parentNode);110 $$parent = $$( parent ); 127 111 128 112 $$( node ).remove(); 129 113 … … 133 117 } 134 118 135 119 editor.selection.setCursorLocation( parent ); 136 137 pattern.callback.apply( editor, args ); 120 editor.execCommand( pattern.cmd ); 138 121 } ); 139 122 140 123 // We need to wait for native events to be triggered. … … 144 127 145 128 return false; 146 129 } ); 147 } ); 130 } 131 132 function enter() { 133 var selection = editor.selection, 134 rng = selection.getRng(), 135 offset = rng.startOffset, 136 start = rng.startContainer, 137 node = firstNode( start ), 138 text = node.data, 139 i = enterPatterns.length, 140 pattern; 141 142 if ( ! node ) { 143 return; 144 } 145 146 while ( i-- ) { 147 if ( text.indexOf( enterPatterns[ i ].start ) === 0 ) { 148 pattern = enterPatterns[ i ]; 149 break; 150 } 151 } 152 153 if ( ! pattern ) { 154 return; 155 } 156 157 if ( node === start && tinymce.trim( text ) === pattern.start ) { 158 return; 159 } 160 161 if ( node === start ) { 162 offset = Math.max( 0, offset - pattern.start.length ); 163 } 164 165 editor.undoManager.add(); 166 167 editor.undoManager.transact( function() { 168 node.deleteData( 0, pattern.start.length ); 169 170 editor.formatter.apply( pattern.format, {}, start ); 171 172 rng.setStart( start, offset ); 173 rng.collapse( true ); 174 selection.setRng( rng ); 175 176 console.log( editor.getContent() ); 177 } ); 178 } 148 179 } ); 149 180 } )( window.tinymce, window.setTimeout ); -
tests/qunit/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js
144 144 assert.equal( editor.getContent(), '<ul>\n<li>tes</li>\n</ul>' ); 145 145 }, assert.async() ); 146 146 } ); 147 148 QUnit.test( 'Heading 3', function( assert ) { 149 editor.setContent( '<p>### test</p>' ); 150 editor.selection.setCursorLocation( editor.$( 'p' )[0].firstChild, 8 ); 151 152 type( '\n', function() { 153 assert.equal( editor.getContent(), '<h3>test</h3>\n<p> </p>' ); 154 }, assert.async() ); 155 } ); 156 157 QUnit.test( 'Heading 3 with elements.', function( assert ) { 158 editor.setContent( '<p>###<del>test</del></p>' ); 159 editor.selection.setCursorLocation( editor.$( 'del' )[0].firstChild, 4 ); 160 161 type( '\n', function() { 162 assert.equal( editor.getContent(), '<h3><del>test</del></h3>\n<p> </p>' ); 163 }, assert.async() ); 164 } ); 165 166 QUnit.test( 'Don\'t convert without content', function( assert ) { 167 editor.setContent( '<p>### </p>' ); 168 editor.selection.setCursorLocation( editor.$( 'p' )[0].firstChild, 4 ); 169 170 type( '\n', function() { 171 assert.equal( editor.getContent(), '<p>### </p>\n<p> </p>' ); 172 }, assert.async() ); 173 } ); 147 174 } )( window.jQuery, window.QUnit, window.tinymce, window.Utils.type, window.setTimeout );