8
0

undoengine-integration.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import ModelTestEditor from '/tests/ckeditor5/_utils/modeltesteditor.js';
  7. import Range from '/ckeditor5/engine/model/range.js';
  8. import Position from '/ckeditor5/engine/model/position.js';
  9. import UndoEngine from '/ckeditor5/undo/undoengine.js';
  10. import { setData, getData } from '/tests/engine/_utils/model.js';
  11. import deleteContents from '/ckeditor5/engine/model/composer/deletecontents.js';
  12. let editor, doc, root;
  13. beforeEach( () => {
  14. return ModelTestEditor.create( {
  15. features: [ UndoEngine ]
  16. } )
  17. .then( newEditor => {
  18. editor = newEditor;
  19. doc = editor.document;
  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<selection />o</p><p>bar</p>' );
  39. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  40. output( '<p>fozzz<selection />o</p><p>bar</p>' );
  41. editor.execute( 'undo' );
  42. output( '<p>fo<selection />o</p><p>bar</p>' );
  43. undoDisabled();
  44. } );
  45. it( 'multiple adding and undo', () => {
  46. input( '<p>fo<selection />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<selection />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<selection />xxxbar</p>' );
  54. editor.execute( 'undo' );
  55. output( '<p>fozzzo</p><p><selection />xxxbar</p>' );
  56. editor.execute( 'undo' );
  57. output( '<p>fo<selection />o</p><p>bar</p>' );
  58. undoDisabled();
  59. } );
  60. it( 'multiple adding mixed with undo', () => {
  61. input( '<p>fo<selection />o</p><p>bar</p>' );
  62. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  63. output( '<p>fozzz<selection />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<selection />bar</p>' );
  67. editor.execute( 'undo' );
  68. output( '<p>fozzzo</p><p><selection />bar</p>' );
  69. setSelection( [ 0, 0 ], [ 0, 0 ] );
  70. doc.batch().insert( doc.selection.getFirstPosition(), 'xxx' );
  71. output( '<p>xxx<selection />fozzzo</p><p>bar</p>' );
  72. editor.execute( 'undo' );
  73. output( '<p><selection />fozzzo</p><p>bar</p>' );
  74. editor.execute( 'undo' );
  75. output( '<p>fo<selection />o</p><p>bar</p>' );
  76. undoDisabled();
  77. } );
  78. it( 'multiple remove and undo', () => {
  79. input( '<p><selection />foo</p><p>bar</p>' );
  80. doc.batch().remove( Range.createFromPositionAndShift( doc.selection.getFirstPosition(), 2 ) );
  81. output( '<p><selection />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<selection /></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<selection />ar</p>' );
  88. editor.execute( 'undo' );
  89. // As above.
  90. output( '<p><selection />foo</p><p>bar</p>' );
  91. undoDisabled();
  92. } );
  93. it( 'add and remove different parts and undo', () => {
  94. input( '<p>fo<selection />o</p><p>bar</p>' );
  95. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  96. output( '<p>fozzz<selection />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<selection />r</p>' );
  100. editor.execute( 'undo' );
  101. output( '<p>fozzzo</p><p>ba<selection />r</p>' );
  102. editor.execute( 'undo' );
  103. output( '<p>fo<selection />o</p><p>bar</p>' );
  104. undoDisabled();
  105. } );
  106. it( 'add and remove same part and undo', () => {
  107. input( '<p>fo<selection />o</p><p>bar</p>' );
  108. doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' );
  109. output( '<p>fozzz<selection />o</p><p>bar</p>' );
  110. doc.batch().remove( Range.createFromPositionAndShift( new Position( root, [ 0, 2 ] ) , 3 ) );
  111. output( '<p>fo<selection />o</p><p>bar</p>' );
  112. editor.execute( 'undo' );
  113. output( '<p>fozzz<selection />o</p><p>bar</p>' );
  114. editor.execute( 'undo' );
  115. output( '<p>fo<selection />o</p><p>bar</p>' );
  116. undoDisabled();
  117. } );
  118. } );
  119. describe( 'moving', () => {
  120. it( 'move same content twice then undo', () => {
  121. input( '<p>f<selection>o</selection>z</p><p>bar</p>' );
  122. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) );
  123. output( '<p>fz</p><p><selection>o</selection>bar</p>' );
  124. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0, 2 ] ) );
  125. output( '<p>fz<selection>o</selection></p><p>bar</p>' );
  126. editor.execute( 'undo' );
  127. output( '<p>fz</p><p><selection>o</selection>bar</p>' );
  128. editor.execute( 'undo' );
  129. output( '<p>f<selection>o</selection>z</p><p>bar</p>' );
  130. undoDisabled();
  131. } );
  132. it( 'move content and new parent then undo', () => {
  133. input( '<p>f<selection>o</selection>z</p><p>bar</p>' );
  134. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) );
  135. output( '<p>fz</p><p><selection>o</selection>bar</p>' );
  136. setSelection( [ 1 ], [ 2 ] );
  137. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0 ] ) );
  138. output( '<selection><p>obar</p></selection><p>fz</p>' );
  139. editor.execute( 'undo' );
  140. output( '<p>fz</p><selection><p>obar</p></selection>' );
  141. editor.execute( 'undo' );
  142. output( '<p>f<selection>o</selection>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<selection>ob</selection>ar</p>' );
  149. doc.batch().setAttr( 'bold', true, doc.selection.getFirstRange() );
  150. output( '<p>fo<selection><$text bold=true>ob</$text></selection>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<selection bold=true /><$text bold=true>b</$text>ar</p>' );
  154. editor.execute( 'undo' );
  155. output( '<p>fo<$text bold=true>o<selection bold=true />b</$text>ar</p>' );
  156. editor.execute( 'undo' );
  157. output( '<p>fo<selection>ob</selection>ar</p>' );
  158. undoDisabled();
  159. } );
  160. } );
  161. describe( 'wrapping, unwrapping, merging, splitting', () => {
  162. it( 'wrap and undo', () => {
  163. input( 'fo<selection>zb</selection>ar' );
  164. doc.batch().wrap( doc.selection.getFirstRange(), 'p' );
  165. output( 'fo<selection><p>zb</p></selection>ar' );
  166. editor.execute( 'undo' );
  167. output( 'fo<selection>zb</selection>ar' );
  168. undoDisabled();
  169. } );
  170. it( 'wrap, move and undo', () => {
  171. input( 'fo<selection>zb</selection>ar' );
  172. doc.batch().wrap( doc.selection.getFirstRange(), 'p' );
  173. // Would be better if selection was inside P.
  174. output( 'fo<selection><p>zb</p></selection>ar' );
  175. setSelection( [ 2, 0 ], [ 2, 1 ] );
  176. doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 0 ] ) );
  177. output( '<selection>z</selection>fo<p>b</p>ar' );
  178. editor.execute( 'undo' );
  179. output( 'fo<p><selection>z</selection>b</p>ar' );
  180. editor.execute( 'undo' );
  181. output( 'fo<selection>zb</selection>ar' );
  182. undoDisabled();
  183. } );
  184. it( 'unwrap and undo', () => {
  185. input( '<p>foo<selection />bar</p>' );
  186. doc.batch().unwrap( doc.selection.getFirstPosition().parent );
  187. output( 'foo<selection />bar' );
  188. editor.execute( 'undo' );
  189. output( '<p>foo<selection />bar</p>' );
  190. undoDisabled();
  191. } );
  192. it( 'merge and undo', () => {
  193. input( '<p>foo</p><p><selection />bar</p>' );
  194. doc.batch().merge( new Position( root, [ 1 ] ) );
  195. // Because selection is stuck with <p> it ends up in graveyard. We have to manually move it to correct node.
  196. setSelection( [ 0, 3 ], [ 0, 3 ] );
  197. output( '<p>foo<selection />bar</p>' );
  198. editor.execute( 'undo' );
  199. output( '<p>foo</p><p><selection />bar</p>' );
  200. undoDisabled();
  201. } );
  202. it( 'split and undo', () => {
  203. input( '<p>foo<selection />bar</p>' );
  204. doc.batch().split( doc.selection.getFirstPosition() );
  205. // Because selection is stuck with <p> it ends up in wrong node. We have to manually move it to correct node.
  206. setSelection( [ 1, 0 ], [ 1, 0 ] );
  207. output( '<p>foo</p><p><selection />bar</p>' );
  208. editor.execute( 'undo' );
  209. output( '<p>foo<selection />bar</p>' );
  210. undoDisabled();
  211. } );
  212. } );
  213. describe( 'other edge cases', () => {
  214. it( 'deleteContents between two nodes', () => {
  215. input( '<p>fo<selection>o</p><p>b</selection>ar</p>' );
  216. deleteContents( doc.batch(), doc.selection, { merge: true } );
  217. output( '<p>fo<selection />ar</p>' );
  218. editor.execute( 'undo' );
  219. output( '<p>fo<selection>o</p><p>b</selection>ar</p>' );
  220. } );
  221. } );
  222. } );