tableselection.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import { defaultConversion, defaultSchema, formatTable, modelTable } from './_utils/utils';
  8. import TableSelection from '../src/tableselection';
  9. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  10. describe( 'TableSelection', () => {
  11. let editor, model, root, tableSelection;
  12. beforeEach( () => {
  13. return VirtualTestEditor.create( {
  14. plugins: [ TableSelection ]
  15. } ).then( newEditor => {
  16. editor = newEditor;
  17. model = editor.model;
  18. root = model.document.getRoot( 'main' );
  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( '#pluginName', () => {
  28. it( 'should provide plugin name', () => {
  29. expect( TableSelection.pluginName ).to.equal( 'TableSelection' );
  30. } );
  31. } );
  32. describe( 'start()', () => {
  33. it( 'should start selection', () => {
  34. setData( model, modelTable( [
  35. [ '00[]', '01' ],
  36. [ '10', '11' ]
  37. ] ) );
  38. const nodeByPath = root.getNodeByPath( [ 0, 0, 0 ] );
  39. tableSelection.startSelection( nodeByPath );
  40. expect( tableSelection.isSelecting ).to.be.true;
  41. } );
  42. it( 'update selection to single table cell', () => {
  43. setData( model, modelTable( [
  44. [ '00[]', '01' ],
  45. [ '10', '11' ]
  46. ] ) );
  47. const nodeByPath = root.getNodeByPath( [ 0, 0, 0 ] );
  48. tableSelection.startSelection( nodeByPath );
  49. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ nodeByPath ] );
  50. } );
  51. } );
  52. describe( 'stop()', () => {
  53. it( 'should stop selection', () => {
  54. setData( model, modelTable( [
  55. [ '00[]', '01' ],
  56. [ '10', '11' ]
  57. ] ) );
  58. const nodeByPath = root.getNodeByPath( [ 0, 0, 0 ] );
  59. tableSelection.startSelection( nodeByPath );
  60. expect( tableSelection.isSelecting ).to.be.true;
  61. tableSelection.stopSelection( nodeByPath );
  62. expect( tableSelection.isSelecting ).to.be.false;
  63. } );
  64. it( 'update selection to passed table cell', () => {
  65. setData( model, modelTable( [
  66. [ '00[]', '01' ],
  67. [ '10', '11' ]
  68. ] ) );
  69. const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
  70. const endNode = root.getNodeByPath( [ 0, 1, 1 ] );
  71. tableSelection.startSelection( startNode );
  72. tableSelection.stopSelection( endNode );
  73. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [
  74. startNode,
  75. root.getNodeByPath( [ 0, 0, 1 ] ),
  76. root.getNodeByPath( [ 0, 1, 0 ] ),
  77. endNode
  78. ] );
  79. } );
  80. it( 'should not update selection if alredy stopped', () => {
  81. setData( model, modelTable( [
  82. [ '00[]', '01', '02' ],
  83. [ '10', '11', '12' ]
  84. ] ) );
  85. const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
  86. const firstEndNode = root.getNodeByPath( [ 0, 0, 1 ] );
  87. tableSelection.startSelection( startNode );
  88. tableSelection.stopSelection( firstEndNode );
  89. expect( tableSelection.isSelecting ).to.be.false;
  90. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
  91. const secondEndNode = root.getNodeByPath( [ 0, 0, 2 ] );
  92. tableSelection.stopSelection( secondEndNode );
  93. expect( tableSelection.isSelecting ).to.be.false;
  94. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
  95. } );
  96. it( 'should not update selection if table cell is from another parent', () => {
  97. setData( model, modelTable( [
  98. [ '00[]', '01' ]
  99. ] ) + modelTable( [
  100. [ 'aa', 'bb' ]
  101. ] ) );
  102. tableSelection.startSelection( root.getNodeByPath( [ 0, 0, 0 ] ) );
  103. tableSelection.stopSelection( root.getNodeByPath( [ 1, 0, 1 ] ) );
  104. expect( tableSelection.isSelecting ).to.be.false;
  105. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ root.getNodeByPath( [ 0, 0, 0 ] ) ] );
  106. } );
  107. } );
  108. describe( 'update()', () => {
  109. it( 'should update selection', () => {
  110. setData( model, modelTable( [
  111. [ '00[]', '01', '02' ],
  112. [ '10', '11', '12' ]
  113. ] ) );
  114. const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
  115. const firstEndNode = root.getNodeByPath( [ 0, 0, 1 ] );
  116. tableSelection.startSelection( startNode );
  117. tableSelection.updateSelection( firstEndNode );
  118. expect( tableSelection.isSelecting ).to.be.true;
  119. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
  120. const secondEndNode = root.getNodeByPath( [ 0, 0, 2 ] );
  121. tableSelection.updateSelection( secondEndNode );
  122. expect( tableSelection.isSelecting ).to.be.true;
  123. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode, secondEndNode ] );
  124. } );
  125. it( 'should not update selection if stopped', () => {
  126. setData( model, modelTable( [
  127. [ '00[]', '01', '02' ],
  128. [ '10', '11', '12' ]
  129. ] ) );
  130. const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
  131. const firstEndNode = root.getNodeByPath( [ 0, 0, 1 ] );
  132. tableSelection.startSelection( startNode );
  133. tableSelection.updateSelection( firstEndNode );
  134. expect( tableSelection.isSelecting ).to.be.true;
  135. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
  136. tableSelection.stopSelection( firstEndNode );
  137. expect( tableSelection.isSelecting ).to.be.false;
  138. const secondEndNode = root.getNodeByPath( [ 0, 0, 2 ] );
  139. tableSelection.updateSelection( secondEndNode );
  140. expect( tableSelection.isSelecting ).to.be.false;
  141. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
  142. } );
  143. it( 'should not update selection if table cell is from another parent', () => {
  144. setData( model, modelTable( [
  145. [ '00[]', '01' ]
  146. ] ) + modelTable( [
  147. [ 'aa', 'bb' ]
  148. ] ) );
  149. tableSelection.startSelection( root.getNodeByPath( [ 0, 0, 0 ] ) );
  150. tableSelection.updateSelection( root.getNodeByPath( [ 1, 0, 1 ] ) );
  151. expect( tableSelection.isSelecting ).to.be.true;
  152. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ root.getNodeByPath( [ 0, 0, 0 ] ) ] );
  153. } );
  154. it( 'should update view selection', () => {
  155. setData( model, modelTable( [
  156. [ '00[]', '01', '02' ],
  157. [ '10', '11', '12' ]
  158. ] ) );
  159. const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
  160. const firstEndNode = root.getNodeByPath( [ 0, 0, 1 ] );
  161. tableSelection.startSelection( startNode );
  162. tableSelection.updateSelection( firstEndNode );
  163. expect( formatTable( getViewData( editor.editing.view ) ) ).to.equal( formatTable(
  164. '<figure class="table">' +
  165. '<table>' +
  166. '<tbody>' +
  167. '<tr>' +
  168. '[<td class="selected">00</td>][<td class="selected">01</td>]<td>02</td>' +
  169. '</tr>' +
  170. '<tr>' +
  171. '<td>10</td><td>11</td><td>12</td>' +
  172. '</tr>' +
  173. '</tbody>' +
  174. '</table>' +
  175. '</figure>'
  176. ) );
  177. } );
  178. } );
  179. describe( 'getSelection()', () => {
  180. it( 'should return empty array if not started', () => {
  181. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [] );
  182. } );
  183. it( 'should return block of selected nodes', () => {
  184. setData( model, modelTable( [
  185. [ '00[]', '01', '02', '03' ],
  186. [ '10', '11', '12', '13' ],
  187. [ '20', '21', '22', '23' ],
  188. [ '30', '31', '32', '33' ]
  189. ] ) );
  190. tableSelection.startSelection( root.getNodeByPath( [ 0, 1, 1 ] ) );
  191. tableSelection.updateSelection( root.getNodeByPath( [ 0, 2, 2 ] ) );
  192. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [
  193. root.getNodeByPath( [ 0, 1, 1 ] ),
  194. root.getNodeByPath( [ 0, 1, 2 ] ),
  195. root.getNodeByPath( [ 0, 2, 1 ] ),
  196. root.getNodeByPath( [ 0, 2, 2 ] )
  197. ] );
  198. } );
  199. it( 'should return block of selected nodes (inverted selection)', () => {
  200. setData( model, modelTable( [
  201. [ '00[]', '01', '02', '03' ],
  202. [ '10', '11', '12', '13' ],
  203. [ '20', '21', '22', '23' ],
  204. [ '30', '31', '32', '33' ]
  205. ] ) );
  206. tableSelection.startSelection( root.getNodeByPath( [ 0, 2, 2 ] ) );
  207. tableSelection.updateSelection( root.getNodeByPath( [ 0, 1, 1 ] ) );
  208. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [
  209. root.getNodeByPath( [ 0, 1, 1 ] ),
  210. root.getNodeByPath( [ 0, 1, 2 ] ),
  211. root.getNodeByPath( [ 0, 2, 1 ] ),
  212. root.getNodeByPath( [ 0, 2, 2 ] )
  213. ] );
  214. } );
  215. } );
  216. } );