removecolumncommand.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import RemoveColumnCommand from '../../src/commands/removecolumncommand';
  8. import TableSelection from '../../src/tableselection';
  9. import { defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
  10. import TableUtils from '../../src/tableutils';
  11. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  12. describe( 'RemoveColumnCommand', () => {
  13. let editor, model, command;
  14. beforeEach( () => {
  15. return ModelTestEditor
  16. .create( {
  17. plugins: [ TableUtils, TableSelection ]
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. model = editor.model;
  22. command = new RemoveColumnCommand( editor );
  23. defaultSchema( model.schema );
  24. defaultConversion( editor.conversion );
  25. } );
  26. } );
  27. afterEach( () => {
  28. return editor.destroy();
  29. } );
  30. describe( 'isEnabled', () => {
  31. it( 'should be true if selection is inside table cell', () => {
  32. setData( model, modelTable( [
  33. [ '00[]', '01' ],
  34. [ '10', '11' ]
  35. ] ) );
  36. expect( command.isEnabled ).to.be.true;
  37. } );
  38. it( 'should be true if selection contains multiple cells', () => {
  39. setData( model, modelTable( [
  40. [ '00', '01' ],
  41. [ '10', '11' ]
  42. ] ) );
  43. const tableSelection = editor.plugins.get( TableSelection );
  44. const modelRoot = model.document.getRoot();
  45. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  46. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  47. expect( command.isEnabled ).to.be.true;
  48. } );
  49. it( 'should be false if selection is inside table with one column only', () => {
  50. setData( model, modelTable( [
  51. [ '00' ],
  52. [ '10[]' ],
  53. [ '20[]' ]
  54. ] ) );
  55. expect( command.isEnabled ).to.be.false;
  56. } );
  57. it( 'should be false if selection is outside a table', () => {
  58. setData( model, '<paragraph>11[]</paragraph>' );
  59. expect( command.isEnabled ).to.be.false;
  60. } );
  61. } );
  62. describe( 'execute()', () => {
  63. it( 'should remove a given column', () => {
  64. setData( model, modelTable( [
  65. [ '00', '01', '02' ],
  66. [ '10', '[]11', '12' ],
  67. [ '20', '21', '22' ]
  68. ] ) );
  69. command.execute();
  70. assertEqualMarkup( getData( model ), modelTable( [
  71. [ '00', '02' ],
  72. [ '10', '[]12' ],
  73. [ '20', '22' ]
  74. ] ) );
  75. } );
  76. it( 'should remove a given column from a table start', () => {
  77. setData( model, modelTable( [
  78. [ '[]00', '01' ],
  79. [ '10', '11' ],
  80. [ '20', '21' ]
  81. ] ) );
  82. command.execute();
  83. assertEqualMarkup( getData( model ), modelTable( [
  84. [ '[]01' ],
  85. [ '11' ],
  86. [ '21' ]
  87. ] ) );
  88. } );
  89. describe( 'with multiple cells selected', () => {
  90. it( 'should properly remove the first column', () => {
  91. setData( model, modelTable( [
  92. [ '00', '01' ],
  93. [ '10', '11' ],
  94. [ '20', '21' ],
  95. [ '30', '31' ]
  96. ] ) );
  97. const tableSelection = editor.plugins.get( TableSelection );
  98. const modelRoot = model.document.getRoot();
  99. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) );
  100. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 0 ] ) );
  101. command.execute();
  102. assertEqualMarkup( getData( model ), modelTable( [
  103. [ '01' ],
  104. [ '[]11' ],
  105. [ '21' ],
  106. [ '31' ]
  107. ] ) );
  108. } );
  109. it( 'should properly remove a middle column', () => {
  110. setData( model, modelTable( [
  111. [ '00', '01', '02' ],
  112. [ '10', '11', '12' ],
  113. [ '20', '21', '22' ],
  114. [ '30', '31', '32' ]
  115. ] ) );
  116. const tableSelection = editor.plugins.get( TableSelection );
  117. const modelRoot = model.document.getRoot();
  118. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  119. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) );
  120. command.execute();
  121. assertEqualMarkup( getData( model ), modelTable( [
  122. [ '00', '02' ],
  123. [ '10', '[]12' ],
  124. [ '20', '22' ],
  125. [ '30', '32' ]
  126. ] ) );
  127. } );
  128. it( 'should properly remove the last column', () => {
  129. setData( model, modelTable( [
  130. [ '00', '01' ],
  131. [ '10', '11' ],
  132. [ '20', '21' ],
  133. [ '30', '31' ]
  134. ] ) );
  135. const tableSelection = editor.plugins.get( TableSelection );
  136. const modelRoot = model.document.getRoot();
  137. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  138. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) );
  139. command.execute();
  140. assertEqualMarkup( getData( model ), modelTable( [
  141. [ '00' ],
  142. [ '[]10' ],
  143. [ '20' ],
  144. [ '30' ]
  145. ] ) );
  146. } );
  147. } );
  148. it( 'should change heading columns if removing a heading column', () => {
  149. setData( model, modelTable( [
  150. [ '00', '01' ],
  151. [ '[]10', '11' ],
  152. [ '20', '21' ]
  153. ], { headingColumns: 2 } ) );
  154. command.execute();
  155. assertEqualMarkup( getData( model ), modelTable( [
  156. [ '01' ],
  157. [ '[]11' ],
  158. [ '21' ]
  159. ], { headingColumns: 1 } ) );
  160. } );
  161. it( 'should decrease colspan of table cells from previous column', () => {
  162. setData( model, modelTable( [
  163. [ { colspan: 4, contents: '00' }, '03' ],
  164. [ { colspan: 3, contents: '10' }, '13' ],
  165. [ { colspan: 2, contents: '20' }, '22[]', '23' ],
  166. [ '30', { colspan: 2, contents: '31' }, '33' ],
  167. [ '40', '41', '42', '43' ]
  168. ] ) );
  169. command.execute();
  170. assertEqualMarkup( getData( model ), modelTable( [
  171. [ { colspan: 3, contents: '00' }, '03' ],
  172. [ { colspan: 2, contents: '10' }, '13' ],
  173. [ { colspan: 2, contents: '20' }, '[]23' ],
  174. [ '30', '31', '33' ],
  175. [ '40', '41', '43' ]
  176. ] ) );
  177. } );
  178. it( 'should decrease colspan of cells that are on removed column', () => {
  179. setData( model, modelTable( [
  180. [ { colspan: 3, contents: '[]00' }, '03' ],
  181. [ { colspan: 2, contents: '10' }, '13' ],
  182. [ '20', '21', '22', '23' ]
  183. ] ) );
  184. command.execute();
  185. assertEqualMarkup( getData( model ), modelTable( [
  186. [ { colspan: 2, contents: '[]00' }, '03' ],
  187. [ '10', '13' ],
  188. [ '21', '22', '23' ]
  189. ] ) );
  190. } );
  191. it( 'should move focus to previous column of removed cell if in last column', () => {
  192. setData( model, modelTable( [
  193. [ '00', '01', '02' ],
  194. [ '10', '11', '12[]' ],
  195. [ '20', '21', '22' ]
  196. ] ) );
  197. command.execute();
  198. assertEqualMarkup( getData( model ), modelTable( [
  199. [ '00', '01' ],
  200. [ '10', '[]11' ],
  201. [ '20', '21' ]
  202. ] ) );
  203. } );
  204. } );
  205. } );