Ticket #36585: 36585.3.diff
File 36585.3.diff, 6.5 KB (added by , 7 years ago) |
---|
-
src/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js
39 39 40 40 var canUndo; 41 41 var chars = []; 42 var contentChangeCallback = null; 42 43 43 44 tinymce.each( inlinePatterns, function( pattern ) { 44 45 tinymce.each( ( pattern.start + pattern.end ).split( '' ), function( c ) { … … 48 49 } ); 49 50 } ); 50 51 51 editor.on( 'selectionchange', function() {52 canUndo = null;53 } );54 55 52 editor.on( 'keydown', function( event ) { 56 53 if ( ( canUndo && event.keyCode === 27 /* ESCAPE */ ) || ( canUndo === 'space' && event.keyCode === VK.BACKSPACE ) ) { 57 54 editor.undoManager.undo(); … … 62 59 if ( event.keyCode === VK.ENTER && ! VK.modifierPressed( event ) ) { 63 60 enter(); 64 61 } 65 }, true );66 62 67 editor.on( 'keyup', function( event ) { 63 function queueContentChangeCallback( callback ) { 64 if ( 'onselectionchange' in editor.getDoc() ) { 65 contentChangeCallback = callback; 66 } else { 67 // Gecko doesn't support the 'selectionchange' event 68 setTimeout( callback, 0 ); 69 } 70 } 71 72 // We need to execute space() and inline() after 'keydown' because 73 // they depend on the editor content being updated with the keypress in 74 // question, and before 'keyup' because other keys may be pressed 75 // before then. So queue the callback and execute it during the 76 // 'selectionchange' event that immediately follows the keypress. 68 77 if ( event.keyCode === VK.SPACEBAR && ! event.ctrlKey && ! event.metaKey && ! event.altKey ) { 69 space();78 queueContentChangeCallback( space ); 70 79 } else if ( event.keyCode > 47 && ! ( event.keyCode >= 91 && event.keyCode <= 93 ) ) { 71 inline();80 queueContentChangeCallback( inline ); 72 81 } 82 }, true ); 83 84 editor.on( 'selectionchange', function( event ) { 85 canUndo = null; 86 if ( contentChangeCallback ) { 87 contentChangeCallback(); 88 contentChangeCallback = null; 89 } 73 90 } ); 74 91 75 92 function inline() { -
tests/qunit/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js
6 6 return; 7 7 } 8 8 9 function mceType(chr) { 10 var editor = tinymce.activeEditor, keyCode, charCode, evt, startElm, rng, startContainer, startOffset, textNode; 9 function mceType( chr, eventType ) { 10 var editor = tinymce.activeEditor, 11 doKeyDown = ! eventType || /\bkeydown\b/.test( eventType ), 12 doKeyPress = ! eventType || /\bkeypress\b/.test( eventType ), 13 doKeyUp = ! eventType || /\bkeyup\b/.test( eventType ), 14 keyCode, charCode, evt, 15 startElm, rng, startContainer, startOffset, 16 textNode; 11 17 12 18 function charCodeToKeyCode(charCode) { 13 19 var lookup = { … … 56 62 evt = evt || {keyCode: keyCode, charCode: charCode}; 57 63 58 64 startElm = editor.selection.getStart(); 59 fakeEvent(startElm, 'keydown', evt); 60 fakeEvent(startElm, 'keypress', evt); 65 if ( doKeyDown ) { 66 fakeEvent( startElm, 'keydown', evt ); 67 } 68 if ( doKeyPress ) { 69 fakeEvent( startElm, 'keypress', evt ); 70 } 61 71 62 if ( !evt.isDefaultPrevented()) {72 if ( ( doKeyDown || doKeyPress ) && ! evt.isDefaultPrevented() ) { 63 73 if (keyCode === 8) { 64 74 if (editor.getDoc().selection) { 65 75 rng = editor.getDoc().selection.createRange(); … … 107 117 rng.collapse(true); 108 118 editor.selection.setRng(rng); 109 119 } 120 editor.fire( 'selectionchange' ); 110 121 } 111 122 112 fakeEvent(startElm, 'keyup', evt); 123 if ( doKeyUp ) { 124 fakeEvent( startElm, 'keyup', evt ); 125 } 113 126 } 114 127 115 128 function type() { … … 219 232 }, assert.async() ); 220 233 } ); 221 234 222 QUnit.test( 'Only transform when atthe cursor is at the start.', function( assert ) {235 QUnit.test( 'Only transform when the cursor is at the start.', function( assert ) { 223 236 editor.setContent( '<p>* test</p>' ); 224 237 editor.selection.setCursorLocation( editor.$( 'p' )[0].firstChild, 6 ); 225 238 … … 228 241 }, assert.async() ); 229 242 } ); 230 243 244 QUnit.test( 'Transform when space pressed along with another key. (1)', function( assert ) { 245 var done = assert.async(); 246 247 editor.setContent( '<p>*' ); 248 editor.selection.setCursorLocation( editor.$( 'p' )[0].firstChild, 1 ); 249 250 mceType( ' ', 'keydown,keypress' ); 251 mceType( 'a', 'keydown,keypress' ); 252 mceType( ' ', 'keyup' ); 253 mceType( 'a', 'keyup' ); 254 255 setTimeout( function() { 256 assert.equal( editor.getContent(), '<ul>\n<li>a</li>\n</ul>' ); 257 assert.equal( editor.selection.getRng().startOffset, 1 ); 258 done(); 259 } ); 260 } ); 261 262 QUnit.test( 'Transform when space pressed along with another key. (2)', function( assert ) { 263 var done = assert.async(); 264 265 editor.setContent( '<p>*' ); 266 editor.selection.setCursorLocation( editor.$( 'p' )[0].firstChild, 1 ); 267 268 mceType( ' ', 'keydown,keypress' ); 269 mceType( '0', 'keydown,keypress' ); 270 mceType( '0', 'keyup' ); 271 mceType( ' ', 'keyup' ); 272 273 setTimeout( function() { 274 assert.equal( editor.getContent(), '<ul>\n<li>0</li>\n</ul>' ); 275 assert.equal( editor.selection.getRng().startOffset, 1 ); 276 done(); 277 } ); 278 } ); 279 231 280 QUnit.test( 'Backspace should undo the transformation.', function( assert ) { 232 281 editor.setContent( '<p>test</p>' ); 233 282 editor.selection.setCursorLocation(); … … 309 358 assert.equal( editor.getContent(), '<p>test `````</p>' ); 310 359 }, assert.async() ); 311 360 } ); 361 362 QUnit.test( 'Inline: backtick pressed along with another key. (1)', function( assert ) { 363 type( '`abc', function() { 364 mceType( '`', 'keydown,keypress' ); 365 mceType( 'a', 'keydown,keypress' ); 366 mceType( '`', 'keyup' ); 367 mceType( 'a', 'keyup' ); 368 }, function() { 369 assert.equal( editor.getContent(), '<p><code>abc</code>a</p>' ); 370 assert.equal( editor.selection.getRng().startOffset, 2 ); 371 }, assert.async() ); 372 } ); 373 374 QUnit.test( 'Inline: backtick pressed along with another key. (2)', function( assert ) { 375 type( '`abc', function() { 376 mceType( '`', 'keydown,keypress' ); 377 mceType( 'a', 'keydown,keypress' ); 378 mceType( 'a', 'keyup' ); 379 mceType( '`', 'keyup' ); 380 }, function() { 381 assert.equal( editor.getContent(), '<p><code>abc</code>a</p>' ); 382 assert.equal( editor.selection.getRng().startOffset, 2 ); 383 }, assert.async() ); 384 } ); 385 312 386 } )( window.jQuery, window.QUnit, window.tinymce, window.setTimeout );