undoengine-integration.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 Element from '@ckeditor/ckeditor5-engine/src/model/element';
  9. import UndoEngine from '../src/undoengine';
  10. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. describe( 'UndoEngine integration', () => {
  12. let editor, doc, root;
  13. beforeEach( () => {
  14. return ModelTestEditor.create( { plugins: [ UndoEngine ] } )
  15. .then( newEditor => {
  16. editor = newEditor;
  17. doc = editor.document;
  18. doc.schema.registerItem( 'p', '$block' );
  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( 'adding and removing content', () => {
  35. it( 'add and undo', () => {
  36. input( '<p>fo[]o</p><p>bar</p>' );
  37. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  38. output( '<p>fozzz[]o</p><p>bar</p>' );
  39. editor.execute( 'undo' );
  40. output( '<p>fo[]o</p><p>bar</p>' );
  41. undoDisabled();
  42. } );
  43. it( 'multiple adding and undo', () => {
  44. input( '<p>fo[]o</p><p>bar</p>' );
  45. doc.batch()
  46. .insert( doc.selection.getFirstPosition(), 'zzz' )
  47. .insert( new Position( root, [ 1, 0 ] ), 'xxx' );
  48. output( '<p>fozzz[]o</p><p>xxxbar</p>' );
  49. setSelection( [ 1, 0 ], [ 1, 0 ] );
  50. doc.batch().insert( doc.selection.getFirstPosition(), 'yyy' );
  51. output( '<p>fozzzo</p><p>yyy[]xxxbar</p>' );
  52. editor.execute( 'undo' );
  53. output( '<p>fozzzo</p><p>[]xxxbar</p>' );
  54. editor.execute( 'undo' );
  55. output( '<p>fo[]o</p><p>bar</p>' );
  56. undoDisabled();
  57. } );
  58. it( 'multiple adding mixed with undo', () => {
  59. input( '<p>fo[]o</p><p>bar</p>' );
  60. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  61. output( '<p>fozzz[]o</p><p>bar</p>' );
  62. setSelection( [ 1, 0 ], [ 1, 0 ] );
  63. doc.batch().insert( doc.selection.getFirstPosition(), 'yyy' );
  64. output( '<p>fozzzo</p><p>yyy[]bar</p>' );
  65. editor.execute( 'undo' );
  66. output( '<p>fozzzo</p><p>[]bar</p>' );
  67. setSelection( [ 0, 0 ], [ 0, 0 ] );
  68. doc.batch().insert( doc.selection.getFirstPosition(), 'xxx' );
  69. output( '<p>xxx[]fozzzo</p><p>bar</p>' );
  70. editor.execute( 'undo' );
  71. output( '<p>[]fozzzo</p><p>bar</p>' );
  72. editor.execute( 'undo' );
  73. output( '<p>fo[]o</p><p>bar</p>' );
  74. undoDisabled();
  75. } );
  76. it( 'multiple remove and undo', () => {
  77. input( '<p>[]foo</p><p>bar</p>' );
  78. doc.batch().remove( Range.createFromPositionAndShift( doc.selection.getFirstPosition(), 2 ) );
  79. output( '<p>[]o</p><p>bar</p>' );
  80. setSelection( [ 1, 1 ], [ 1, 1 ] );
  81. doc.batch().remove( Range.createFromPositionAndShift( doc.selection.getFirstPosition(), 2 ) );
  82. output( '<p>o</p><p>b[]</p>' );
  83. editor.execute( 'undo' );
  84. // Here is an edge case that selection could be before or after `ar`.
  85. output( '<p>o</p><p>bar[]</p>' );
  86. editor.execute( 'undo' );
  87. // As above.
  88. output( '<p>fo[]o</p><p>bar</p>' );
  89. undoDisabled();
  90. } );
  91. it( 'add and remove different parts and undo', () => {
  92. input( '<p>fo[]o</p><p>bar</p>' );
  93. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  94. output( '<p>fozzz[]o</p><p>bar</p>' );
  95. setSelection( [ 1, 2 ], [ 1, 2 ] );
  96. doc.batch().remove( Range.createFromPositionAndShift( new Position( root, [ 1, 1 ] ), 1 ) );
  97. output( '<p>fozzzo</p><p>b[]r</p>' );
  98. editor.execute( 'undo' );
  99. output( '<p>fozzzo</p><p>ba[]r</p>' );
  100. editor.execute( 'undo' );
  101. output( '<p>fo[]o</p><p>bar</p>' );
  102. undoDisabled();
  103. } );
  104. it( 'add and remove same part and undo', () => {
  105. input( '<p>fo[]o</p><p>bar</p>' );
  106. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  107. output( '<p>fozzz[]o</p><p>bar</p>' );
  108. doc.batch().remove( Range.createFromPositionAndShift( new Position( root, [ 0, 2 ] ), 3 ) );
  109. output( '<p>fo[]o</p><p>bar</p>' );
  110. editor.execute( 'undo' );
  111. output( '<p>fozzz[]o</p><p>bar</p>' );
  112. editor.execute( 'undo' );
  113. output( '<p>fo[]o</p><p>bar</p>' );
  114. undoDisabled();
  115. } );
  116. } );
  117. describe( 'moving', () => {
  118. it( 'move same content twice then undo', () => {
  119. input( '<p>f[o]z</p><p>bar</p>' );
  120. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) );
  121. output( '<p>fz</p><p>[o]bar</p>' );
  122. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0, 2 ] ) );
  123. output( '<p>fz[o]</p><p>bar</p>' );
  124. editor.execute( 'undo' );
  125. output( '<p>fz</p><p>[o]bar</p>' );
  126. editor.execute( 'undo' );
  127. output( '<p>f[o]z</p><p>bar</p>' );
  128. undoDisabled();
  129. } );
  130. it( 'move content and new parent then undo', () => {
  131. input( '<p>f[o]z</p><p>bar</p>' );
  132. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) );
  133. output( '<p>fz</p><p>[o]bar</p>' );
  134. setSelection( [ 1 ], [ 2 ] );
  135. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0 ] ) );
  136. output( '[<p>obar</p>]<p>fz</p>' );
  137. editor.execute( 'undo' );
  138. output( '<p>fz</p>[<p>obar</p>]' );
  139. editor.execute( 'undo' );
  140. output( '<p>f[o]z</p><p>bar</p>' );
  141. undoDisabled();
  142. } );
  143. } );
  144. describe( 'attributes with other', () => {
  145. it( 'attributes then insert inside then undo', () => {
  146. input( '<p>fo[ob]ar</p>' );
  147. doc.batch().setAttribute( doc.selection.getFirstRange(), 'bold', true );
  148. output( '<p>fo[<$text bold="true">ob</$text>]ar</p>' );
  149. setSelection( [ 0, 3 ], [ 0, 3 ] );
  150. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  151. output( '<p>fo<$text bold="true">o</$text>zzz<$text bold="true">[]b</$text>ar</p>' );
  152. expect( doc.selection.getAttribute( 'bold' ) ).to.true;
  153. editor.execute( 'undo' );
  154. output( '<p>fo<$text bold="true">o[]b</$text>ar</p>' );
  155. expect( doc.selection.getAttribute( 'bold' ) ).to.true;
  156. editor.execute( 'undo' );
  157. output( '<p>fo[ob]ar</p>' );
  158. undoDisabled();
  159. } );
  160. } );
  161. describe( 'wrapping, unwrapping, merging, splitting', () => {
  162. it( 'wrap and undo', () => {
  163. doc.schema.allow( { name: '$text', inside: '$root' } );
  164. input( 'fo[zb]ar' );
  165. doc.batch().wrap( doc.selection.getFirstRange(), 'p' );
  166. output( 'fo<p>[zb]</p>ar' );
  167. editor.execute( 'undo' );
  168. output( 'fo[zb]ar' );
  169. undoDisabled();
  170. } );
  171. it( 'wrap, move and undo', () => {
  172. doc.schema.allow( { name: '$text', inside: '$root' } );
  173. input( 'fo[zb]ar' );
  174. doc.batch().wrap( doc.selection.getFirstRange(), 'p' );
  175. // Would be better if selection was inside P.
  176. output( 'fo<p>[zb]</p>ar' );
  177. setSelection( [ 2, 0 ], [ 2, 1 ] );
  178. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0 ] ) );
  179. output( '[z]fo<p>b</p>ar' );
  180. editor.execute( 'undo' );
  181. output( 'fo<p>[z]b</p>ar' );
  182. editor.execute( 'undo' );
  183. output( 'fo[zb]ar' );
  184. undoDisabled();
  185. } );
  186. it( 'unwrap and undo', () => {
  187. input( '<p>foo[]bar</p>' );
  188. doc.batch().unwrap( doc.selection.getFirstPosition().parent );
  189. output( 'foo[]bar' );
  190. editor.execute( 'undo' );
  191. output( '<p>foo[]bar</p>' );
  192. undoDisabled();
  193. } );
  194. it( 'merge and undo', () => {
  195. input( '<p>foo</p><p>[]bar</p>' );
  196. doc.batch().merge( new Position( root, [ 1 ] ) );
  197. // Because selection is stuck with <p> it ends up in graveyard. We have to manually move it to correct node.
  198. setSelection( [ 0, 3 ], [ 0, 3 ] );
  199. output( '<p>foo[]bar</p>' );
  200. editor.execute( 'undo' );
  201. output( '<p>foo</p><p>bar[]</p>' );
  202. undoDisabled();
  203. } );
  204. it( 'split and undo', () => {
  205. input( '<p>foo[]bar</p>' );
  206. doc.batch().split( doc.selection.getFirstPosition() );
  207. // Because selection is stuck with <p> it ends up in wrong node. We have to manually move it to correct node.
  208. setSelection( [ 1, 0 ], [ 1, 0 ] );
  209. output( '<p>foo</p><p>[]bar</p>' );
  210. editor.execute( 'undo' );
  211. output( '<p>foobar[]</p>' );
  212. undoDisabled();
  213. } );
  214. } );
  215. describe( 'other edge cases', () => {
  216. it( 'deleteContent between two nodes', () => {
  217. input( '<p>fo[o</p><p>b]ar</p>' );
  218. editor.data.deleteContent( doc.selection, doc.batch() );
  219. output( '<p>fo[]ar</p>' );
  220. editor.execute( 'undo' );
  221. output( '<p>fo[o</p><p>b]ar</p>' );
  222. } );
  223. // Related to ckeditor5-engine#891 and ckeditor5-list#51.
  224. it( 'change attribute of removed node then undo and redo', () => {
  225. const gy = doc.graveyard;
  226. const batch = doc.batch();
  227. const p = new Element( 'p' );
  228. root.appendChildren( p );
  229. batch.remove( p );
  230. batch.setAttribute( p, 'bold', true );
  231. editor.execute( 'undo' );
  232. editor.execute( 'redo' );
  233. expect( p.root ).to.equal( gy );
  234. expect( p.getAttribute( 'bold' ) ).to.be.true;
  235. } );
  236. // Related to ckeditor5-engine#891.
  237. it( 'change attribute of removed node then undo and redo', () => {
  238. const gy = doc.graveyard;
  239. const batch = doc.batch();
  240. const p1 = new Element( 'p' );
  241. const p2 = new Element( 'p' );
  242. const p3 = new Element( 'p' );
  243. root.appendChildren( [ p1, p2 ] );
  244. batch.remove( p1 ).remove( p2 ).insert( new Position( root, [ 0 ] ), p3 );
  245. editor.execute( 'undo' );
  246. editor.execute( 'redo' );
  247. expect( p1.root ).to.equal( gy );
  248. expect( p2.root ).to.equal( gy );
  249. expect( p3.root ).to.equal( root );
  250. } );
  251. } );
  252. } );