tableselection.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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, modelTable } from './_utils/utils';
  8. import TableSelection from '../src/tableselection';
  9. import { getData as getViewData } from '../../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. it( 'should set view selection', () => {
  52. setData( model, modelTable( [
  53. [ '00[]', '01', '02' ],
  54. [ '10', '11', '12' ]
  55. ] ) );
  56. tableSelection.startSelection( root.getNodeByPath( [ 0, 0, 0 ] ) );
  57. expect( getViewData( editor.editing.view ) ).to.equal(
  58. '<figure class="table">' +
  59. '<table>' +
  60. '<tbody>' +
  61. '<tr>' +
  62. '[<td>00</td>]<td>01</td><td>02</td>' +
  63. '</tr>' +
  64. '<tr>' +
  65. '<td>10</td><td>11</td><td>12</td>' +
  66. '</tr>' +
  67. '</tbody>' +
  68. '</table>' +
  69. '</figure>'
  70. );
  71. } );
  72. } );
  73. describe( 'stop()', () => {
  74. it( 'should stop selection', () => {
  75. setData( model, modelTable( [
  76. [ '00[]', '01' ],
  77. [ '10', '11' ]
  78. ] ) );
  79. const nodeByPath = root.getNodeByPath( [ 0, 0, 0 ] );
  80. tableSelection.startSelection( nodeByPath );
  81. expect( tableSelection.isSelecting ).to.be.true;
  82. tableSelection.stopSelection( nodeByPath );
  83. expect( tableSelection.isSelecting ).to.be.false;
  84. } );
  85. it( 'update selection to passed table cell', () => {
  86. setData( model, modelTable( [
  87. [ '00[]', '01' ],
  88. [ '10', '11' ]
  89. ] ) );
  90. const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
  91. const endNode = root.getNodeByPath( [ 0, 1, 1 ] );
  92. tableSelection.startSelection( startNode );
  93. tableSelection.stopSelection( endNode );
  94. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [
  95. startNode,
  96. root.getNodeByPath( [ 0, 0, 1 ] ),
  97. root.getNodeByPath( [ 0, 1, 0 ] ),
  98. endNode
  99. ] );
  100. } );
  101. it( 'should not update selection if alredy stopped', () => {
  102. setData( model, modelTable( [
  103. [ '00[]', '01', '02' ],
  104. [ '10', '11', '12' ]
  105. ] ) );
  106. const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
  107. const firstEndNode = root.getNodeByPath( [ 0, 0, 1 ] );
  108. tableSelection.startSelection( startNode );
  109. tableSelection.stopSelection( firstEndNode );
  110. expect( tableSelection.isSelecting ).to.be.false;
  111. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
  112. const secondEndNode = root.getNodeByPath( [ 0, 0, 2 ] );
  113. tableSelection.stopSelection( secondEndNode );
  114. expect( tableSelection.isSelecting ).to.be.false;
  115. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
  116. } );
  117. it( 'should not update selection if table cell is from another parent', () => {
  118. setData( model, modelTable( [
  119. [ '00[]', '01' ]
  120. ] ) + modelTable( [
  121. [ 'aa', 'bb' ]
  122. ] ) );
  123. tableSelection.startSelection( root.getNodeByPath( [ 0, 0, 0 ] ) );
  124. tableSelection.stopSelection( root.getNodeByPath( [ 1, 0, 1 ] ) );
  125. expect( tableSelection.isSelecting ).to.be.false;
  126. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ root.getNodeByPath( [ 0, 0, 0 ] ) ] );
  127. } );
  128. it( 'should update view selection', () => {
  129. setData( model, modelTable( [
  130. [ '00[]', '01', '02' ],
  131. [ '10', '11', '12' ]
  132. ] ) );
  133. const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
  134. const firstEndNode = root.getNodeByPath( [ 0, 0, 1 ] );
  135. tableSelection.startSelection( startNode );
  136. tableSelection.stopSelection( firstEndNode );
  137. expect( getViewData( editor.editing.view ) ).to.equal(
  138. '<figure class="table">' +
  139. '<table>' +
  140. '<tbody>' +
  141. '<tr>' +
  142. '[<td>00</td>][<td>01</td>]<td>02</td>' +
  143. '</tr>' +
  144. '<tr>' +
  145. '<td>10</td><td>11</td><td>12</td>' +
  146. '</tr>' +
  147. '</tbody>' +
  148. '</table>' +
  149. '</figure>'
  150. );
  151. } );
  152. } );
  153. describe( 'update()', () => {
  154. it( 'should update 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( tableSelection.isSelecting ).to.be.true;
  164. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
  165. const secondEndNode = root.getNodeByPath( [ 0, 0, 2 ] );
  166. tableSelection.updateSelection( secondEndNode );
  167. expect( tableSelection.isSelecting ).to.be.true;
  168. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode, secondEndNode ] );
  169. } );
  170. it( 'should not update selection if stopped', () => {
  171. setData( model, modelTable( [
  172. [ '00[]', '01', '02' ],
  173. [ '10', '11', '12' ]
  174. ] ) );
  175. const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
  176. const firstEndNode = root.getNodeByPath( [ 0, 0, 1 ] );
  177. tableSelection.startSelection( startNode );
  178. tableSelection.updateSelection( firstEndNode );
  179. expect( tableSelection.isSelecting ).to.be.true;
  180. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
  181. tableSelection.stopSelection( firstEndNode );
  182. expect( tableSelection.isSelecting ).to.be.false;
  183. const secondEndNode = root.getNodeByPath( [ 0, 0, 2 ] );
  184. tableSelection.updateSelection( secondEndNode );
  185. expect( tableSelection.isSelecting ).to.be.false;
  186. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
  187. } );
  188. it( 'should not update selection if table cell is from another parent', () => {
  189. setData( model, modelTable( [
  190. [ '00[]', '01' ]
  191. ] ) + modelTable( [
  192. [ 'aa', 'bb' ]
  193. ] ) );
  194. tableSelection.startSelection( root.getNodeByPath( [ 0, 0, 0 ] ) );
  195. tableSelection.updateSelection( root.getNodeByPath( [ 1, 0, 1 ] ) );
  196. expect( tableSelection.isSelecting ).to.be.true;
  197. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ root.getNodeByPath( [ 0, 0, 0 ] ) ] );
  198. } );
  199. it( 'should update view selection', () => {
  200. setData( model, modelTable( [
  201. [ '00[]', '01', '02' ],
  202. [ '10', '11', '12' ]
  203. ] ) );
  204. const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
  205. const firstEndNode = root.getNodeByPath( [ 0, 0, 1 ] );
  206. tableSelection.startSelection( startNode );
  207. tableSelection.updateSelection( firstEndNode );
  208. expect( getViewData( editor.editing.view ) ).to.equal(
  209. '<figure class="table">' +
  210. '<table>' +
  211. '<tbody>' +
  212. '<tr>' +
  213. '[<td>00</td>][<td>01</td>]<td>02</td>' +
  214. '</tr>' +
  215. '<tr>' +
  216. '<td>10</td><td>11</td><td>12</td>' +
  217. '</tr>' +
  218. '</tbody>' +
  219. '</table>' +
  220. '</figure>'
  221. );
  222. } );
  223. } );
  224. describe( 'getSelection()', () => {
  225. it( 'should return empty array if not started', () => {
  226. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [] );
  227. } );
  228. it( 'should return block of selected nodes', () => {
  229. setData( model, modelTable( [
  230. [ '00[]', '01', '02', '03' ],
  231. [ '10', '11', '12', '13' ],
  232. [ '20', '21', '22', '23' ],
  233. [ '30', '31', '32', '33' ]
  234. ] ) );
  235. tableSelection.startSelection( root.getNodeByPath( [ 0, 1, 1 ] ) );
  236. tableSelection.updateSelection( root.getNodeByPath( [ 0, 2, 2 ] ) );
  237. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [
  238. root.getNodeByPath( [ 0, 1, 1 ] ),
  239. root.getNodeByPath( [ 0, 1, 2 ] ),
  240. root.getNodeByPath( [ 0, 2, 1 ] ),
  241. root.getNodeByPath( [ 0, 2, 2 ] )
  242. ] );
  243. } );
  244. it( 'should return block of selected nodes (inverted selection)', () => {
  245. setData( model, modelTable( [
  246. [ '00[]', '01', '02', '03' ],
  247. [ '10', '11', '12', '13' ],
  248. [ '20', '21', '22', '23' ],
  249. [ '30', '31', '32', '33' ]
  250. ] ) );
  251. tableSelection.startSelection( root.getNodeByPath( [ 0, 2, 2 ] ) );
  252. tableSelection.updateSelection( root.getNodeByPath( [ 0, 1, 1 ] ) );
  253. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [
  254. root.getNodeByPath( [ 0, 1, 1 ] ),
  255. root.getNodeByPath( [ 0, 1, 2 ] ),
  256. root.getNodeByPath( [ 0, 2, 1 ] ),
  257. root.getNodeByPath( [ 0, 2, 2 ] )
  258. ] );
  259. } );
  260. } );
  261. } );