redocommand.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  7. import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  8. import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
  9. import UndoCommand from '../src/undocommand';
  10. import RedoCommand from '../src/redocommand';
  11. import { itemAt, getText } from '@ckeditor/ckeditor5-engine/tests/model/_utils/utils';
  12. describe( 'RedoCommand', () => {
  13. let editor, model, root, redo, undo;
  14. beforeEach( () => {
  15. return ModelTestEditor.create().then( newEditor => {
  16. editor = newEditor;
  17. redo = new RedoCommand( editor );
  18. model = editor.model;
  19. root = model.document.getRoot();
  20. } );
  21. } );
  22. afterEach( () => {
  23. return editor.destroy();
  24. } );
  25. describe( 'RedoCommand', () => {
  26. describe( 'execute()', () => {
  27. const p = pos => new Position( root, [].concat( pos ) );
  28. const r = ( a, b ) => new Range( p( a ), p( b ) );
  29. let batch0, batch1, batch2;
  30. const batches = new Set();
  31. beforeEach( () => {
  32. undo = new UndoCommand( editor );
  33. // Simple integration with undo.
  34. undo.on( 'revert', ( evt, undoneBatch, undoingBatch ) => {
  35. if ( !batches.has( undoingBatch ) ) {
  36. redo.addBatch( undoingBatch );
  37. batches.add( undoingBatch );
  38. }
  39. } );
  40. /*
  41. [root]
  42. - {}
  43. */
  44. model.change( writer => {
  45. writer.setSelection( r( 0, 0 ) );
  46. } );
  47. batch0 = new Batch();
  48. undo.addBatch( batch0 );
  49. model.enqueueChange( batch0, writer => {
  50. writer.insertText( 'foobar', p( 0 ) );
  51. } );
  52. /*
  53. [root]
  54. - f
  55. - o
  56. - o
  57. - b
  58. - a
  59. - r{}
  60. */
  61. // Let's make things spicy and this time, make a backward selection.
  62. model.change( writer => {
  63. writer.setSelection( r( 2, 4 ), { backward: true } );
  64. } );
  65. batch1 = new Batch();
  66. undo.addBatch( batch1 );
  67. model.enqueueChange( batch1, writer => {
  68. writer.setAttribute( 'key', 'value', r( 2, 4 ) );
  69. } );
  70. /*
  71. [root]
  72. - f
  73. - o
  74. - {o (key: value)
  75. - b} (key: value)
  76. - a
  77. - r
  78. */
  79. model.change( writer => {
  80. writer.setSelection( r( 1, 3 ) );
  81. } );
  82. batch2 = new Batch();
  83. undo.addBatch( batch2 );
  84. model.enqueueChange( batch2, writer => {
  85. writer.move( r( 1, 3 ), p( 6 ) );
  86. } );
  87. /*
  88. [root]
  89. - f
  90. - b (key: value)
  91. - a
  92. - r
  93. - {o
  94. - o} (key: value)
  95. */
  96. } );
  97. it( 'should redo batch undone by undo command', () => {
  98. undo.execute( batch2 );
  99. redo.execute();
  100. // Should be back at original state:
  101. /*
  102. [root]
  103. - f
  104. - b (key: value)
  105. - a
  106. - r
  107. - {o
  108. - o} (key: value)
  109. */
  110. expect( getText( root ) ).to.equal( 'fbaroo' );
  111. expect( itemAt( root, 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  112. expect( itemAt( root, 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
  113. expect( editor.model.document.selection.getRanges().next().value.isEqual( r( 4, 6 ) ) ).to.be.true;
  114. expect( editor.model.document.selection.isBackward ).to.be.false;
  115. } );
  116. it( 'should redo series of batches undone by undo command', () => {
  117. undo.execute();
  118. undo.execute();
  119. undo.execute();
  120. redo.execute();
  121. // Should be like after applying `batch0`:
  122. /*
  123. [root]
  124. - f
  125. - o
  126. - o
  127. - b
  128. - a
  129. - r{}
  130. */
  131. expect( getText( root ) ).to.equal( 'foobar' );
  132. expect( Array.from( root.getChildren() ).find( node => node.hasAttribute( 'key' ) ) ).to.be.undefined;
  133. expect( editor.model.document.selection.getRanges().next().value.isEqual( r( 6, 6 ) ) ).to.be.true;
  134. expect( editor.model.document.selection.isBackward ).to.be.false;
  135. redo.execute();
  136. // Should be like after applying `batch1`:
  137. /*
  138. [root]
  139. - f
  140. - o
  141. - {o (key: value)
  142. - b} (key: value)
  143. - a
  144. - r
  145. */
  146. expect( getText( root ) ).to.equal( 'foobar' );
  147. expect( itemAt( root, 2 ).getAttribute( 'key' ) ).to.equal( 'value' );
  148. expect( itemAt( root, 3 ).getAttribute( 'key' ) ).to.equal( 'value' );
  149. expect( editor.model.document.selection.getRanges().next().value.isEqual( r( 2, 4 ) ) ).to.be.true;
  150. expect( editor.model.document.selection.isBackward ).to.be.true;
  151. redo.execute();
  152. // Should be like after applying `batch2`:
  153. /*
  154. [root]
  155. - f
  156. - b (key: value)
  157. - a
  158. - r
  159. - {o
  160. - o} (key: value)
  161. */
  162. expect( getText( root ) ).to.equal( 'fbaroo' );
  163. expect( itemAt( root, 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  164. expect( itemAt( root, 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
  165. expect( editor.model.document.selection.getRanges().next().value.isEqual( r( 4, 6 ) ) ).to.be.true;
  166. expect( editor.model.document.selection.isBackward ).to.be.false;
  167. } );
  168. it( 'should redo batch selectively undone by undo command', () => {
  169. undo.execute( batch0 );
  170. redo.execute();
  171. // Should be back to original state:
  172. /*
  173. [root]
  174. - f
  175. - b (key: value)
  176. - a
  177. - r
  178. - o
  179. - o{} (key: value)
  180. */
  181. expect( getText( root ) ).to.equal( 'fbaroo' );
  182. expect( itemAt( root, 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  183. expect( itemAt( root, 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
  184. expect( editor.model.document.selection.getRanges().next().value.isEqual( r( 6, 6 ) ) ).to.be.true;
  185. expect( editor.model.document.selection.isBackward ).to.be.false;
  186. } );
  187. it( 'should redo batch selectively undone by undo command #2', () => {
  188. undo.execute( batch1 );
  189. undo.execute( batch2 );
  190. redo.execute();
  191. redo.execute();
  192. // Should be back to original state:
  193. /*
  194. [root]
  195. - f
  196. - {b} (key: value)
  197. - a
  198. - r
  199. - o
  200. - o (key: value)
  201. */
  202. expect( getText( root ) ).to.equal( 'fbaroo' );
  203. expect( itemAt( root, 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  204. expect( itemAt( root, 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
  205. expect( editor.model.document.selection.getRanges().next().value.isEqual( r( 1, 2 ) ) ).to.be.true;
  206. expect( editor.model.document.selection.isBackward ).to.be.true;
  207. } );
  208. it( 'should transform redo batch by changes written in history that happened after undo but before redo #2', () => {
  209. // Now it is "fBaroO".
  210. // Undo moving "oo" to the end of string. Now it is "foOBar". Capitals mean set attribute.
  211. undo.execute();
  212. // Remove "ar".
  213. model.change( writer => {
  214. writer.remove( r( 4, 6 ) );
  215. } );
  216. // Undo setting attribute on "ob". Now it is "foob".
  217. undo.execute();
  218. // Append "xx" at the beginning. Now it is "xxfoob".
  219. model.change( writer => {
  220. writer.insertText( 'xx', p( 0 ) );
  221. } );
  222. // Redo setting attribute on "ob". Now it is "xxfoOB".
  223. redo.execute();
  224. expect( getText( root ) ).to.equal( 'xxfoob' );
  225. expect( itemAt( root, 4 ).getAttribute( 'key' ) ).to.equal( 'value' );
  226. expect( itemAt( root, 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
  227. expect( editor.model.document.selection.getFirstRange().isEqual( r( 4, 6 ) ) ).to.be.true;
  228. expect( editor.model.document.selection.isBackward ).to.be.true;
  229. // Redo moving "oo". Now it is "xxfBoO". Selection is expected to be on just moved "oO".
  230. redo.execute();
  231. expect( getText( root ) ).to.equal( 'xxfboo' );
  232. expect( itemAt( root, 3 ).getAttribute( 'key' ) ).to.equal( 'value' );
  233. expect( itemAt( root, 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
  234. expect( editor.model.document.selection.getFirstRange().isEqual( r( 4, 6 ) ) ).to.be.true;
  235. expect( editor.model.document.selection.isBackward ).to.be.false;
  236. } );
  237. it( 'should pass redoing batch to enqueueChange method', () => {
  238. undo.execute( batch2 );
  239. const enqueueChangeSpy = sinon.spy( model, 'enqueueChange' );
  240. const undoSpy = sinon.spy( redo, '_undo' );
  241. redo.execute();
  242. sinon.assert.calledOnce( enqueueChangeSpy );
  243. sinon.assert.calledOnce( undoSpy );
  244. const redoingBatch = enqueueChangeSpy.firstCall.args[ 0 ];
  245. expect( redoingBatch instanceof Batch ).to.be.true;
  246. expect( undoSpy.firstCall.args[ 1 ] ).to.equal( redoingBatch );
  247. } );
  248. } );
  249. } );
  250. } );