undoediting.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * @license Copyright (c) 2003-2019, 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 not add a batch that has only non-document operations', () => {
  62. sinon.spy( undo._undoCommand, 'addBatch' );
  63. model.change( writer => {
  64. const docFrag = writer.createDocumentFragment();
  65. const element = writer.createElement( 'paragraph' );
  66. writer.insert( element, docFrag, 0 );
  67. writer.insertText( 'foo', null, element, 0 );
  68. } );
  69. expect( undo._undoCommand.addBatch.called ).to.be.false;
  70. } );
  71. it( 'should add a batch that has both document and non-document operations', () => {
  72. sinon.spy( undo._undoCommand, 'addBatch' );
  73. model.change( writer => {
  74. const element = writer.createElement( 'paragraph' );
  75. writer.insertText( 'foo', null, element, 0 );
  76. writer.insert( element, root, 0 );
  77. } );
  78. expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
  79. } );
  80. it( 'should set CTRL+Z keystroke', () => {
  81. const spy = sinon.stub( editor, 'execute' );
  82. const wasHandled = editor.keystrokes.press( {
  83. keyCode: keyCodes.z,
  84. ctrlKey: true,
  85. preventDefault: sinon.spy(),
  86. stopPropagation: sinon.spy()
  87. } );
  88. expect( wasHandled ).to.be.true;
  89. expect( spy.calledWithExactly( 'undo' ) ).to.be.true;
  90. } );
  91. it( 'should set CTRL+Y keystroke', () => {
  92. const spy = sinon.stub( editor, 'execute' );
  93. const wasHandled = editor.keystrokes.press( {
  94. keyCode: keyCodes.y,
  95. ctrlKey: true,
  96. preventDefault: sinon.spy(),
  97. stopPropagation: sinon.spy()
  98. } );
  99. expect( wasHandled ).to.be.true;
  100. expect( spy.calledWithExactly( 'redo' ) ).to.be.true;
  101. } );
  102. it( 'should set CTRL+SHIFT+Z keystroke', () => {
  103. const spy = sinon.stub( editor, 'execute' );
  104. const keyEventData = {
  105. keyCode: keyCodes.z,
  106. ctrlKey: true,
  107. shiftKey: true,
  108. preventDefault: sinon.spy(),
  109. stopPropagation: sinon.spy()
  110. };
  111. const wasHandled = editor.keystrokes.press( keyEventData );
  112. expect( wasHandled ).to.be.true;
  113. expect( spy.calledWithExactly( 'redo' ) ).to.be.true;
  114. expect( keyEventData.preventDefault.calledOnce ).to.be.true;
  115. } );
  116. } );