redocommand.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /**
  2. * @license Copyright (c) 2003-2017, 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. editor = new ModelTestEditor();
  16. redo = new RedoCommand( editor );
  17. model = editor.model;
  18. root = model.document.getRoot();
  19. } );
  20. afterEach( () => {
  21. return editor.destroy();
  22. } );
  23. describe( 'RedoCommand', () => {
  24. describe( 'execute()', () => {
  25. const p = pos => new Position( root, [].concat( pos ) );
  26. const r = ( a, b ) => new Range( 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. editor.model.document.selection.setRanges( [ r( 0, 0 ) ] );
  43. batch0 = new Batch();
  44. undo.addBatch( batch0 );
  45. model.enqueueChange( batch0, writer => {
  46. writer.insertText( 'foobar', p( 0 ) );
  47. } );
  48. /*
  49. [root]
  50. - f
  51. - o
  52. - o
  53. - b
  54. - a
  55. - r{}
  56. */
  57. // Let's make things spicy and this time, make a backward selection.
  58. editor.model.document.selection.setRanges( [ r( 2, 4 ) ], true );
  59. batch1 = new Batch();
  60. undo.addBatch( batch1 );
  61. model.enqueueChange( batch1, writer => {
  62. writer.setAttribute( 'key', 'value', r( 2, 4 ) );
  63. } );
  64. /*
  65. [root]
  66. - f
  67. - o
  68. - {o (key: value)
  69. - b} (key: value)
  70. - a
  71. - r
  72. */
  73. editor.model.document.selection.setRanges( [ r( 1, 3 ) ] );
  74. batch2 = new Batch();
  75. undo.addBatch( batch2 );
  76. model.enqueueChange( batch2, writer => {
  77. writer.move( r( 1, 3 ), p( 6 ) );
  78. } );
  79. /*
  80. [root]
  81. - f
  82. - b (key: value)
  83. - a
  84. - r
  85. - {o
  86. - o} (key: value)
  87. */
  88. } );
  89. it( 'should redo batch undone by undo command', () => {
  90. undo.execute( batch2 );
  91. redo.execute();
  92. // Should be back at original state:
  93. /*
  94. [root]
  95. - f
  96. - b (key: value)
  97. - a
  98. - r
  99. - {o
  100. - o} (key: value)
  101. */
  102. expect( getText( root ) ).to.equal( 'fbaroo' );
  103. expect( itemAt( root, 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  104. expect( itemAt( root, 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
  105. expect( editor.model.document.selection.getRanges().next().value.isEqual( r( 4, 6 ) ) ).to.be.true;
  106. expect( editor.model.document.selection.isBackward ).to.be.false;
  107. } );
  108. it( 'should redo series of batches undone by undo command', () => {
  109. undo.execute();
  110. undo.execute();
  111. undo.execute();
  112. redo.execute();
  113. // Should be like after applying `batch0`:
  114. /*
  115. [root]
  116. - f
  117. - o
  118. - o
  119. - b
  120. - a
  121. - r{}
  122. */
  123. expect( getText( root ) ).to.equal( 'foobar' );
  124. expect( Array.from( root.getChildren() ).find( node => node.hasAttribute( 'key' ) ) ).to.be.undefined;
  125. expect( editor.model.document.selection.getRanges().next().value.isEqual( r( 6, 6 ) ) ).to.be.true;
  126. expect( editor.model.document.selection.isBackward ).to.be.false;
  127. redo.execute();
  128. // Should be like after applying `batch1`:
  129. /*
  130. [root]
  131. - f
  132. - o
  133. - {o (key: value)
  134. - b} (key: value)
  135. - a
  136. - r
  137. */
  138. expect( getText( root ) ).to.equal( 'foobar' );
  139. expect( itemAt( root, 2 ).getAttribute( 'key' ) ).to.equal( 'value' );
  140. expect( itemAt( root, 3 ).getAttribute( 'key' ) ).to.equal( 'value' );
  141. expect( editor.model.document.selection.getRanges().next().value.isEqual( r( 2, 4 ) ) ).to.be.true;
  142. expect( editor.model.document.selection.isBackward ).to.be.true;
  143. redo.execute();
  144. // Should be like after applying `batch2`:
  145. /*
  146. [root]
  147. - f
  148. - b (key: value)
  149. - a
  150. - r
  151. - {o
  152. - o} (key: value)
  153. */
  154. expect( getText( root ) ).to.equal( 'fbaroo' );
  155. expect( itemAt( root, 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  156. expect( itemAt( root, 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
  157. expect( editor.model.document.selection.getRanges().next().value.isEqual( r( 4, 6 ) ) ).to.be.true;
  158. expect( editor.model.document.selection.isBackward ).to.be.false;
  159. } );
  160. it( 'should redo batch selectively undone by undo command', () => {
  161. undo.execute( batch0 );
  162. redo.execute();
  163. // Should be back to original state:
  164. /*
  165. [root]
  166. - f
  167. - b (key: value)
  168. - a
  169. - r
  170. - o
  171. - o{} (key: value)
  172. */
  173. expect( getText( root ) ).to.equal( 'fbaroo' );
  174. expect( itemAt( root, 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  175. expect( itemAt( root, 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
  176. expect( editor.model.document.selection.getRanges().next().value.isEqual( r( 6, 6 ) ) ).to.be.true;
  177. expect( editor.model.document.selection.isBackward ).to.be.false;
  178. } );
  179. it( 'should redo batch selectively undone by undo command #2', () => {
  180. undo.execute( batch1 );
  181. undo.execute( batch2 );
  182. redo.execute();
  183. redo.execute();
  184. // Should be back to original state:
  185. /*
  186. [root]
  187. - f
  188. - {b} (key: value)
  189. - a
  190. - r
  191. - o
  192. - o (key: value)
  193. */
  194. expect( getText( root ) ).to.equal( 'fbaroo' );
  195. expect( itemAt( root, 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  196. expect( itemAt( root, 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
  197. expect( editor.model.document.selection.getRanges().next().value.isEqual( r( 1, 2 ) ) ).to.be.true;
  198. expect( editor.model.document.selection.isBackward ).to.be.true;
  199. } );
  200. it( 'should transform redo batch by changes written in history that happened after undo but before redo #2', () => {
  201. // Now it is "fBaroO".
  202. // Undo moving "oo" to the end of string. Now it is "foOBar". Capitals mean set attribute.
  203. undo.execute();
  204. // Remove "ar".
  205. model.change( writer => {
  206. writer.remove( r( 4, 6 ) );
  207. } );
  208. // Undo setting attribute on "ob". Now it is "foob".
  209. undo.execute();
  210. // Append "xx" at the beginning. Now it is "xxfoob".
  211. model.change( writer => {
  212. writer.insertText( 'xx', p( 0 ) );
  213. } );
  214. // Redo setting attribute on "ob". Now it is "xxfoOB".
  215. redo.execute();
  216. expect( getText( root ) ).to.equal( 'xxfoob' );
  217. expect( itemAt( root, 4 ).getAttribute( 'key' ) ).to.equal( 'value' );
  218. expect( itemAt( root, 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
  219. expect( editor.model.document.selection.getFirstRange().isEqual( r( 4, 6 ) ) ).to.be.true;
  220. expect( editor.model.document.selection.isBackward ).to.be.true;
  221. // Redo moving "oo". Now it is "xxfBoO". Selection is expected to be on just moved "oO".
  222. redo.execute();
  223. expect( getText( root ) ).to.equal( 'xxfboo' );
  224. expect( itemAt( root, 3 ).getAttribute( 'key' ) ).to.equal( 'value' );
  225. expect( itemAt( root, 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
  226. expect( editor.model.document.selection.getFirstRange().isEqual( r( 4, 6 ) ) ).to.be.true;
  227. expect( editor.model.document.selection.isBackward ).to.be.false;
  228. } );
  229. } );
  230. } );
  231. } );