indentblockcommand.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import IndentBlockCommand from '../src/indentblockcommand';
  8. describe( 'IndentBlockCommand', () => {
  9. let editor, command, model;
  10. beforeEach( () => {
  11. return ModelTestEditor
  12. .create()
  13. .then( newEditor => {
  14. editor = newEditor;
  15. model = editor.model;
  16. model.schema.register( 'paragraph', {
  17. inheritAllFrom: '$block',
  18. allowAttributes: [ 'indent' ]
  19. } );
  20. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  21. } );
  22. } );
  23. afterEach( () => {
  24. command.destroy();
  25. return editor.destroy();
  26. } );
  27. describe( 'indent', () => {
  28. describe( 'using classes', () => {
  29. beforeEach( () => {
  30. command = new IndentBlockCommand( editor, {
  31. classes: [
  32. 'indent-1',
  33. 'indent-2',
  34. 'indent-3',
  35. 'indent-4'
  36. ],
  37. direction: 'forward'
  38. } );
  39. } );
  40. describe( 'isEnabled', () => {
  41. it( 'should be false in block that does not support indent', () => {
  42. setData( model, '<block>f[]oo</block>' );
  43. expect( command.isEnabled ).to.be.false;
  44. } );
  45. it( 'should be true in non-indented block', () => {
  46. setData( model, '<paragraph>f[]oo</paragraph>' );
  47. expect( command.isEnabled ).to.be.true;
  48. } );
  49. it( 'should be true in indented block and there are still indentation classes', () => {
  50. setData( model, '<paragraph indent="indent-2">f[]oo</paragraph>' );
  51. expect( command.isEnabled ).to.be.true;
  52. } );
  53. it( 'should be true in indented block in last indentation class', () => {
  54. setData( model, '<paragraph indent="indent-4">f[]oo</paragraph>' );
  55. expect( command.isEnabled ).to.be.false;
  56. } );
  57. } );
  58. describe( 'execute()', () => {
  59. it( 'should set first indent class for non-indented block', () => {
  60. setData( model, '<paragraph>f[]oo</paragraph>' );
  61. command.execute();
  62. expect( getData( model ) ).to.equal( '<paragraph indent="indent-1">f[]oo</paragraph>' );
  63. } );
  64. it( 'should set next indent class for indented block', () => {
  65. setData( model, '<paragraph indent="indent-2">f[]oo</paragraph>' );
  66. command.execute();
  67. expect( getData( model ) ).to.equal( '<paragraph indent="indent-3">f[]oo</paragraph>' );
  68. } );
  69. } );
  70. } );
  71. describe( 'using offset', () => {
  72. beforeEach( () => {
  73. command = new IndentBlockCommand( editor, {
  74. offset: 50,
  75. unit: 'px',
  76. direction: 'forward'
  77. } );
  78. } );
  79. describe( 'isEnabled', () => {
  80. it( 'should be false in block that does not support indent', () => {
  81. setData( model, '<block>f[]oo</block>' );
  82. expect( command.isEnabled ).to.be.false;
  83. } );
  84. it( 'should be true in non-indented block', () => {
  85. setData( model, '<paragraph>f[]oo</paragraph>' );
  86. expect( command.isEnabled ).to.be.true;
  87. } );
  88. it( 'should be true in indented block', () => {
  89. setData( model, '<paragraph indent="50px">f[]oo</paragraph>' );
  90. expect( command.isEnabled ).to.be.true;
  91. } );
  92. it( 'should be true in indented block with different unit', () => {
  93. setData( model, '<paragraph indent="2em">f[]oo</paragraph>' );
  94. expect( command.isEnabled ).to.be.true;
  95. } );
  96. } );
  97. describe( 'execute()', () => {
  98. it( 'should set first offset for non-indented block', () => {
  99. setData( model, '<paragraph>f[]oo</paragraph>' );
  100. command.execute();
  101. expect( getData( model ) ).to.equal( '<paragraph indent="50px">f[]oo</paragraph>' );
  102. } );
  103. it( 'should calculate next offset for indented block', () => {
  104. setData( model, '<paragraph indent="100px">f[]oo</paragraph>' );
  105. command.execute();
  106. expect( getData( model ) ).to.equal( '<paragraph indent="150px">f[]oo</paragraph>' );
  107. } );
  108. it( 'should calculate next offset for indented block even if current indent is not tied to offset', () => {
  109. setData( model, '<paragraph indent="27px">f[]oo</paragraph>' );
  110. command.execute();
  111. expect( getData( model ) ).to.equal( '<paragraph indent="77px">f[]oo</paragraph>' );
  112. } );
  113. it( 'should set first offset if current indent has different unit', () => {
  114. setData( model, '<paragraph indent="3mm">f[]oo</paragraph>' );
  115. command.execute();
  116. expect( getData( model ) ).to.equal( '<paragraph indent="50px">f[]oo</paragraph>' );
  117. } );
  118. } );
  119. } );
  120. } );
  121. describe( 'outdent', () => {
  122. describe( 'using classes', () => {
  123. beforeEach( () => {
  124. command = new IndentBlockCommand( editor, {
  125. classes: [
  126. 'indent-1',
  127. 'indent-2',
  128. 'indent-3',
  129. 'indent-4'
  130. ],
  131. direction: 'backward'
  132. } );
  133. } );
  134. describe( 'isEnabled', () => {
  135. it( 'should be false in block that does not support indent', () => {
  136. setData( model, '<block>f[]oo</block>' );
  137. expect( command.isEnabled ).to.be.false;
  138. } );
  139. it( 'should be false in non-indented block', () => {
  140. setData( model, '<paragraph>f[]oo</paragraph>' );
  141. expect( command.isEnabled ).to.be.false;
  142. } );
  143. it( 'should be true in indented block and there are still indentation classes', () => {
  144. setData( model, '<paragraph indent="indent-2">f[]oo</paragraph>' );
  145. expect( command.isEnabled ).to.be.true;
  146. } );
  147. it( 'should be true in indented block in last indentation class', () => {
  148. setData( model, '<paragraph indent="indent-4">f[]oo</paragraph>' );
  149. expect( command.isEnabled ).to.be.true;
  150. } );
  151. } );
  152. describe( 'execute()', () => {
  153. it( 'should set previous indent class for indented block', () => {
  154. setData( model, '<paragraph indent="indent-2">f[]oo</paragraph>' );
  155. command.execute();
  156. expect( getData( model ) ).to.equal( '<paragraph indent="indent-1">f[]oo</paragraph>' );
  157. } );
  158. } );
  159. } );
  160. describe( 'using offset', () => {
  161. beforeEach( () => {
  162. command = new IndentBlockCommand( editor, {
  163. offset: 50,
  164. unit: 'px',
  165. direction: 'backward'
  166. } );
  167. } );
  168. describe( 'isEnabled', () => {
  169. it( 'should be false in block that does not support indent', () => {
  170. setData( model, '<block>f[]oo</block>' );
  171. expect( command.isEnabled ).to.be.false;
  172. } );
  173. it( 'should be false in non-indented block', () => {
  174. setData( model, '<paragraph>f[]oo</paragraph>' );
  175. expect( command.isEnabled ).to.be.false;
  176. } );
  177. it( 'should be true in indented block', () => {
  178. setData( model, '<paragraph indent="50px">f[]oo</paragraph>' );
  179. expect( command.isEnabled ).to.be.true;
  180. } );
  181. it( 'should be true in indented block with different unit', () => {
  182. setData( model, '<paragraph indent="2em">f[]oo</paragraph>' );
  183. expect( command.isEnabled ).to.be.true;
  184. } );
  185. } );
  186. describe( 'execute()', () => {
  187. it( 'should set remove offset if indent is on first offset', () => {
  188. setData( model, '<paragraph indent="50px">f[]oo</paragraph>' );
  189. command.execute();
  190. expect( getData( model ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
  191. } );
  192. it( 'should calculate next offset for indented block', () => {
  193. setData( model, '<paragraph indent="100px">f[]oo</paragraph>' );
  194. command.execute();
  195. expect( getData( model ) ).to.equal( '<paragraph indent="50px">f[]oo</paragraph>' );
  196. } );
  197. it( 'should calculate next offset for indented block even if current indent is not tied to offset', () => {
  198. setData( model, '<paragraph indent="92px">f[]oo</paragraph>' );
  199. command.execute();
  200. expect( getData( model ) ).to.equal( '<paragraph indent="42px">f[]oo</paragraph>' );
  201. } );
  202. it( 'should remove offset if current indent has different unit', () => {
  203. setData( model, '<paragraph indent="3mm">f[]oo</paragraph>' );
  204. command.execute();
  205. expect( getData( model ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
  206. } );
  207. } );
  208. } );
  209. } );
  210. } );