editingkeystrokehandler.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import VirtualTestEditor from '../tests/_utils/virtualtesteditor';
  6. import EditingKeystrokeHandler from '../src/editingkeystrokehandler';
  7. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  8. describe( 'EditingKeystrokeHandler', () => {
  9. let editor, keystrokes, executeSpy;
  10. beforeEach( () => {
  11. return VirtualTestEditor.create()
  12. .then( newEditor => {
  13. editor = newEditor;
  14. keystrokes = new EditingKeystrokeHandler( editor );
  15. executeSpy = sinon.stub( editor, 'execute' );
  16. } );
  17. } );
  18. describe( 'set()', () => {
  19. describe( 'with a command', () => {
  20. it( 'prevents default when the keystroke was handled', () => {
  21. const keyEvtData = getCtrlA();
  22. keystrokes.set( 'Ctrl+A', 'foo' );
  23. keystrokes.press( keyEvtData );
  24. sinon.assert.calledWithExactly( executeSpy, 'foo' );
  25. sinon.assert.calledOnce( keyEvtData.preventDefault );
  26. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  27. } );
  28. it( 'does not prevent default when the keystroke was not handled', () => {
  29. const keyEvtData = getCtrlA();
  30. keystrokes.press( keyEvtData );
  31. sinon.assert.notCalled( executeSpy );
  32. sinon.assert.notCalled( keyEvtData.preventDefault );
  33. sinon.assert.notCalled( keyEvtData.stopPropagation );
  34. } );
  35. it( 'provides a callback which stops the event and remaining callbacks in the keystroke handler', () => {
  36. const spy1 = sinon.spy();
  37. const spy2 = sinon.spy();
  38. const spy3 = sinon.spy();
  39. keystrokes.set( [ 'ctrl', 'A' ], spy1 );
  40. keystrokes.set( [ 'ctrl', 'A' ], spy2, { priority: 'high' } );
  41. keystrokes.set( [ 'ctrl', 'A' ], 'foo', { priority: 'low' } );
  42. keystrokes.set( [ 'ctrl', 'A' ], ( keyEvtData, cancel ) => {
  43. spy3();
  44. cancel();
  45. } );
  46. keystrokes.press( getCtrlA() );
  47. sinon.assert.callOrder( spy2, spy1, spy3 );
  48. sinon.assert.notCalled( executeSpy );
  49. } );
  50. } );
  51. describe( 'with a callback', () => {
  52. it( 'never prevents default', () => {
  53. const callback = sinon.spy();
  54. const keyEvtData = getCtrlA();
  55. keystrokes.set( 'Ctrl+A', callback );
  56. keystrokes.press( keyEvtData );
  57. sinon.assert.calledOnce( callback );
  58. sinon.assert.notCalled( keyEvtData.preventDefault );
  59. sinon.assert.notCalled( keyEvtData.stopPropagation );
  60. } );
  61. } );
  62. it( 'supports priorities', () => {
  63. const spy1 = sinon.spy();
  64. const spy2 = sinon.spy();
  65. const spy3 = sinon.spy();
  66. keystrokes.set( [ 'ctrl', 'A' ], spy1 );
  67. keystrokes.set( [ 'ctrl', 'A' ], spy2, { priority: 'high' } );
  68. keystrokes.set( [ 'ctrl', 'A' ], 'foo', { priority: 'low' } );
  69. keystrokes.set( [ 'ctrl', 'A' ], spy3 );
  70. keystrokes.press( getCtrlA() );
  71. sinon.assert.callOrder( spy2, spy1, spy3, executeSpy );
  72. } );
  73. } );
  74. describe( 'press()', () => {
  75. it( 'executes a command', () => {
  76. keystrokes.set( 'Ctrl+A', 'foo' );
  77. const wasHandled = keystrokes.press( getCtrlA() );
  78. sinon.assert.calledOnce( executeSpy );
  79. sinon.assert.calledWithExactly( executeSpy, 'foo' );
  80. expect( wasHandled ).to.be.true;
  81. } );
  82. it( 'executes a callback', () => {
  83. const callback = sinon.spy();
  84. keystrokes.set( 'Ctrl+A', callback );
  85. const wasHandled = keystrokes.press( getCtrlA() );
  86. expect( executeSpy.called ).to.be.false;
  87. expect( callback.calledOnce ).to.be.true;
  88. expect( wasHandled ).to.be.true;
  89. } );
  90. } );
  91. } );
  92. function getCtrlA() {
  93. return {
  94. keyCode: keyCodes.a,
  95. ctrlKey: true,
  96. preventDefault: sinon.spy(),
  97. stopPropagation: sinon.spy()
  98. };
  99. }