selectcolumncommand.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 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 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 given column from a table start', () => {
  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 given column from a table start when selection is at the end', () => {
  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 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. it( 'should select col-spanned cells', () => {
  104. // +----+----+----+----+
  105. // | 00 |
  106. // +----+----+----+----+
  107. // | 10 | 13 |
  108. // +----+----+----+----+
  109. // | 20 | 22 | 23 |
  110. // +----+----+----+----+
  111. // | 30 | 31 | 33 |
  112. // +----+----+----+----+
  113. // | 40 | 41 | 42 | 43 |
  114. // +----+----+----+----+
  115. setData( model, modelTable( [
  116. [ { colspan: 4, contents: '00' } ],
  117. [ { colspan: 3, contents: '10' }, '13' ],
  118. [ { colspan: 2, contents: '20' }, '22', '23' ],
  119. [ '30', { colspan: 2, contents: '31' }, '33' ],
  120. [ '40', '41[]', '42', '43' ]
  121. ] ) );
  122. command.execute();
  123. /* eslint-disable no-multi-spaces */
  124. assertSelectedCells( model, [
  125. [ 0 ],
  126. [ 0, 0 ],
  127. [ 0, 0, 0 ],
  128. [ 0, 1, 0 ],
  129. [ 0, 1, 0, 0 ]
  130. ] );
  131. /* eslint-enable no-multi-spaces */
  132. } );
  133. describe( 'with multiple columns selected', () => {
  134. it( 'should properly select middle columns', () => {
  135. setData( model, modelTable( [
  136. [ '00', '01', '02', '03' ],
  137. [ '10', '11', '12', '13' ],
  138. [ '20', '21', '22', '23' ]
  139. ] ) );
  140. tableSelection._setCellSelection(
  141. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  142. modelRoot.getNodeByPath( [ 0, 0, 2 ] )
  143. );
  144. command.execute();
  145. assertSelectedCells( model, [
  146. [ 0, 1, 1, 0 ],
  147. [ 0, 1, 1, 0 ],
  148. [ 0, 1, 1, 0 ]
  149. ] );
  150. } );
  151. it( 'should properly select middle columns in reversed order', () => {
  152. setData( model, modelTable( [
  153. [ '00', '01', '02', '03' ],
  154. [ '10', '11', '12', '13' ],
  155. [ '20', '21', '22', '23' ]
  156. ] ) );
  157. tableSelection._setCellSelection(
  158. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  159. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  160. );
  161. command.execute();
  162. assertSelectedCells( model, [
  163. [ 0, 1, 1, 0 ],
  164. [ 0, 1, 1, 0 ],
  165. [ 0, 1, 1, 0 ]
  166. ] );
  167. } );
  168. it( 'should properly select tailing columns', () => {
  169. setData( model, modelTable( [
  170. [ '00', '01', '02', '03' ],
  171. [ '10', '11', '12', '13' ],
  172. [ '20', '21', '22', '23' ]
  173. ] ) );
  174. tableSelection._setCellSelection(
  175. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  176. modelRoot.getNodeByPath( [ 0, 0, 3 ] )
  177. );
  178. command.execute();
  179. assertSelectedCells( model, [
  180. [ 0, 0, 1, 1 ],
  181. [ 0, 0, 1, 1 ],
  182. [ 0, 0, 1, 1 ]
  183. ] );
  184. } );
  185. it( 'should properly select beginning columns', () => {
  186. setData( model, modelTable( [
  187. [ '00', '01', '02', '03' ],
  188. [ '10', '11', '12', '13' ],
  189. [ '20', '21', '22', '23' ]
  190. ] ) );
  191. tableSelection._setCellSelection(
  192. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  193. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  194. );
  195. command.execute();
  196. assertSelectedCells( model, [
  197. [ 1, 1, 0, 0 ],
  198. [ 1, 1, 0, 0 ],
  199. [ 1, 1, 0, 0 ]
  200. ] );
  201. } );
  202. it( 'should properly select multiple columns from square selection', () => {
  203. setData( model, modelTable( [
  204. [ '00', '01', '02' ],
  205. [ '10', '11', '12' ],
  206. [ '20', '21', '22' ]
  207. ] ) );
  208. tableSelection._setCellSelection(
  209. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  210. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  211. );
  212. command.execute();
  213. assertSelectedCells( model, [
  214. [ 0, 1, 1 ],
  215. [ 0, 1, 1 ],
  216. [ 0, 1, 1 ]
  217. ] );
  218. } );
  219. it( 'should support selecting mixed heading and cell columns', () => {
  220. setData( model, modelTable( [
  221. [ '00', '01', '02', '03' ],
  222. [ '10', '11', '12', '13' ],
  223. [ '20', '21', '22', '23' ]
  224. ], { headingRows: 1 } ) );
  225. tableSelection._setCellSelection(
  226. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  227. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  228. );
  229. command.execute();
  230. assertSelectedCells( model, [
  231. [ 1, 1, 0, 0 ],
  232. [ 1, 1, 0, 0 ],
  233. [ 1, 1, 0, 0 ]
  234. ] );
  235. } );
  236. } );
  237. describe( 'with entire column selected', () => {
  238. it( 'should select a column if all its cells are selected', () => {
  239. setData( model, modelTable( [
  240. [ '00', '01', '02' ],
  241. [ '10', '11', '12' ],
  242. [ '20', '21', '22' ]
  243. ] ) );
  244. tableSelection._setCellSelection(
  245. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  246. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  247. );
  248. command.execute();
  249. assertSelectedCells( model, [
  250. [ 0, 1, 0 ],
  251. [ 0, 1, 0 ],
  252. [ 0, 1, 0 ]
  253. ] );
  254. } );
  255. it( 'should properly select column if reversed selection is made', () => {
  256. setData( model, modelTable( [
  257. [ '00', '01' ],
  258. [ '10', '11' ]
  259. ] ) );
  260. tableSelection._setCellSelection(
  261. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  262. modelRoot.getNodeByPath( [ 0, 0, 0 ] )
  263. );
  264. command.execute();
  265. assertSelectedCells( model, [
  266. [ 1, 0 ],
  267. [ 1, 0 ]
  268. ] );
  269. } );
  270. } );
  271. } );
  272. } );