insertcolumncommand.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 HorizontalLineEditing from '@ckeditor/ckeditor5-horizontal-line/src/horizontallineediting';
  7. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import InsertColumnCommand from '../../src/commands/insertcolumncommand';
  9. import TableSelection from '../../src/tableselection';
  10. import { assertSelectedCells, defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
  11. import TableUtils from '../../src/tableutils';
  12. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  13. describe( 'InsertColumnCommand', () => {
  14. let editor, model, command;
  15. beforeEach( () => {
  16. return ModelTestEditor
  17. .create( {
  18. plugins: [ TableUtils, TableSelection, HorizontalLineEditing ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. model = editor.model;
  23. defaultSchema( model.schema );
  24. defaultConversion( editor.conversion );
  25. } );
  26. } );
  27. afterEach( () => {
  28. return editor.destroy();
  29. } );
  30. describe( 'order=right', () => {
  31. beforeEach( () => {
  32. command = new InsertColumnCommand( editor );
  33. } );
  34. describe( 'isEnabled', () => {
  35. it( 'should be false if wrong node', () => {
  36. setData( model, '<paragraph>foo[]</paragraph>' );
  37. expect( command.isEnabled ).to.be.false;
  38. } );
  39. it( 'should be true if in table', () => {
  40. setData( model, modelTable( [ [ '[]' ] ] ) );
  41. expect( command.isEnabled ).to.be.true;
  42. } );
  43. } );
  44. describe( 'execute()', () => {
  45. it( 'should insert column in given table to the right of the selection\'s column', () => {
  46. setData( model, modelTable( [
  47. [ '11[]', '12' ],
  48. [ '21', '22' ]
  49. ] ) );
  50. command.execute();
  51. assertEqualMarkup( getData( model ), modelTable( [
  52. [ '11[]', '', '12' ],
  53. [ '21', '', '22' ]
  54. ] ) );
  55. } );
  56. it( 'should insert column in given table to the right of the selection\'s column (selection in block content)', () => {
  57. setData( model, modelTable( [
  58. [ '11', '<paragraph>12[]</paragraph>', '13' ]
  59. ] ) );
  60. command.execute();
  61. assertEqualMarkup( getData( model ), modelTable( [
  62. [ '11', '<paragraph>12[]</paragraph>', '', '13' ]
  63. ] ) );
  64. } );
  65. it( 'should insert column at table end', () => {
  66. setData( model, modelTable( [
  67. [ '11', '12' ],
  68. [ '21', '22[]' ]
  69. ] ) );
  70. command.execute();
  71. assertEqualMarkup( getData( model ), modelTable( [
  72. [ '11', '12', '' ],
  73. [ '21', '22[]', '' ]
  74. ] ) );
  75. } );
  76. it( 'should insert column after a multi column selection', () => {
  77. setData( model, modelTable( [
  78. [ '11', '12', '13' ],
  79. [ '21', '22', '23' ]
  80. ] ) );
  81. const tableSelection = editor.plugins.get( TableSelection );
  82. const modelRoot = model.document.getRoot();
  83. tableSelection.setCellSelection(
  84. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  85. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  86. );
  87. command.execute();
  88. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  89. [ '11', '12', '', '13' ],
  90. [ '21', '22', '', '23' ]
  91. ] ) );
  92. assertSelectedCells( model, [
  93. [ 1, 1, 0, 0 ],
  94. [ 1, 1, 0, 0 ]
  95. ] );
  96. } );
  97. it( 'should update table heading columns attribute when inserting column in headings section', () => {
  98. setData( model, modelTable( [
  99. [ '11[]', '12' ],
  100. [ '21', '22' ],
  101. [ '31', '32' ]
  102. ], { headingColumns: 2 } ) );
  103. command.execute();
  104. assertEqualMarkup( getData( model ), modelTable( [
  105. [ '11[]', '', '12' ],
  106. [ '21', '', '22' ],
  107. [ '31', '', '32' ]
  108. ], { headingColumns: 3 } ) );
  109. } );
  110. it( 'should not update table heading columns attribute when inserting column after headings section', () => {
  111. setData( model, modelTable( [
  112. [ '11', '12[]', '13' ],
  113. [ '21', '22', '23' ],
  114. [ '31', '32', '33' ]
  115. ], { headingColumns: 2 } ) );
  116. command.execute();
  117. assertEqualMarkup( getData( model ), modelTable( [
  118. [ '11', '12[]', '', '13' ],
  119. [ '21', '22', '', '23' ],
  120. [ '31', '32', '', '33' ]
  121. ], { headingColumns: 2 } ) );
  122. } );
  123. it( 'should skip spanned columns', () => {
  124. setData( model, modelTable( [
  125. [ '11[]', '12' ],
  126. [ { colspan: 2, contents: '21' } ],
  127. [ '31', '32' ]
  128. ], { headingColumns: 2 } ) );
  129. command.execute();
  130. assertEqualMarkup( getData( model ), modelTable( [
  131. [ '11[]', '', '12' ],
  132. [ { colspan: 3, contents: '21' } ],
  133. [ '31', '', '32' ]
  134. ], { headingColumns: 3 } ) );
  135. } );
  136. it( 'should skip wide spanned columns', () => {
  137. setData( model, modelTable( [
  138. [ '11', '12[]', '13', '14', '15' ],
  139. [ '21', '22', { colspan: 2, contents: '23' }, '25' ],
  140. [ { colspan: 4, contents: '31' }, { colspan: 2, contents: '34' } ]
  141. ], { headingColumns: 4 } ) );
  142. command.execute();
  143. assertEqualMarkup( getData( model ), modelTable( [
  144. [ '11', '12[]', '', '13', '14', '15' ],
  145. [ '21', '22', '', { colspan: 2, contents: '23' }, '25' ],
  146. [ { colspan: 5, contents: '31' }, { colspan: 2, contents: '34' } ]
  147. ], { headingColumns: 5 } ) );
  148. } );
  149. it( 'should insert a column when a widget in the table cell is selected', () => {
  150. setData( model, modelTable( [
  151. [ '11', '12' ],
  152. [ '21', '22' ],
  153. [ '31', '[<horizontalLine></horizontalLine>]' ]
  154. ] ) );
  155. command.execute();
  156. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  157. [ '11', '12', '' ],
  158. [ '21', '22', '' ],
  159. [ '31', '<horizontalLine></horizontalLine>', '' ]
  160. ] ) );
  161. } );
  162. } );
  163. } );
  164. describe( 'order=left', () => {
  165. beforeEach( () => {
  166. command = new InsertColumnCommand( editor, { order: 'left' } );
  167. } );
  168. describe( 'isEnabled', () => {
  169. it( 'should be false if wrong node', () => {
  170. setData( model, '<paragraph>foo[]</paragraph>' );
  171. expect( command.isEnabled ).to.be.false;
  172. } );
  173. it( 'should be true if in table', () => {
  174. setData( model, modelTable( [ [ '[]' ] ] ) );
  175. expect( command.isEnabled ).to.be.true;
  176. } );
  177. } );
  178. describe( 'execute()', () => {
  179. it( 'should insert column in given table to the left of the selection\'s column', () => {
  180. setData( model, modelTable( [
  181. [ '11', '12[]' ],
  182. [ '21', '22' ]
  183. ] ) );
  184. command.execute();
  185. assertEqualMarkup( getData( model ), modelTable( [
  186. [ '11', '', '12[]' ],
  187. [ '21', '', '22' ]
  188. ] ) );
  189. } );
  190. it( 'should insert column in given table to the left of the selection\'s column (selection in block content)', () => {
  191. setData( model, modelTable( [
  192. [ '11', '<paragraph>12[]</paragraph>', '13' ]
  193. ] ) );
  194. command.execute();
  195. assertEqualMarkup( getData( model ), modelTable( [
  196. [ '11', '', '<paragraph>12[]</paragraph>', '13' ]
  197. ] ) );
  198. } );
  199. it( 'should insert columns at the table start', () => {
  200. setData( model, modelTable( [
  201. [ '11', '12' ],
  202. [ '[]21', '22' ]
  203. ] ) );
  204. command.execute();
  205. assertEqualMarkup( getData( model ), modelTable( [
  206. [ '', '11', '12' ],
  207. [ '', '[]21', '22' ]
  208. ] ) );
  209. } );
  210. it( 'should insert column before a multi column selection', () => {
  211. setData( model, modelTable( [
  212. [ '11', '12', '13' ],
  213. [ '21', '22', '23' ]
  214. ] ) );
  215. const tableSelection = editor.plugins.get( TableSelection );
  216. const modelRoot = model.document.getRoot();
  217. tableSelection.setCellSelection(
  218. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  219. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  220. );
  221. command.execute();
  222. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  223. [ '', '11', '12', '13' ],
  224. [ '', '21', '22', '23' ]
  225. ] ) );
  226. assertSelectedCells( model, [
  227. [ 0, 1, 1, 0 ],
  228. [ 0, 1, 1, 0 ]
  229. ] );
  230. } );
  231. it( 'should update table heading columns attribute when inserting column in headings section', () => {
  232. setData( model, modelTable( [
  233. [ '11', '12[]' ],
  234. [ '21', '22' ],
  235. [ '31', '32' ]
  236. ], { headingColumns: 2 } ) );
  237. command.execute();
  238. assertEqualMarkup( getData( model ), modelTable( [
  239. [ '11', '', '12[]' ],
  240. [ '21', '', '22' ],
  241. [ '31', '', '32' ]
  242. ], { headingColumns: 3 } ) );
  243. } );
  244. it( 'should not update table heading columns attribute when inserting column after headings section', () => {
  245. setData( model, modelTable( [
  246. [ '11', '12', '13[]' ],
  247. [ '21', '22', '23' ],
  248. [ '31', '32', '33' ]
  249. ], { headingColumns: 2 } ) );
  250. command.execute();
  251. assertEqualMarkup( getData( model ), modelTable( [
  252. [ '11', '12', '', '13[]' ],
  253. [ '21', '22', '', '23' ],
  254. [ '31', '32', '', '33' ]
  255. ], { headingColumns: 2 } ) );
  256. } );
  257. it( 'should skip spanned columns', () => {
  258. setData( model, modelTable( [
  259. [ '11', '12[]' ],
  260. [ { colspan: 2, contents: '21' } ],
  261. [ '31', '32' ]
  262. ], { headingColumns: 2 } ) );
  263. command.execute();
  264. assertEqualMarkup( getData( model ), modelTable( [
  265. [ '11', '', '12[]' ],
  266. [ { colspan: 3, contents: '21' } ],
  267. [ '31', '', '32' ]
  268. ], { headingColumns: 3 } ) );
  269. } );
  270. it( 'should skip wide spanned columns', () => {
  271. setData( model, modelTable( [
  272. [ '11', '12', '13[]', '14', '15' ],
  273. [ '21', '22', { colspan: 2, contents: '23' }, '25' ],
  274. [ { colspan: 4, contents: '31' }, { colspan: 2, contents: '34' } ]
  275. ], { headingColumns: 4 } ) );
  276. command.execute();
  277. assertEqualMarkup( getData( model ), modelTable( [
  278. [ '11', '12', '', '13[]', '14', '15' ],
  279. [ '21', '22', '', { colspan: 2, contents: '23' }, '25' ],
  280. [ { colspan: 5, contents: '31' }, { colspan: 2, contents: '34' } ]
  281. ], { headingColumns: 5 } ) );
  282. } );
  283. } );
  284. } );
  285. } );