redocommand.js 8.3 KB

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