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