8
0

selectcolumncommand.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import SelectColumnCommand from '../../src/commands/selectcolumncommand';
  8. import TableSelection from '../../src/tableselection';
  9. import { assertSelectedCells, defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
  10. describe( 'SelectColumnCommand', () => {
  11. let editor, model, modelRoot, command, tableSelection;
  12. beforeEach( () => {
  13. return VirtualTestEditor.create( { plugins: [ TableSelection ] } )
  14. .then( newEditor => {
  15. editor = newEditor;
  16. model = editor.model;
  17. modelRoot = model.document.getRoot();
  18. command = new SelectColumnCommand( editor );
  19. tableSelection = editor.plugins.get( TableSelection );
  20. defaultSchema( model.schema );
  21. defaultConversion( editor.conversion );
  22. } );
  23. } );
  24. afterEach( () => {
  25. return editor.destroy();
  26. } );
  27. describe( 'isEnabled', () => {
  28. it( 'should be true if the selection is inside table cell', () => {
  29. setData( model, modelTable( [
  30. [ '00[]', '01' ],
  31. [ '10', '11' ]
  32. ] ) );
  33. expect( command.isEnabled ).to.be.true;
  34. } );
  35. it( 'should be true if the selection contains multiple cells', () => {
  36. setData( model, modelTable( [
  37. [ '00', '01' ],
  38. [ '10', '11' ],
  39. [ '20', '21' ]
  40. ] ) );
  41. tableSelection.setCellSelection(
  42. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  43. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  44. );
  45. expect( command.isEnabled ).to.be.true;
  46. } );
  47. it( 'should be false if the selection is outside a table', () => {
  48. setData( model, '<paragraph>11[]</paragraph>' );
  49. expect( command.isEnabled ).to.be.false;
  50. } );
  51. } );
  52. describe( 'execute()', () => {
  53. it( 'should select a column of a table cell with a collapsed selection', () => {
  54. setData( model, modelTable( [
  55. [ '00', '01', '02' ],
  56. [ '10', '[]11', '12' ],
  57. [ '20', '21', '22' ]
  58. ] ) );
  59. command.execute();
  60. assertSelectedCells( model, [
  61. [ 0, 1, 0 ],
  62. [ 0, 1, 0 ],
  63. [ 0, 1, 0 ]
  64. ] );
  65. } );
  66. it( 'should select a column of table cell with a collapsed selection in first table cell', () => {
  67. setData( model, modelTable( [
  68. [ '[]00', '01' ],
  69. [ '10', '11' ],
  70. [ '20', '21' ]
  71. ] ) );
  72. command.execute();
  73. assertSelectedCells( model, [
  74. [ 1, 0 ],
  75. [ 1, 0 ],
  76. [ 1, 0 ]
  77. ] );
  78. } );
  79. it( 'should select a column of table cell with a collapsed selection in last cell in the first column', () => {
  80. setData( model, modelTable( [
  81. [ '00', '01' ],
  82. [ '10', '11' ],
  83. [ '20[]', '21' ]
  84. ] ) );
  85. command.execute();
  86. assertSelectedCells( model, [
  87. [ 1, 0 ],
  88. [ 1, 0 ],
  89. [ 1, 0 ]
  90. ] );
  91. } );
  92. it( 'should select a column of table cell with collapsed selection in the first cell of the last column', () => {
  93. setData( model, modelTable( [
  94. [ '00', '01[]' ],
  95. [ '10', '11' ]
  96. ] ) );
  97. command.execute();
  98. assertSelectedCells( model, [
  99. [ 0, 1 ],
  100. [ 0, 1 ]
  101. ] );
  102. } );
  103. describe( 'with col-spanned cells', () => {
  104. beforeEach( () => {
  105. // +----+----+----+----+
  106. // | 00 |
  107. // +----+----+----+----+
  108. // | 10 | 13 |
  109. // +----+----+----+----+
  110. // | 20 | 22 | 23 |
  111. // +----+----+----+----+
  112. // | 30 | 31 | 33 |
  113. // +----+----+----+----+
  114. // | 40 | 41 | 42 | 43 |
  115. // +----+----+----+----+
  116. setData( model, modelTable( [
  117. [ { colspan: 4, contents: '00' } ],
  118. [ { colspan: 3, contents: '10' }, '13' ],
  119. [ { colspan: 2, contents: '20' }, '22', '23' ],
  120. [ '30', { colspan: 2, contents: '31' }, '33' ],
  121. [ '40', '41', '42', '43' ]
  122. ] ) );
  123. } );
  124. it( 'should select only one column if only one cell is selected', () => {
  125. // Selection in cell 10.
  126. tableSelection.setCellSelection(
  127. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  128. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  129. );
  130. /* eslint-disable no-multi-spaces */
  131. assertSelectedCells( model, [
  132. [ 0 ],
  133. [ 1, 0 ],
  134. [ 0, 0, 0 ],
  135. [ 0, 0, 0 ],
  136. [ 0, 0, 0, 0 ]
  137. ] );
  138. /* eslint-enable no-multi-spaces */
  139. command.execute();
  140. /* eslint-disable no-multi-spaces */
  141. assertSelectedCells( model, [
  142. [ 1 ],
  143. [ 1, 0 ],
  144. [ 1, 0, 0 ],
  145. [ 1, 0, 0 ],
  146. [ 1, 0, 0, 0 ]
  147. ] );
  148. /* eslint-enable no-multi-spaces */
  149. } );
  150. it( 'should not select col-spanned columns that start in other column', () => {
  151. // Selection in cell 42.
  152. tableSelection.setCellSelection(
  153. modelRoot.getNodeByPath( [ 0, 4, 2 ] ),
  154. modelRoot.getNodeByPath( [ 0, 4, 2 ] )
  155. );
  156. /* eslint-disable no-multi-spaces */
  157. assertSelectedCells( model, [
  158. [ 0 ],
  159. [ 0, 0 ],
  160. [ 0, 0, 0 ],
  161. [ 0, 0, 0 ],
  162. [ 0, 0, 1, 0 ]
  163. ] );
  164. /* eslint-enable no-multi-spaces */
  165. command.execute();
  166. /* eslint-disable no-multi-spaces */
  167. assertSelectedCells( model, [
  168. [ 0 ],
  169. [ 0, 0 ],
  170. [ 0, 1, 0 ],
  171. [ 0, 0, 0 ],
  172. [ 0, 0, 1, 0 ]
  173. ] );
  174. /* eslint-enable no-multi-spaces */
  175. } );
  176. it( 'should not select col-spanned columns that start in other column but include those that start in selected column', () => {
  177. // Selection in cell 41.
  178. tableSelection.setCellSelection(
  179. modelRoot.getNodeByPath( [ 0, 4, 1 ] ),
  180. modelRoot.getNodeByPath( [ 0, 4, 1 ] )
  181. );
  182. /* eslint-disable no-multi-spaces */
  183. assertSelectedCells( model, [
  184. [ 0 ],
  185. [ 0, 0 ],
  186. [ 0, 0, 0 ],
  187. [ 0, 0, 0 ],
  188. [ 0, 1, 0, 0 ]
  189. ] );
  190. /* eslint-enable no-multi-spaces */
  191. command.execute();
  192. /* eslint-disable no-multi-spaces */
  193. assertSelectedCells( model, [
  194. [ 0 ],
  195. [ 0, 0 ],
  196. [ 0, 0, 0 ],
  197. [ 0, 1, 0 ],
  198. [ 0, 1, 0, 0 ]
  199. ] );
  200. /* eslint-enable no-multi-spaces */
  201. } );
  202. it( 'should select properly for multiple not spanned cells selected', () => {
  203. // Selection in cells 40 - 41.
  204. tableSelection.setCellSelection(
  205. modelRoot.getNodeByPath( [ 0, 4, 0 ] ),
  206. modelRoot.getNodeByPath( [ 0, 4, 1 ] )
  207. );
  208. /* eslint-disable no-multi-spaces */
  209. assertSelectedCells( model, [
  210. [ 0 ],
  211. [ 0, 0 ],
  212. [ 0, 0, 0 ],
  213. [ 0, 0, 0 ],
  214. [ 1, 1, 0, 0 ]
  215. ] );
  216. /* eslint-enable no-multi-spaces */
  217. command.execute();
  218. /* eslint-disable no-multi-spaces */
  219. assertSelectedCells( model, [
  220. [ 1 ],
  221. [ 1, 0 ],
  222. [ 1, 0, 0 ],
  223. [ 1, 1, 0 ],
  224. [ 1, 1, 0, 0 ]
  225. ] );
  226. /* eslint-enable no-multi-spaces */
  227. } );
  228. it( 'should select properly for multiple cells selected including spanned one', () => {
  229. // Selection in cells 31 - 33.
  230. tableSelection.setCellSelection(
  231. modelRoot.getNodeByPath( [ 0, 3, 1 ] ),
  232. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  233. );
  234. /* eslint-disable no-multi-spaces */
  235. assertSelectedCells( model, [
  236. [ 0 ],
  237. [ 0, 0 ],
  238. [ 0, 0, 0 ],
  239. [ 0, 1, 1 ],
  240. [ 0, 0, 0, 0 ]
  241. ] );
  242. /* eslint-enable no-multi-spaces */
  243. command.execute();
  244. /* eslint-disable no-multi-spaces */
  245. assertSelectedCells( model, [
  246. [ 0 ],
  247. [ 0, 1 ],
  248. [ 0, 1, 1 ],
  249. [ 0, 1, 1 ],
  250. [ 0, 1, 1, 1 ]
  251. ] );
  252. /* eslint-enable no-multi-spaces */
  253. } );
  254. } );
  255. describe( 'with multiple columns selected', () => {
  256. beforeEach( () => {
  257. setData( model, modelTable( [
  258. [ '00', '01', '02', '03' ],
  259. [ '10', '11', '12', '13' ],
  260. [ '20', '21', '22', '23' ]
  261. ] ) );
  262. } );
  263. it( 'should properly select middle columns', () => {
  264. tableSelection.setCellSelection(
  265. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  266. modelRoot.getNodeByPath( [ 0, 0, 2 ] )
  267. );
  268. command.execute();
  269. assertSelectedCells( model, [
  270. [ 0, 1, 1, 0 ],
  271. [ 0, 1, 1, 0 ],
  272. [ 0, 1, 1, 0 ]
  273. ] );
  274. } );
  275. it( 'should properly select middle columns in reversed order', () => {
  276. tableSelection.setCellSelection(
  277. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  278. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  279. );
  280. command.execute();
  281. assertSelectedCells( model, [
  282. [ 0, 1, 1, 0 ],
  283. [ 0, 1, 1, 0 ],
  284. [ 0, 1, 1, 0 ]
  285. ] );
  286. } );
  287. it( 'should properly select tailing columns', () => {
  288. tableSelection.setCellSelection(
  289. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  290. modelRoot.getNodeByPath( [ 0, 0, 3 ] )
  291. );
  292. command.execute();
  293. assertSelectedCells( model, [
  294. [ 0, 0, 1, 1 ],
  295. [ 0, 0, 1, 1 ],
  296. [ 0, 0, 1, 1 ]
  297. ] );
  298. } );
  299. it( 'should properly select beginning columns', () => {
  300. tableSelection.setCellSelection(
  301. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  302. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  303. );
  304. command.execute();
  305. assertSelectedCells( model, [
  306. [ 1, 1, 0, 0 ],
  307. [ 1, 1, 0, 0 ],
  308. [ 1, 1, 0, 0 ]
  309. ] );
  310. } );
  311. it( 'should properly select multiple columns from square selection', () => {
  312. tableSelection.setCellSelection(
  313. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  314. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  315. );
  316. command.execute();
  317. assertSelectedCells( model, [
  318. [ 0, 1, 1, 0 ],
  319. [ 0, 1, 1, 0 ],
  320. [ 0, 1, 1, 0 ]
  321. ] );
  322. } );
  323. it( 'should support selecting mixed heading and cell columns', () => {
  324. setData( model, modelTable( [
  325. [ '00', '01', '02', '03' ],
  326. [ '10', '11', '12', '13' ],
  327. [ '20', '21', '22', '23' ]
  328. ], { headingRows: 1 } ) );
  329. tableSelection.setCellSelection(
  330. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  331. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  332. );
  333. command.execute();
  334. assertSelectedCells( model, [
  335. [ 1, 1, 0, 0 ],
  336. [ 1, 1, 0, 0 ],
  337. [ 1, 1, 0, 0 ]
  338. ] );
  339. } );
  340. } );
  341. describe( 'with entire column selected', () => {
  342. it( 'should select a column if all its cells are selected', () => {
  343. setData( model, modelTable( [
  344. [ '00', '01', '02' ],
  345. [ '10', '11', '12' ],
  346. [ '20', '21', '22' ]
  347. ] ) );
  348. tableSelection.setCellSelection(
  349. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  350. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  351. );
  352. command.execute();
  353. assertSelectedCells( model, [
  354. [ 0, 1, 0 ],
  355. [ 0, 1, 0 ],
  356. [ 0, 1, 0 ]
  357. ] );
  358. } );
  359. it( 'should properly select column if reversed selection is made', () => {
  360. setData( model, modelTable( [
  361. [ '00', '01' ],
  362. [ '10', '11' ]
  363. ] ) );
  364. tableSelection.setCellSelection(
  365. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  366. modelRoot.getNodeByPath( [ 0, 0, 0 ] )
  367. );
  368. command.execute();
  369. assertSelectedCells( model, [
  370. [ 1, 0 ],
  371. [ 1, 0 ]
  372. ] );
  373. } );
  374. } );
  375. } );
  376. } );