selectrowcommand.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 SelectRowCommand from '../../src/commands/selectrowcommand';
  8. import TableSelection from '../../src/tableselection';
  9. import { assertSelectedCells, defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
  10. describe( 'SelectRowCommand', () => {
  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 SelectRowCommand( 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 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 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 given row', () => {
  54. setData( model, modelTable( [
  55. [ '00', '01' ],
  56. [ '[]10', '11' ],
  57. [ '20', '21' ]
  58. ] ) );
  59. command.execute();
  60. assertSelectedCells( model, [
  61. [ 0, 0 ],
  62. [ 1, 1 ],
  63. [ 0, 0 ]
  64. ] );
  65. } );
  66. it( 'should select a given row 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, 1 ],
  75. [ 0, 0 ],
  76. [ 0, 0 ]
  77. ] );
  78. } );
  79. it( 'should select a given row 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, 1 ],
  88. [ 0, 0 ],
  89. [ 0, 0 ]
  90. ] );
  91. } );
  92. it( 'should select last row', () => {
  93. setData( model, modelTable( [
  94. [ '00', '01' ],
  95. [ '[]10', '11' ]
  96. ] ) );
  97. command.execute();
  98. assertSelectedCells( model, [
  99. [ 0, 0 ],
  100. [ 1, 1 ]
  101. ] );
  102. } );
  103. it( 'should select row-spanned cells', () => {
  104. // +----+----+----+----+----+
  105. // | 00 | 01 | 02 | 03 | 04 |
  106. // + + + +----+----+
  107. // | | | | 13 | 14 |
  108. // + + +----+ +----+
  109. // | | | 22 | | 24 |
  110. // + +----+----+----+----+
  111. // | | 31 | 32 | 33 | 34 |
  112. // +----+----+----+----+----+
  113. setData( model, modelTable( [
  114. [ { rowspan: 4, contents: '00' }, { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  115. [ { rowspan: 2, contents: '13' }, '14[]' ],
  116. [ '22', '23', '24' ],
  117. [ '30', '31', '32', '33', '34' ]
  118. ] ) );
  119. command.execute();
  120. /* eslint-disable no-multi-spaces */
  121. assertSelectedCells( model, [
  122. [ 0, 0, 0, 0, 0 ],
  123. [ 1, 1 ],
  124. [ 0, 0 ],
  125. [ 0, 0, 0, 0 ]
  126. ] );
  127. /* eslint-enable no-multi-spaces */
  128. } );
  129. describe( 'with multiple rows selected', () => {
  130. it( 'should properly select middle rows', () => {
  131. setData( model, modelTable( [
  132. [ '00', '01' ],
  133. [ '10', '11' ],
  134. [ '20', '21' ],
  135. [ '30', '31' ]
  136. ] ) );
  137. tableSelection._setCellSelection(
  138. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  139. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  140. );
  141. command.execute();
  142. assertSelectedCells( model, [
  143. [ 0, 0 ],
  144. [ 1, 1 ],
  145. [ 1, 1 ],
  146. [ 0, 0 ]
  147. ] );
  148. } );
  149. it( 'should properly select middle rows in reversed order', () => {
  150. setData( model, modelTable( [
  151. [ '00', '01' ],
  152. [ '10', '11' ],
  153. [ '20', '21' ],
  154. [ '30', '31' ]
  155. ] ) );
  156. tableSelection._setCellSelection(
  157. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  158. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  159. );
  160. command.execute();
  161. assertSelectedCells( model, [
  162. [ 0, 0 ],
  163. [ 1, 1 ],
  164. [ 1, 1 ],
  165. [ 0, 0 ]
  166. ] );
  167. } );
  168. it( 'should properly select tailing rows', () => {
  169. setData( model, modelTable( [
  170. [ '00', '01' ],
  171. [ '10', '11' ],
  172. [ '20', '21' ],
  173. [ '30', '31' ]
  174. ] ) );
  175. tableSelection._setCellSelection(
  176. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  177. modelRoot.getNodeByPath( [ 0, 3, 0 ] )
  178. );
  179. command.execute();
  180. assertSelectedCells( model, [
  181. [ 0, 0 ],
  182. [ 0, 0 ],
  183. [ 1, 1 ],
  184. [ 1, 1 ]
  185. ] );
  186. } );
  187. it( 'should properly select beginning rows', () => {
  188. setData( model, modelTable( [
  189. [ '00', '01' ],
  190. [ '10', '11' ],
  191. [ '20', '21' ],
  192. [ '30', '31' ]
  193. ] ) );
  194. tableSelection._setCellSelection(
  195. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  196. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  197. );
  198. command.execute();
  199. assertSelectedCells( model, [
  200. [ 1, 1 ],
  201. [ 1, 1 ],
  202. [ 0, 0 ],
  203. [ 0, 0 ]
  204. ] );
  205. } );
  206. it( 'should properly select multiple rows from square selection', () => {
  207. setData( model, modelTable( [
  208. [ '00', '01', '02' ],
  209. [ '10', '11', '12' ],
  210. [ '20', '21', '22' ],
  211. [ '30', '31', '32' ]
  212. ] ) );
  213. tableSelection._setCellSelection(
  214. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  215. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  216. );
  217. command.execute();
  218. assertSelectedCells( model, [
  219. [ 0, 0, 0 ],
  220. [ 1, 1, 1 ],
  221. [ 1, 1, 1 ],
  222. [ 0, 0, 0 ]
  223. ] );
  224. } );
  225. it( 'should support selecting mixed heading and cell rows', () => {
  226. setData( model, modelTable( [
  227. [ '00', '01' ],
  228. [ '10', '11' ],
  229. [ '20', '21' ]
  230. ], { headingColumns: 1 } ) );
  231. tableSelection._setCellSelection(
  232. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  233. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  234. );
  235. command.execute();
  236. assertSelectedCells( model, [
  237. [ 1, 1 ],
  238. [ 1, 1 ],
  239. [ 0, 0 ]
  240. ] );
  241. } );
  242. } );
  243. describe( 'with entire row selected', () => {
  244. it( 'should select a row if all its cells are selected', () => {
  245. setData( model, modelTable( [
  246. [ '00', '01' ],
  247. [ '10', '11' ],
  248. [ '20', '21' ]
  249. ] ) );
  250. tableSelection._setCellSelection(
  251. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  252. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  253. );
  254. command.execute();
  255. assertSelectedCells( model, [
  256. [ 0, 0 ],
  257. [ 1, 1 ],
  258. [ 0, 0 ]
  259. ] );
  260. } );
  261. it( 'should properly select row if reversed selection is made', () => {
  262. setData( model, modelTable( [
  263. [ '00', '01' ],
  264. [ '10', '11' ]
  265. ] ) );
  266. tableSelection._setCellSelection(
  267. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  268. modelRoot.getNodeByPath( [ 0, 0, 0 ] )
  269. );
  270. command.execute();
  271. assertSelectedCells( model, [
  272. [ 1, 1 ],
  273. [ 0, 0 ]
  274. ] );
  275. } );
  276. } );
  277. } );
  278. } );