undoengine-integration.js 9.1 KB

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