8
0

indentcommand.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
  6. import Model from '@ckeditor/ckeditor5-engine/src/model/model';
  7. import IndentCommand from '../src/indentcommand';
  8. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. describe( 'IndentCommand', () => {
  10. let editor, model, doc, root;
  11. beforeEach( () => {
  12. editor = new Editor();
  13. editor.model = new Model();
  14. model = editor.model;
  15. doc = model.document;
  16. root = doc.createRoot();
  17. model.schema.register( 'listItem', {
  18. inheritAllFrom: '$block',
  19. allowAttributes: [ 'listType', 'listIndent' ]
  20. } );
  21. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  22. setData(
  23. model,
  24. '<listItem listIndent="0" listType="bulleted">a</listItem>' +
  25. '<listItem listIndent="0" listType="bulleted">b</listItem>' +
  26. '<listItem listIndent="1" listType="bulleted">c</listItem>' +
  27. '<listItem listIndent="2" listType="bulleted">d</listItem>' +
  28. '<listItem listIndent="2" listType="bulleted">e</listItem>' +
  29. '<listItem listIndent="1" listType="bulleted">f</listItem>' +
  30. '<listItem listIndent="0" listType="bulleted">g</listItem>'
  31. );
  32. } );
  33. describe( 'IndentCommand - forward (indent)', () => {
  34. let command;
  35. beforeEach( () => {
  36. command = new IndentCommand( editor, 'forward' );
  37. } );
  38. afterEach( () => {
  39. command.destroy();
  40. } );
  41. describe( 'isEnabled', () => {
  42. it( 'should be true if selection starts in list item', () => {
  43. model.change( writer => {
  44. writer.setSelection( root.getChild( 5 ), 0 );
  45. } );
  46. expect( command.isEnabled ).to.be.true;
  47. } );
  48. it( 'should be false if selection starts in first list item', () => {
  49. model.change( writer => {
  50. writer.setSelection( root.getChild( 0 ), 0 );
  51. } );
  52. expect( command.isEnabled ).to.be.false;
  53. } );
  54. // Reported in PR #53.
  55. it( 'should be false if selection starts in first list item #2', () => {
  56. setData(
  57. model,
  58. '<listItem listIndent="0" listType="bulleted">a</listItem>' +
  59. '<listItem listIndent="1" listType="bulleted">b</listItem>' +
  60. '<listItem listIndent="0" listType="bulleted">c</listItem>' +
  61. '<listItem listIndent="1" listType="bulleted">[]d</listItem>' +
  62. '<listItem listIndent="2" listType="bulleted">e</listItem>'
  63. );
  64. expect( command.isEnabled ).to.be.false;
  65. } );
  66. // Reported in PR #53.
  67. it( 'should be false if selection starts in first list item #3', () => {
  68. setData(
  69. model,
  70. '<listItem listIndent="0" listType="bulleted">a</listItem>' +
  71. '<listItem listIndent="1" listType="bulleted">b</listItem>' +
  72. '<listItem listIndent="0" listType="numbered">c</listItem>' +
  73. '<listItem listIndent="1" listType="bulleted">d</listItem>' +
  74. '<listItem listIndent="0" listType="bulleted">[]e</listItem>'
  75. );
  76. expect( command.isEnabled ).to.be.false;
  77. } );
  78. it( 'should be false if selection starts in first list item of top level list with different type than previous list', () => {
  79. setData(
  80. model,
  81. '<listItem listIndent="0" listType="bulleted">a</listItem>' +
  82. '<listItem listIndent="0" listType="numbered">[]b</listItem>'
  83. );
  84. expect( command.isEnabled ).to.be.false;
  85. } );
  86. it( 'should be false if selection starts in a list item that has bigger indent than it\'s previous sibling', () => {
  87. model.change( writer => {
  88. writer.setSelection( root.getChild( 2 ), 0 );
  89. } );
  90. expect( command.isEnabled ).to.be.false;
  91. } );
  92. // Edge case but may happen that some other blocks will also use the indent attribute
  93. // and before we fixed it the command was enabled in such a case.
  94. it( 'should be false if selection starts in a paragraph with indent attribute', () => {
  95. model.schema.extend( 'paragraph', { allowAttributes: 'listIndent' } );
  96. setData( model, '<listItem listIndent="0">a</listItem><paragraph listIndent="0">b[]</paragraph>' );
  97. expect( command.isEnabled ).to.be.false;
  98. } );
  99. } );
  100. describe( 'execute()', () => {
  101. it( 'should use parent batch', () => {
  102. model.change( writer => {
  103. writer.setSelection( root.getChild( 5 ), 0 );
  104. } );
  105. model.change( writer => {
  106. expect( writer.batch.operations.length ).to.equal( 0 );
  107. command.execute();
  108. expect( writer.batch.operations.length ).to.be.above( 0 );
  109. } );
  110. } );
  111. it( 'should increment indent attribute by 1', () => {
  112. model.change( writer => {
  113. writer.setSelection( root.getChild( 5 ), 0 );
  114. } );
  115. command.execute();
  116. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  117. '<listItem listIndent="0" listType="bulleted">a</listItem>' +
  118. '<listItem listIndent="0" listType="bulleted">b</listItem>' +
  119. '<listItem listIndent="1" listType="bulleted">c</listItem>' +
  120. '<listItem listIndent="2" listType="bulleted">d</listItem>' +
  121. '<listItem listIndent="2" listType="bulleted">e</listItem>' +
  122. '<listItem listIndent="2" listType="bulleted">f</listItem>' +
  123. '<listItem listIndent="0" listType="bulleted">g</listItem>'
  124. );
  125. } );
  126. it( 'should increment indent of all sub-items of indented item', () => {
  127. model.change( writer => {
  128. writer.setSelection( root.getChild( 1 ), 0 );
  129. } );
  130. command.execute();
  131. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  132. '<listItem listIndent="0" listType="bulleted">a</listItem>' +
  133. '<listItem listIndent="1" listType="bulleted">b</listItem>' +
  134. '<listItem listIndent="2" listType="bulleted">c</listItem>' +
  135. '<listItem listIndent="3" listType="bulleted">d</listItem>' +
  136. '<listItem listIndent="3" listType="bulleted">e</listItem>' +
  137. '<listItem listIndent="2" listType="bulleted">f</listItem>' +
  138. '<listItem listIndent="0" listType="bulleted">g</listItem>'
  139. );
  140. } );
  141. it( 'should increment indent of all selected item when multiple items are selected', () => {
  142. model.change( writer => {
  143. writer.setSelection( writer.createRange(
  144. writer.createPositionFromPath( root.getChild( 1 ), [ 0 ] ),
  145. writer.createPositionFromPath( root.getChild( 3 ), [ 1 ] )
  146. ) );
  147. } );
  148. command.execute();
  149. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  150. '<listItem listIndent="0" listType="bulleted">a</listItem>' +
  151. '<listItem listIndent="1" listType="bulleted">b</listItem>' +
  152. '<listItem listIndent="2" listType="bulleted">c</listItem>' +
  153. '<listItem listIndent="3" listType="bulleted">d</listItem>' +
  154. '<listItem listIndent="2" listType="bulleted">e</listItem>' +
  155. '<listItem listIndent="1" listType="bulleted">f</listItem>' +
  156. '<listItem listIndent="0" listType="bulleted">g</listItem>'
  157. );
  158. } );
  159. it( 'should fire "_executeCleanup" event after finish all operations with all changed items', done => {
  160. model.change( writer => {
  161. writer.setSelection( root.getChild( 1 ), 0 );
  162. } );
  163. command.on( '_executeCleanup', ( evt, data ) => {
  164. expect( data ).to.deep.equal( [
  165. root.getChild( 1 ),
  166. root.getChild( 2 ),
  167. root.getChild( 3 ),
  168. root.getChild( 4 ),
  169. root.getChild( 5 )
  170. ] );
  171. done();
  172. } );
  173. command.execute();
  174. } );
  175. } );
  176. } );
  177. describe( 'IndentCommand - backward (outdent)', () => {
  178. let command;
  179. beforeEach( () => {
  180. command = new IndentCommand( editor, 'backward' );
  181. } );
  182. afterEach( () => {
  183. command.destroy();
  184. } );
  185. describe( 'isEnabled', () => {
  186. it( 'should be true if selection starts in list item', () => {
  187. model.change( writer => {
  188. writer.setSelection( root.getChild( 5 ), 0 );
  189. } );
  190. expect( command.isEnabled ).to.be.true;
  191. } );
  192. it( 'should be true if selection starts in first list item', () => {
  193. // This is in contrary to forward indent command.
  194. model.change( writer => {
  195. writer.setSelection( root.getChild( 0 ), 0 );
  196. } );
  197. expect( command.isEnabled ).to.be.true;
  198. } );
  199. it( 'should be true if selection starts in a list item that has bigger indent than it\'s previous sibling', () => {
  200. // This is in contrary to forward indent command.
  201. model.change( writer => {
  202. writer.setSelection( root.getChild( 2 ), 0 );
  203. } );
  204. expect( command.isEnabled ).to.be.true;
  205. } );
  206. } );
  207. describe( 'execute()', () => {
  208. it( 'should decrement indent attribute by 1 (if it is bigger than 0)', () => {
  209. model.change( writer => {
  210. writer.setSelection( root.getChild( 5 ), 0 );
  211. } );
  212. command.execute();
  213. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  214. '<listItem listIndent="0" listType="bulleted">a</listItem>' +
  215. '<listItem listIndent="0" listType="bulleted">b</listItem>' +
  216. '<listItem listIndent="1" listType="bulleted">c</listItem>' +
  217. '<listItem listIndent="2" listType="bulleted">d</listItem>' +
  218. '<listItem listIndent="2" listType="bulleted">e</listItem>' +
  219. '<listItem listIndent="0" listType="bulleted">f</listItem>' +
  220. '<listItem listIndent="0" listType="bulleted">g</listItem>'
  221. );
  222. } );
  223. it( 'should rename listItem to paragraph (if indent is equal to 0)', () => {
  224. model.change( writer => {
  225. writer.setSelection( root.getChild( 0 ), 0 );
  226. } );
  227. command.execute();
  228. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  229. '<paragraph listIndent="0" listType="bulleted">a</paragraph>' +
  230. '<listItem listIndent="0" listType="bulleted">b</listItem>' +
  231. '<listItem listIndent="1" listType="bulleted">c</listItem>' +
  232. '<listItem listIndent="2" listType="bulleted">d</listItem>' +
  233. '<listItem listIndent="2" listType="bulleted">e</listItem>' +
  234. '<listItem listIndent="1" listType="bulleted">f</listItem>' +
  235. '<listItem listIndent="0" listType="bulleted">g</listItem>'
  236. );
  237. } );
  238. it( 'should decrement indent of all sub-items of outdented item', () => {
  239. model.change( writer => {
  240. writer.setSelection( root.getChild( 1 ), 0 );
  241. } );
  242. command.execute();
  243. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  244. '<listItem listIndent="0" listType="bulleted">a</listItem>' +
  245. '<paragraph listIndent="0" listType="bulleted">b</paragraph>' +
  246. '<listItem listIndent="0" listType="bulleted">c</listItem>' +
  247. '<listItem listIndent="1" listType="bulleted">d</listItem>' +
  248. '<listItem listIndent="1" listType="bulleted">e</listItem>' +
  249. '<listItem listIndent="0" listType="bulleted">f</listItem>' +
  250. '<listItem listIndent="0" listType="bulleted">g</listItem>'
  251. );
  252. } );
  253. it( 'should outdent all selected item when multiple items are selected', () => {
  254. model.change( writer => {
  255. writer.setSelection( writer.createRange(
  256. writer.createPositionFromPath( root.getChild( 1 ), [ 0 ] ),
  257. writer.createPositionFromPath( root.getChild( 3 ), [ 1 ] )
  258. ) );
  259. } );
  260. command.execute();
  261. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  262. '<listItem listIndent="0" listType="bulleted">a</listItem>' +
  263. '<paragraph listIndent="0" listType="bulleted">b</paragraph>' +
  264. '<listItem listIndent="0" listType="bulleted">c</listItem>' +
  265. '<listItem listIndent="1" listType="bulleted">d</listItem>' +
  266. '<listItem listIndent="2" listType="bulleted">e</listItem>' +
  267. '<listItem listIndent="1" listType="bulleted">f</listItem>' +
  268. '<listItem listIndent="0" listType="bulleted">g</listItem>'
  269. );
  270. } );
  271. } );
  272. } );
  273. } );