8
0

undoediting.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import UndoEditing from '../src/undoediting';
  7. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  8. describe( 'UndoEditing', () => {
  9. let editor, undo, model, root;
  10. beforeEach( () => {
  11. editor = new ModelTestEditor();
  12. model = editor.model;
  13. root = model.document.getRoot();
  14. undo = new UndoEditing( editor );
  15. undo.init();
  16. } );
  17. afterEach( () => {
  18. undo.destroy();
  19. } );
  20. it( 'should register undo command and redo command', () => {
  21. expect( editor.commands.get( 'undo' ) ).to.equal( undo._undoCommand );
  22. expect( editor.commands.get( 'redo' ) ).to.equal( undo._redoCommand );
  23. } );
  24. it( 'should add a batch to undo command and clear redo stack, if it\'s type is "default"', () => {
  25. sinon.spy( undo._undoCommand, 'addBatch' );
  26. sinon.spy( undo._redoCommand, 'clearStack' );
  27. expect( undo._undoCommand.addBatch.called ).to.be.false;
  28. expect( undo._redoCommand.clearStack.called ).to.be.false;
  29. model.change( writer => {
  30. writer.insertText( 'foobar', root );
  31. } );
  32. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  33. expect( undo._redoCommand.clearStack.calledOnce ).to.be.true;
  34. } );
  35. it( 'should add each batch only once', () => {
  36. sinon.spy( undo._undoCommand, 'addBatch' );
  37. model.change( writer => {
  38. writer.insertText( 'foobar', root );
  39. writer.insertText( 'foobar', root );
  40. } );
  41. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  42. } );
  43. it( 'should add a batch to undo command, if it\'s type is undo and it comes from redo command', () => {
  44. sinon.spy( undo._undoCommand, 'addBatch' );
  45. sinon.spy( undo._redoCommand, 'clearStack' );
  46. const batch = model.createBatch();
  47. undo._redoCommand._createdBatches.add( batch );
  48. model.enqueueChange( batch, writer => {
  49. writer.insertText( 'foobar', root );
  50. } );
  51. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  52. expect( undo._redoCommand.clearStack.called ).to.be.false;
  53. } );
  54. it( 'should add a batch to redo command on undo revert event', () => {
  55. sinon.spy( undo._redoCommand, 'addBatch' );
  56. sinon.spy( undo._redoCommand, 'clearStack' );
  57. undo._undoCommand.fire( 'revert', null, model.createBatch() );
  58. expect( undo._redoCommand.addBatch.calledOnce ).to.be.true;
  59. expect( undo._redoCommand.clearStack.called ).to.be.false;
  60. } );
  61. it( 'should add redo batch to undo', () => {
  62. sinon.spy( undo._undoCommand, 'addBatch' );
  63. model.change( writer => {
  64. writer.insertText( 'foobar', root );
  65. } );
  66. model.change( writer => {
  67. writer.insertText( 'baz', root );
  68. } );
  69. editor.execute( 'undo' );
  70. editor.execute( 'undo' );
  71. editor.execute( 'redo' );
  72. sinon.assert.calledThrice( undo._undoCommand.addBatch );
  73. editor.execute( 'redo' );
  74. sinon.assert.callCount( undo._undoCommand.addBatch, 4 );
  75. } );
  76. it( 'should not add a batch that has only non-document operations', () => {
  77. sinon.spy( undo._undoCommand, 'addBatch' );
  78. model.change( writer => {
  79. const docFrag = writer.createDocumentFragment();
  80. const element = writer.createElement( 'paragraph' );
  81. writer.insert( element, docFrag, 0 );
  82. writer.insertText( 'foo', null, element, 0 );
  83. } );
  84. expect( undo._undoCommand.addBatch.called ).to.be.false;
  85. } );
  86. it( 'should not add a transparent batch', () => {
  87. sinon.spy( undo._undoCommand, 'addBatch' );
  88. model.enqueueChange( 'transparent', writer => {
  89. writer.insertText( 'foobar', root );
  90. } );
  91. expect( undo._undoCommand.addBatch.called ).to.be.false;
  92. } );
  93. it( 'should add a batch that has both document and non-document operations', () => {
  94. sinon.spy( undo._undoCommand, 'addBatch' );
  95. model.change( writer => {
  96. const element = writer.createElement( 'paragraph' );
  97. writer.insertText( 'foo', null, element, 0 );
  98. writer.insert( element, root, 0 );
  99. } );
  100. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  101. } );
  102. it( 'should set CTRL+Z keystroke', () => {
  103. const spy = sinon.stub( editor, 'execute' );
  104. const wasHandled = editor.keystrokes.press( {
  105. keyCode: keyCodes.z,
  106. ctrlKey: true,
  107. preventDefault: sinon.spy(),
  108. stopPropagation: sinon.spy()
  109. } );
  110. expect( wasHandled ).to.be.true;
  111. expect( spy.calledWithExactly( 'undo' ) ).to.be.true;
  112. } );
  113. it( 'should set CTRL+Y keystroke', () => {
  114. const spy = sinon.stub( editor, 'execute' );
  115. const wasHandled = editor.keystrokes.press( {
  116. keyCode: keyCodes.y,
  117. ctrlKey: true,
  118. preventDefault: sinon.spy(),
  119. stopPropagation: sinon.spy()
  120. } );
  121. expect( wasHandled ).to.be.true;
  122. expect( spy.calledWithExactly( 'redo' ) ).to.be.true;
  123. } );
  124. it( 'should set CTRL+SHIFT+Z keystroke', () => {
  125. const spy = sinon.stub( editor, 'execute' );
  126. const keyEventData = {
  127. keyCode: keyCodes.z,
  128. ctrlKey: true,
  129. shiftKey: true,
  130. preventDefault: sinon.spy(),
  131. stopPropagation: sinon.spy()
  132. };
  133. const wasHandled = editor.keystrokes.press( keyEventData );
  134. expect( wasHandled ).to.be.true;
  135. expect( spy.calledWithExactly( 'redo' ) ).to.be.true;
  136. expect( keyEventData.preventDefault.calledOnce ).to.be.true;
  137. } );
  138. } );