tableselection.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import TableEditing from '../src/tableediting';
  9. import TableSelection from '../src/tableselection';
  10. import { modelTable } from './_utils/utils';
  11. describe( 'table selection', () => {
  12. let editor, model, tableSelection, modelRoot;
  13. beforeEach( async () => {
  14. editor = await VirtualTestEditor.create( {
  15. plugins: [ TableEditing, TableSelection, Paragraph ]
  16. } );
  17. model = editor.model;
  18. modelRoot = model.document.getRoot();
  19. tableSelection = editor.plugins.get( TableSelection );
  20. setModelData( model, modelTable( [
  21. [ '11[]', '12', '13' ],
  22. [ '21', '22', '23' ],
  23. [ '31', '32', '33' ]
  24. ] ) );
  25. } );
  26. afterEach( async () => {
  27. await editor.destroy();
  28. } );
  29. describe( 'TableSelection', () => {
  30. describe( 'startSelectingFrom()', () => {
  31. it( 'should not change model selection', () => {
  32. const spy = sinon.spy();
  33. model.document.selection.on( 'change', spy );
  34. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  35. sinon.assert.notCalled( spy );
  36. } );
  37. } );
  38. describe( 'setSelectingTo()', () => {
  39. it( 'should set model selection on passed cell if startSelectingFrom() was not used', () => {
  40. const spy = sinon.spy();
  41. model.document.selection.on( 'change', spy );
  42. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  43. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  44. sinon.assert.calledOnce( spy );
  45. assertSelectedCells( [
  46. [ 1, 1, 0 ],
  47. [ 0, 0, 0 ],
  48. [ 0, 0, 0 ]
  49. ] );
  50. } );
  51. it( 'should change model selection if valid selection will be set', () => {
  52. const spy = sinon.spy();
  53. model.document.selection.on( 'change', spy );
  54. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  55. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  56. sinon.assert.calledOnce( spy );
  57. } );
  58. it( 'should not change model selection if passed table cell is from other table then start cell', () => {
  59. setModelData( model,
  60. modelTable( [
  61. [ '11[]', '12', '13' ],
  62. [ '21', '22', '23' ],
  63. [ '31', '32', '33' ]
  64. ] ) +
  65. modelTable( [
  66. [ 'a', 'b' ],
  67. [ 'c', 'd' ]
  68. ] )
  69. );
  70. const spy = sinon.spy();
  71. model.document.selection.on( 'change', spy );
  72. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  73. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 1, 0, 1 ] ) );
  74. sinon.assert.notCalled( spy );
  75. } );
  76. it( 'should select two table cells', () => {
  77. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  78. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  79. assertSelectedCells( [
  80. [ 1, 1, 0 ],
  81. [ 0, 0, 0 ],
  82. [ 0, 0, 0 ]
  83. ] );
  84. } );
  85. it( 'should select four table cells for diagonal selection', () => {
  86. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  87. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  88. assertSelectedCells( [
  89. [ 1, 1, 0 ],
  90. [ 1, 1, 0 ],
  91. [ 0, 0, 0 ]
  92. ] );
  93. } );
  94. it( 'should select row table cells', () => {
  95. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  96. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 2 ] ) );
  97. assertSelectedCells( [
  98. [ 1, 1, 1 ],
  99. [ 0, 0, 0 ],
  100. [ 0, 0, 0 ]
  101. ] );
  102. } );
  103. it( 'should select column table cells', () => {
  104. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  105. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) );
  106. assertSelectedCells( [
  107. [ 0, 1, 0 ],
  108. [ 0, 1, 0 ],
  109. [ 0, 1, 0 ]
  110. ] );
  111. } );
  112. it( 'should create proper selection on consecutive changes', () => {
  113. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  114. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) );
  115. assertSelectedCells( [
  116. [ 0, 0, 0 ],
  117. [ 0, 1, 0 ],
  118. [ 0, 1, 0 ]
  119. ] );
  120. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  121. assertSelectedCells( [
  122. [ 0, 1, 0 ],
  123. [ 0, 1, 0 ],
  124. [ 0, 0, 0 ]
  125. ] );
  126. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 2 ] ) );
  127. assertSelectedCells( [
  128. [ 0, 0, 0 ],
  129. [ 0, 1, 1 ],
  130. [ 0, 1, 1 ]
  131. ] );
  132. } );
  133. } );
  134. describe( 'stopSelection()', () => {
  135. it( 'should not clear currently selected cells if not cell was passed', () => {
  136. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  137. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  138. tableSelection.stopSelection();
  139. assertSelectedCells( [
  140. [ 1, 1, 0 ],
  141. [ 0, 0, 0 ],
  142. [ 0, 0, 0 ]
  143. ] );
  144. } );
  145. it( 'should change model selection if cell was passed', () => {
  146. const spy = sinon.spy();
  147. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  148. model.document.selection.on( 'change', spy );
  149. tableSelection.stopSelection( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  150. sinon.assert.calledOnce( spy );
  151. } );
  152. it( 'should extend selection to passed table cell', () => {
  153. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  154. tableSelection.stopSelection( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  155. assertSelectedCells( [
  156. [ 1, 1, 0 ],
  157. [ 0, 0, 0 ],
  158. [ 0, 0, 0 ]
  159. ] );
  160. } );
  161. } );
  162. describe( 'clearSelection()', () => {
  163. it( 'should not change model selection', () => {
  164. const spy = sinon.spy();
  165. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  166. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  167. model.document.selection.on( 'change', spy );
  168. tableSelection.clearSelection();
  169. sinon.assert.notCalled( spy );
  170. } );
  171. it( 'should not reset model selections', () => {
  172. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  173. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  174. tableSelection.clearSelection();
  175. assertSelectedCells( [
  176. [ 1, 1, 0 ],
  177. [ 0, 0, 0 ],
  178. [ 0, 0, 0 ]
  179. ] );
  180. } );
  181. } );
  182. describe( '* getSelectedTableCells()', () => {
  183. it( 'should return nothing if selection is not started', () => {
  184. expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [] );
  185. } );
  186. it( 'should return two table cells', () => {
  187. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  188. const lastCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  189. tableSelection.startSelectingFrom( firstCell );
  190. tableSelection.setSelectingTo( lastCell );
  191. expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
  192. firstCell, lastCell
  193. ] );
  194. } );
  195. it( 'should return four table cells for diagonal selection', () => {
  196. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  197. const lastCell = modelRoot.getNodeByPath( [ 0, 1, 1 ] );
  198. tableSelection.startSelectingFrom( firstCell );
  199. tableSelection.setSelectingTo( lastCell );
  200. expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
  201. firstCell, modelRoot.getNodeByPath( [ 0, 0, 1 ] ), modelRoot.getNodeByPath( [ 0, 1, 0 ] ), lastCell
  202. ] );
  203. } );
  204. it( 'should return row table cells', () => {
  205. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  206. const lastCell = modelRoot.getNodeByPath( [ 0, 0, 2 ] );
  207. tableSelection.startSelectingFrom( firstCell );
  208. tableSelection.setSelectingTo( lastCell );
  209. expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
  210. firstCell, modelRoot.getNodeByPath( [ 0, 0, 1 ] ), lastCell
  211. ] );
  212. } );
  213. it( 'should return column table cells', () => {
  214. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  215. const lastCell = modelRoot.getNodeByPath( [ 0, 2, 1 ] );
  216. tableSelection.startSelectingFrom( firstCell );
  217. tableSelection.setSelectingTo( lastCell );
  218. expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
  219. firstCell, modelRoot.getNodeByPath( [ 0, 1, 1 ] ), lastCell
  220. ] );
  221. } );
  222. } );
  223. } );
  224. // Helper method for asserting selected table cells.
  225. //
  226. // To check if a table has expected cells selected pass two dimensional array of truthy and falsy values:
  227. //
  228. // assertSelectedCells( [
  229. // [ 0, 1 ],
  230. // [ 0, 1 ]
  231. // ] );
  232. //
  233. // The above call will check if table has second column selected (assuming no spans).
  234. //
  235. // **Note**: This function operates on child indexes - not rows/columns.
  236. function assertSelectedCells( tableMap ) {
  237. const tableIndex = 0;
  238. for ( let rowIndex = 0; rowIndex < tableMap.length; rowIndex++ ) {
  239. const row = tableMap[ rowIndex ];
  240. for ( let cellIndex = 0; cellIndex < row.length; cellIndex++ ) {
  241. const expectSelected = row[ cellIndex ];
  242. if ( expectSelected ) {
  243. assertNodeIsSelected( [ tableIndex, rowIndex, cellIndex ] );
  244. } else {
  245. assertNodeIsNotSelected( [ tableIndex, rowIndex, cellIndex ] );
  246. }
  247. }
  248. }
  249. }
  250. function assertNodeIsSelected( path ) {
  251. const node = modelRoot.getNodeByPath( path );
  252. const selectionRanges = Array.from( model.document.selection.getRanges() );
  253. expect( selectionRanges.some( range => range.containsItem( node ) ), `Expected node [${ path }] to be selected` ).to.be.true;
  254. }
  255. function assertNodeIsNotSelected( path ) {
  256. const node = modelRoot.getNodeByPath( path );
  257. const selectionRanges = Array.from( model.document.selection.getRanges() );
  258. expect( selectionRanges.every( range => !range.containsItem( node ) ), `Expected node [${ path }] to be not selected` ).to.be.true;
  259. }
  260. } );