undoengine-integration.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. let editor, doc, root;
  11. beforeEach( () => {
  12. return ModelTestEditor.create( {
  13. plugins: [ UndoEngine ]
  14. } )
  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( 'UndoEngine integration', () => {
  35. describe( 'adding and removing content', () => {
  36. it( 'add and undo', () => {
  37. input( '<p>fo[]o</p><p>bar</p>' );
  38. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  39. output( '<p>fozzz[]o</p><p>bar</p>' );
  40. editor.execute( 'undo' );
  41. output( '<p>fo[]o</p><p>bar</p>' );
  42. undoDisabled();
  43. } );
  44. it( 'multiple adding and undo', () => {
  45. input( '<p>fo[]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[]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[]xxxbar</p>' );
  53. editor.execute( 'undo' );
  54. output( '<p>fozzzo</p><p>[]xxxbar</p>' );
  55. editor.execute( 'undo' );
  56. output( '<p>fo[]o</p><p>bar</p>' );
  57. undoDisabled();
  58. } );
  59. it( 'multiple adding mixed with undo', () => {
  60. input( '<p>fo[]o</p><p>bar</p>' );
  61. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  62. output( '<p>fozzz[]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[]bar</p>' );
  66. editor.execute( 'undo' );
  67. output( '<p>fozzzo</p><p>[]bar</p>' );
  68. setSelection( [ 0, 0 ], [ 0, 0 ] );
  69. doc.batch().insert( doc.selection.getFirstPosition(), 'xxx' );
  70. output( '<p>xxx[]fozzzo</p><p>bar</p>' );
  71. editor.execute( 'undo' );
  72. output( '<p>[]fozzzo</p><p>bar</p>' );
  73. editor.execute( 'undo' );
  74. output( '<p>fo[]o</p><p>bar</p>' );
  75. undoDisabled();
  76. } );
  77. it( 'multiple remove and undo', () => {
  78. input( '<p>[]foo</p><p>bar</p>' );
  79. doc.batch().remove( Range.createFromPositionAndShift( doc.selection.getFirstPosition(), 2 ) );
  80. output( '<p>[]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[]</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[]ar</p>' );
  87. editor.execute( 'undo' );
  88. // As above.
  89. output( '<p>[]foo</p><p>bar</p>' );
  90. undoDisabled();
  91. } );
  92. it( 'add and remove different parts and undo', () => {
  93. input( '<p>fo[]o</p><p>bar</p>' );
  94. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  95. output( '<p>fozzz[]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[]r</p>' );
  99. editor.execute( 'undo' );
  100. output( '<p>fozzzo</p><p>ba[]r</p>' );
  101. editor.execute( 'undo' );
  102. output( '<p>fo[]o</p><p>bar</p>' );
  103. undoDisabled();
  104. } );
  105. it( 'add and remove same part and undo', () => {
  106. input( '<p>fo[]o</p><p>bar</p>' );
  107. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  108. output( '<p>fozzz[]o</p><p>bar</p>' );
  109. doc.batch().remove( Range.createFromPositionAndShift( new Position( root, [ 0, 2 ] ) , 3 ) );
  110. output( '<p>fo[]o</p><p>bar</p>' );
  111. editor.execute( 'undo' );
  112. output( '<p>fozzz[]o</p><p>bar</p>' );
  113. editor.execute( 'undo' );
  114. output( '<p>fo[]o</p><p>bar</p>' );
  115. undoDisabled();
  116. } );
  117. } );
  118. describe( 'moving', () => {
  119. it( 'move same content twice then undo', () => {
  120. input( '<p>f[o]z</p><p>bar</p>' );
  121. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) );
  122. output( '<p>fz</p><p>[o]bar</p>' );
  123. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0, 2 ] ) );
  124. output( '<p>fz[o]</p><p>bar</p>' );
  125. editor.execute( 'undo' );
  126. output( '<p>fz</p><p>[o]bar</p>' );
  127. editor.execute( 'undo' );
  128. output( '<p>f[o]z</p><p>bar</p>' );
  129. undoDisabled();
  130. } );
  131. it( 'move content and new parent then undo', () => {
  132. input( '<p>f[o]z</p><p>bar</p>' );
  133. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) );
  134. output( '<p>fz</p><p>[o]bar</p>' );
  135. setSelection( [ 1 ], [ 2 ] );
  136. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0 ] ) );
  137. output( '[<p>obar</p>]<p>fz</p>' );
  138. editor.execute( 'undo' );
  139. output( '<p>fz</p>[<p>obar</p>]' );
  140. editor.execute( 'undo' );
  141. output( '<p>f[o]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[ob]ar</p>' );
  148. doc.batch().setAttribute( doc.selection.getFirstRange(), 'bold', true );
  149. output( '<p>fo[<$text bold="true">ob</$text>]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<$text bold="true">[]b</$text>ar</p>' );
  153. expect( doc.selection.getAttribute( 'bold' ) ).to.true;
  154. editor.execute( 'undo' );
  155. output( '<p>fo<$text bold="true">o[]b</$text>ar</p>' );
  156. expect( doc.selection.getAttribute( 'bold' ) ).to.true;
  157. editor.execute( 'undo' );
  158. output( '<p>fo[ob]ar</p>' );
  159. undoDisabled();
  160. } );
  161. } );
  162. describe( 'wrapping, unwrapping, merging, splitting', () => {
  163. it( 'wrap and undo', () => {
  164. doc.schema.allow( { name: '$text', inside: '$root' } );
  165. input( 'fo[zb]ar' );
  166. doc.batch().wrap( doc.selection.getFirstRange(), 'p' );
  167. output( 'fo[<p>zb</p>]ar' );
  168. editor.execute( 'undo' );
  169. output( 'fo[zb]ar' );
  170. undoDisabled();
  171. } );
  172. it( 'wrap, move and undo', () => {
  173. doc.schema.allow( { name: '$text', inside: '$root' } );
  174. input( 'fo[zb]ar' );
  175. doc.batch().wrap( doc.selection.getFirstRange(), 'p' );
  176. // Would be better if selection was inside P.
  177. output( 'fo[<p>zb</p>]ar' );
  178. setSelection( [ 2, 0 ], [ 2, 1 ] );
  179. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0 ] ) );
  180. output( '[z]fo<p>b</p>ar' );
  181. editor.execute( 'undo' );
  182. output( 'fo<p>[z]b</p>ar' );
  183. editor.execute( 'undo' );
  184. output( 'fo[zb]ar' );
  185. undoDisabled();
  186. } );
  187. it( 'unwrap and undo', () => {
  188. input( '<p>foo[]bar</p>' );
  189. doc.batch().unwrap( doc.selection.getFirstPosition().parent );
  190. output( 'foo[]bar' );
  191. editor.execute( 'undo' );
  192. output( '<p>foo[]bar</p>' );
  193. undoDisabled();
  194. } );
  195. it( 'merge and undo', () => {
  196. input( '<p>foo</p><p>[]bar</p>' );
  197. doc.batch().merge( new Position( root, [ 1 ] ) );
  198. // Because selection is stuck with <p> it ends up in graveyard. We have to manually move it to correct node.
  199. setSelection( [ 0, 3 ], [ 0, 3 ] );
  200. output( '<p>foo[]bar</p>' );
  201. editor.execute( 'undo' );
  202. output( '<p>foo</p><p>[]bar</p>' );
  203. undoDisabled();
  204. } );
  205. it( 'split and undo', () => {
  206. input( '<p>foo[]bar</p>' );
  207. doc.batch().split( doc.selection.getFirstPosition() );
  208. // Because selection is stuck with <p> it ends up in wrong node. We have to manually move it to correct node.
  209. setSelection( [ 1, 0 ], [ 1, 0 ] );
  210. output( '<p>foo</p><p>[]bar</p>' );
  211. editor.execute( 'undo' );
  212. output( '<p>foo[]bar</p>' );
  213. undoDisabled();
  214. } );
  215. } );
  216. describe( 'other edge cases', () => {
  217. it( 'deleteContent between two nodes', () => {
  218. input( '<p>fo[o</p><p>b]ar</p>' );
  219. editor.data.deleteContent( doc.selection, doc.batch(), { merge: true } );
  220. output( '<p>fo[]ar</p>' );
  221. editor.execute( 'undo' );
  222. output( '<p>fo[o</p><p>b]ar</p>' );
  223. } );
  224. } );
  225. } );