indentblockcommand.js 8.3 KB

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