undoengine-integration.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from 'tests/core/_utils/modeltesteditor.js';
  6. import Range from 'ckeditor5/engine/model/range.js';
  7. import Position from 'ckeditor5/engine/model/position.js';
  8. import UndoEngine from 'ckeditor5/undo/undoengine.js';
  9. import { setData, getData } from 'ckeditor5/engine/dev-utils/model.js';
  10. describe( 'UndoEngine integration', () => {
  11. let editor, doc, root;
  12. beforeEach( () => {
  13. return ModelTestEditor.create( {
  14. plugins: [ UndoEngine ]
  15. } )
  16. .then( newEditor => {
  17. editor = newEditor;
  18. doc = editor.document;
  19. doc.schema.registerItem( 'p', '$block' );
  20. root = doc.getRoot();
  21. } );
  22. } );
  23. function setSelection( pathA, pathB ) {
  24. doc.selection.setRanges( [ new Range( new Position( root, pathA ), new Position( root, pathB ) ) ] );
  25. }
  26. function input( input ) {
  27. setData( doc, input );
  28. }
  29. function output( output ) {
  30. expect( getData( doc ) ).to.equal( output );
  31. }
  32. function undoDisabled() {
  33. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  34. }
  35. describe( 'UndoEngine integration', () => {
  36. describe( 'adding and removing content', () => {
  37. it( 'add and undo', () => {
  38. input( '<p>fo[]o</p><p>bar</p>' );
  39. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  40. output( '<p>fozzz[]o</p><p>bar</p>' );
  41. editor.execute( 'undo' );
  42. output( '<p>fo[]o</p><p>bar</p>' );
  43. undoDisabled();
  44. } );
  45. it( 'multiple adding and undo', () => {
  46. input( '<p>fo[]o</p><p>bar</p>' );
  47. doc.batch()
  48. .insert( doc.selection.getFirstPosition(), 'zzz' )
  49. .insert( new Position( root, [ 1, 0 ] ), 'xxx' );
  50. output( '<p>fozzz[]o</p><p>xxxbar</p>' );
  51. setSelection( [ 1, 0 ], [ 1, 0 ] );
  52. doc.batch().insert( doc.selection.getFirstPosition(), 'yyy' );
  53. output( '<p>fozzzo</p><p>yyy[]xxxbar</p>' );
  54. editor.execute( 'undo' );
  55. output( '<p>fozzzo</p><p>[]xxxbar</p>' );
  56. editor.execute( 'undo' );
  57. output( '<p>fo[]o</p><p>bar</p>' );
  58. undoDisabled();
  59. } );
  60. it( 'multiple adding mixed with undo', () => {
  61. input( '<p>fo[]o</p><p>bar</p>' );
  62. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  63. output( '<p>fozzz[]o</p><p>bar</p>' );
  64. setSelection( [ 1, 0 ], [ 1, 0 ] );
  65. doc.batch().insert( doc.selection.getFirstPosition(), 'yyy' );
  66. output( '<p>fozzzo</p><p>yyy[]bar</p>' );
  67. editor.execute( 'undo' );
  68. output( '<p>fozzzo</p><p>[]bar</p>' );
  69. setSelection( [ 0, 0 ], [ 0, 0 ] );
  70. doc.batch().insert( doc.selection.getFirstPosition(), 'xxx' );
  71. output( '<p>xxx[]fozzzo</p><p>bar</p>' );
  72. editor.execute( 'undo' );
  73. output( '<p>[]fozzzo</p><p>bar</p>' );
  74. editor.execute( 'undo' );
  75. output( '<p>fo[]o</p><p>bar</p>' );
  76. undoDisabled();
  77. } );
  78. it( 'multiple remove and undo', () => {
  79. input( '<p>[]foo</p><p>bar</p>' );
  80. doc.batch().remove( Range.createFromPositionAndShift( doc.selection.getFirstPosition(), 2 ) );
  81. output( '<p>[]o</p><p>bar</p>' );
  82. setSelection( [ 1, 1 ], [ 1, 1 ] );
  83. doc.batch().remove( Range.createFromPositionAndShift( doc.selection.getFirstPosition(), 2 ) );
  84. output( '<p>o</p><p>b[]</p>' );
  85. editor.execute( 'undo' );
  86. // Here is an edge case that selection could be before or after `ar`.
  87. output( '<p>o</p><p>b[]ar</p>' );
  88. editor.execute( 'undo' );
  89. // As above.
  90. output( '<p>[]foo</p><p>bar</p>' );
  91. undoDisabled();
  92. } );
  93. it( 'add and remove different parts and undo', () => {
  94. input( '<p>fo[]o</p><p>bar</p>' );
  95. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  96. output( '<p>fozzz[]o</p><p>bar</p>' );
  97. setSelection( [ 1, 2 ], [ 1, 2 ] );
  98. doc.batch().remove( Range.createFromPositionAndShift( new Position( root, [ 1, 1 ] ) , 1 ) );
  99. output( '<p>fozzzo</p><p>b[]r</p>' );
  100. editor.execute( 'undo' );
  101. output( '<p>fozzzo</p><p>ba[]r</p>' );
  102. editor.execute( 'undo' );
  103. output( '<p>fo[]o</p><p>bar</p>' );
  104. undoDisabled();
  105. } );
  106. it( 'add and remove same part and undo', () => {
  107. input( '<p>fo[]o</p><p>bar</p>' );
  108. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  109. output( '<p>fozzz[]o</p><p>bar</p>' );
  110. doc.batch().remove( Range.createFromPositionAndShift( new Position( root, [ 0, 2 ] ) , 3 ) );
  111. output( '<p>fo[]o</p><p>bar</p>' );
  112. editor.execute( 'undo' );
  113. output( '<p>fozzz[]o</p><p>bar</p>' );
  114. editor.execute( 'undo' );
  115. output( '<p>fo[]o</p><p>bar</p>' );
  116. undoDisabled();
  117. } );
  118. } );
  119. describe( 'moving', () => {
  120. it( 'move same content twice then undo', () => {
  121. input( '<p>f[o]z</p><p>bar</p>' );
  122. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) );
  123. output( '<p>fz</p><p>[o]bar</p>' );
  124. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0, 2 ] ) );
  125. output( '<p>fz[o]</p><p>bar</p>' );
  126. editor.execute( 'undo' );
  127. output( '<p>fz</p><p>[o]bar</p>' );
  128. editor.execute( 'undo' );
  129. output( '<p>f[o]z</p><p>bar</p>' );
  130. undoDisabled();
  131. } );
  132. it( 'move content and new parent then undo', () => {
  133. input( '<p>f[o]z</p><p>bar</p>' );
  134. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) );
  135. output( '<p>fz</p><p>[o]bar</p>' );
  136. setSelection( [ 1 ], [ 2 ] );
  137. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0 ] ) );
  138. output( '[<p>obar</p>]<p>fz</p>' );
  139. editor.execute( 'undo' );
  140. output( '<p>fz</p>[<p>obar</p>]' );
  141. editor.execute( 'undo' );
  142. output( '<p>f[o]z</p><p>bar</p>' );
  143. undoDisabled();
  144. } );
  145. } );
  146. describe( 'attributes with other', () => {
  147. it( 'attributes then insert inside then undo', () => {
  148. input( '<p>fo[ob]ar</p>' );
  149. doc.batch().setAttribute( doc.selection.getFirstRange(), 'bold', true );
  150. output( '<p>fo[<$text bold="true">ob</$text>]ar</p>' );
  151. setSelection( [ 0, 3 ], [ 0, 3 ] );
  152. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  153. output( '<p>fo<$text bold="true">o</$text>zzz<$text bold="true">[]b</$text>ar</p>' );
  154. expect( doc.selection.getAttribute( 'bold' ) ).to.true;
  155. editor.execute( 'undo' );
  156. output( '<p>fo<$text bold="true">o[]b</$text>ar</p>' );
  157. expect( doc.selection.getAttribute( 'bold' ) ).to.true;
  158. editor.execute( 'undo' );
  159. output( '<p>fo[ob]ar</p>' );
  160. undoDisabled();
  161. } );
  162. } );
  163. describe( 'wrapping, unwrapping, merging, splitting', () => {
  164. it( 'wrap and undo', () => {
  165. doc.schema.allow( { name: '$text', inside: '$root' } );
  166. input( 'fo[zb]ar' );
  167. doc.batch().wrap( doc.selection.getFirstRange(), 'p' );
  168. output( 'fo[<p>zb</p>]ar' );
  169. editor.execute( 'undo' );
  170. output( 'fo[zb]ar' );
  171. undoDisabled();
  172. } );
  173. it( 'wrap, move and undo', () => {
  174. doc.schema.allow( { name: '$text', inside: '$root' } );
  175. input( 'fo[zb]ar' );
  176. doc.batch().wrap( doc.selection.getFirstRange(), 'p' );
  177. // Would be better if selection was inside P.
  178. output( 'fo[<p>zb</p>]ar' );
  179. setSelection( [ 2, 0 ], [ 2, 1 ] );
  180. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0 ] ) );
  181. output( '[z]fo<p>b</p>ar' );
  182. editor.execute( 'undo' );
  183. output( 'fo<p>[z]b</p>ar' );
  184. editor.execute( 'undo' );
  185. output( 'fo[zb]ar' );
  186. undoDisabled();
  187. } );
  188. it( 'unwrap and undo', () => {
  189. input( '<p>foo[]bar</p>' );
  190. doc.batch().unwrap( doc.selection.getFirstPosition().parent );
  191. output( 'foo[]bar' );
  192. editor.execute( 'undo' );
  193. output( '<p>foo[]bar</p>' );
  194. undoDisabled();
  195. } );
  196. it( 'merge and undo', () => {
  197. input( '<p>foo</p><p>[]bar</p>' );
  198. doc.batch().merge( new Position( root, [ 1 ] ) );
  199. // Because selection is stuck with <p> it ends up in graveyard. We have to manually move it to correct node.
  200. setSelection( [ 0, 3 ], [ 0, 3 ] );
  201. output( '<p>foo[]bar</p>' );
  202. editor.execute( 'undo' );
  203. output( '<p>foo</p><p>[]bar</p>' );
  204. undoDisabled();
  205. } );
  206. it( 'split and undo', () => {
  207. input( '<p>foo[]bar</p>' );
  208. doc.batch().split( doc.selection.getFirstPosition() );
  209. // Because selection is stuck with <p> it ends up in wrong node. We have to manually move it to correct node.
  210. setSelection( [ 1, 0 ], [ 1, 0 ] );
  211. output( '<p>foo</p><p>[]bar</p>' );
  212. editor.execute( 'undo' );
  213. output( '<p>foo[]bar</p>' );
  214. undoDisabled();
  215. } );
  216. } );
  217. describe( 'other edge cases', () => {
  218. it( 'deleteContent between two nodes', () => {
  219. input( '<p>fo[o</p><p>b]ar</p>' );
  220. editor.data.deleteContent( doc.selection, doc.batch(), { merge: true } );
  221. output( '<p>fo[]ar</p>' );
  222. editor.execute( 'undo' );
  223. output( '<p>fo[o</p><p>b]ar</p>' );
  224. } );
  225. } );
  226. } );
  227. } );