insertcolumncommand.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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, modelTable } from '../_utils/utils';
  11. import TableUtils from '../../src/tableutils';
  12. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  13. import TableCellPropertiesEditing from '../../src/tablecellproperties/tablecellpropertiesediting';
  14. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  15. describe( 'InsertColumnCommand', () => {
  16. let editor, model, command;
  17. beforeEach( () => {
  18. return ModelTestEditor
  19. .create( {
  20. plugins: [ Paragraph, TableCellPropertiesEditing, TableUtils, TableSelection, HorizontalLineEditing ]
  21. } )
  22. .then( newEditor => {
  23. editor = newEditor;
  24. model = editor.model;
  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. // +----+----+----+----+----+----+
  138. // | 00 | 01 | 02 | 03 | 04 | 05 |
  139. // +----+----+----+----+----+----+
  140. // | 10 | 11 | 12 | 14 | 15 |
  141. // +----+----+----+----+----+----+
  142. // | 20 | 24 |
  143. // +----+----+----+----+----+----+
  144. // ^-- heading columns
  145. setData( model, modelTable( [
  146. [ '00', '01[]', '02', '03', '04', '05' ],
  147. [ '10', '11', { contents: '12', colspan: 2 }, '14', '15' ],
  148. [ { contents: '20', colspan: 4 }, { contents: '24', colspan: 2 } ]
  149. ], { headingColumns: 4 } ) );
  150. command.execute();
  151. // +----+----+----+----+----+----+----+
  152. // | 00 | 01 | | 02 | 03 | 04 | 05 |
  153. // +----+----+----+----+----+----+----+
  154. // | 10 | 11 | | 12 | 14 | 15 |
  155. // +----+----+----+----+----+----+----+
  156. // | 20 | 24 |
  157. // +----+----+----+----+----+----+----+
  158. // ^-- heading columns
  159. assertEqualMarkup( getData( model ), modelTable( [
  160. [ '00', '01[]', '', '02', '03', '04', '05' ],
  161. [ '10', '11', '', { contents: '12', colspan: 2 }, '14', '15' ],
  162. [ { contents: '20', colspan: 5 }, { contents: '24', colspan: 2 } ]
  163. ], { headingColumns: 5 } ) );
  164. } );
  165. it( 'should insert a column when a widget in the table cell is selected', () => {
  166. setData( model, modelTable( [
  167. [ '11', '12' ],
  168. [ '21', '22' ],
  169. [ '31', '[<horizontalLine></horizontalLine>]' ]
  170. ] ) );
  171. command.execute();
  172. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  173. [ '11', '12', '' ],
  174. [ '21', '22', '' ],
  175. [ '31', '<horizontalLine></horizontalLine>', '' ]
  176. ] ) );
  177. } );
  178. } );
  179. } );
  180. describe( 'order=left', () => {
  181. beforeEach( () => {
  182. command = new InsertColumnCommand( editor, { order: 'left' } );
  183. } );
  184. describe( 'isEnabled', () => {
  185. it( 'should be false if wrong node', () => {
  186. setData( model, '<paragraph>foo[]</paragraph>' );
  187. expect( command.isEnabled ).to.be.false;
  188. } );
  189. it( 'should be true if in table', () => {
  190. setData( model, modelTable( [ [ '[]' ] ] ) );
  191. expect( command.isEnabled ).to.be.true;
  192. } );
  193. } );
  194. describe( 'execute()', () => {
  195. it( 'should insert column in given table to the left of the selection\'s column', () => {
  196. setData( model, modelTable( [
  197. [ '11', '12[]' ],
  198. [ '21', '22' ]
  199. ] ) );
  200. command.execute();
  201. assertEqualMarkup( getData( model ), modelTable( [
  202. [ '11', '', '12[]' ],
  203. [ '21', '', '22' ]
  204. ] ) );
  205. } );
  206. it( 'should insert column in given table to the left of the selection\'s column (selection in block content)', () => {
  207. setData( model, modelTable( [
  208. [ '11', '<paragraph>12[]</paragraph>', '13' ]
  209. ] ) );
  210. command.execute();
  211. assertEqualMarkup( getData( model ), modelTable( [
  212. [ '11', '', '<paragraph>12[]</paragraph>', '13' ]
  213. ] ) );
  214. } );
  215. it( 'should insert columns at the table start', () => {
  216. setData( model, modelTable( [
  217. [ '11', '12' ],
  218. [ '[]21', '22' ]
  219. ] ) );
  220. command.execute();
  221. assertEqualMarkup( getData( model ), modelTable( [
  222. [ '', '11', '12' ],
  223. [ '', '[]21', '22' ]
  224. ] ) );
  225. } );
  226. it( 'should insert column before a multi column selection', () => {
  227. setData( model, modelTable( [
  228. [ '11', '12', '13' ],
  229. [ '21', '22', '23' ]
  230. ] ) );
  231. const tableSelection = editor.plugins.get( TableSelection );
  232. const modelRoot = model.document.getRoot();
  233. tableSelection.setCellSelection(
  234. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  235. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  236. );
  237. command.execute();
  238. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  239. [ '', '11', '12', '13' ],
  240. [ '', '21', '22', '23' ]
  241. ] ) );
  242. assertSelectedCells( model, [
  243. [ 0, 1, 1, 0 ],
  244. [ 0, 1, 1, 0 ]
  245. ] );
  246. } );
  247. it( 'should update table heading columns attribute when inserting column in headings section', () => {
  248. setData( model, modelTable( [
  249. [ '11', '12[]' ],
  250. [ '21', '22' ],
  251. [ '31', '32' ]
  252. ], { headingColumns: 2 } ) );
  253. command.execute();
  254. assertEqualMarkup( getData( model ), modelTable( [
  255. [ '11', '', '12[]' ],
  256. [ '21', '', '22' ],
  257. [ '31', '', '32' ]
  258. ], { headingColumns: 3 } ) );
  259. } );
  260. it( 'should not update table heading columns attribute when inserting column after headings section', () => {
  261. setData( model, modelTable( [
  262. [ '11', '12', '13[]' ],
  263. [ '21', '22', '23' ],
  264. [ '31', '32', '33' ]
  265. ], { headingColumns: 2 } ) );
  266. command.execute();
  267. assertEqualMarkup( getData( model ), modelTable( [
  268. [ '11', '12', '', '13[]' ],
  269. [ '21', '22', '', '23' ],
  270. [ '31', '32', '', '33' ]
  271. ], { headingColumns: 2 } ) );
  272. } );
  273. it( 'should skip spanned columns', () => {
  274. setData( model, modelTable( [
  275. [ '11', '12[]' ],
  276. [ { colspan: 2, contents: '21' } ],
  277. [ '31', '32' ]
  278. ], { headingColumns: 2 } ) );
  279. command.execute();
  280. assertEqualMarkup( getData( model ), modelTable( [
  281. [ '11', '', '12[]' ],
  282. [ { colspan: 3, contents: '21' } ],
  283. [ '31', '', '32' ]
  284. ], { headingColumns: 3 } ) );
  285. } );
  286. it( 'should skip wide spanned columns', () => {
  287. // +----+----+----+----+----+----+
  288. // | 00 | 01 | 02 | 03 | 04 | 05 |
  289. // +----+----+----+----+----+----+
  290. // | 10 | 11 | 12 | 14 | 15 |
  291. // +----+----+----+----+----+----+
  292. // | 20 | 24 |
  293. // +----+----+----+----+----+----+
  294. // ^-- heading columns
  295. setData( model, modelTable( [
  296. [ '00', '01', '[]02', '03', '04', '05' ],
  297. [ '10', '11', { contents: '12', colspan: 2 }, '14', '15' ],
  298. [ { contents: '20', colspan: 4 }, { contents: '24', colspan: 2 } ]
  299. ], { headingColumns: 4 } ) );
  300. command.execute();
  301. // +----+----+----+----+----+----+----+
  302. // | 00 | 01 | | 02 | 03 | 04 | 05 |
  303. // +----+----+----+----+----+----+----+
  304. // | 10 | 11 | | 12 | 14 | 15 |
  305. // +----+----+----+----+----+----+----+
  306. // | 20 | 24 |
  307. // +----+----+----+----+----+----+----+
  308. // ^-- heading columns
  309. assertEqualMarkup( getData( model ), modelTable( [
  310. [ '00', '01', '', '[]02', '03', '04', '05' ],
  311. [ '10', '11', '', { contents: '12', colspan: 2 }, '14', '15' ],
  312. [ { contents: '20', colspan: 5 }, { contents: '24', colspan: 2 } ]
  313. ], { headingColumns: 5 } ) );
  314. } );
  315. } );
  316. } );
  317. } );