tableselection.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. describe( 'TableSelection', () => {
  10. let editor, model, root, tableSelection;
  11. beforeEach( () => {
  12. return VirtualTestEditor.create( {
  13. plugins: [ TableSelection ]
  14. } ).then( newEditor => {
  15. editor = newEditor;
  16. model = editor.model;
  17. root = model.document.getRoot( 'main' );
  18. tableSelection = editor.plugins.get( TableSelection );
  19. defaultSchema( model.schema );
  20. defaultConversion( editor.conversion );
  21. } );
  22. } );
  23. afterEach( () => {
  24. return editor.destroy();
  25. } );
  26. describe( '#pluginName', () => {
  27. it( 'should provide plugin name', () => {
  28. expect( TableSelection.pluginName ).to.equal( 'TableSelection' );
  29. } );
  30. } );
  31. describe( 'start()', () => {
  32. it( 'should start selection', () => {
  33. setData( model, modelTable( [
  34. [ '00[]', '01' ],
  35. [ '10', '11' ]
  36. ] ) );
  37. const nodeByPath = root.getNodeByPath( [ 0, 0, 0 ] );
  38. tableSelection.startSelection( nodeByPath );
  39. expect( tableSelection.isSelecting ).to.be.true;
  40. } );
  41. it( 'update selection to single table cell', () => {
  42. setData( model, modelTable( [
  43. [ '00[]', '01' ],
  44. [ '10', '11' ]
  45. ] ) );
  46. const nodeByPath = root.getNodeByPath( [ 0, 0, 0 ] );
  47. tableSelection.startSelection( nodeByPath );
  48. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ nodeByPath ] );
  49. } );
  50. } );
  51. describe( 'stop()', () => {
  52. it( 'should stop selection', () => {
  53. setData( model, modelTable( [
  54. [ '00[]', '01' ],
  55. [ '10', '11' ]
  56. ] ) );
  57. const nodeByPath = root.getNodeByPath( [ 0, 0, 0 ] );
  58. tableSelection.startSelection( nodeByPath );
  59. expect( tableSelection.isSelecting ).to.be.true;
  60. tableSelection.stopSelection( nodeByPath );
  61. expect( tableSelection.isSelecting ).to.be.false;
  62. } );
  63. it( 'update selection to passed table cell', () => {
  64. setData( model, modelTable( [
  65. [ '00[]', '01' ],
  66. [ '10', '11' ]
  67. ] ) );
  68. const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
  69. const endNode = root.getNodeByPath( [ 0, 1, 1 ] );
  70. tableSelection.startSelection( startNode );
  71. tableSelection.stopSelection( endNode );
  72. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [
  73. startNode,
  74. root.getNodeByPath( [ 0, 0, 1 ] ),
  75. root.getNodeByPath( [ 0, 1, 0 ] ),
  76. endNode
  77. ] );
  78. } );
  79. it( 'should not update selection if alredy stopped', () => {
  80. setData( model, modelTable( [
  81. [ '00[]', '01', '02' ],
  82. [ '10', '11', '12' ]
  83. ] ) );
  84. const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
  85. const firstEndNode = root.getNodeByPath( [ 0, 0, 1 ] );
  86. tableSelection.startSelection( startNode );
  87. tableSelection.stopSelection( firstEndNode );
  88. expect( tableSelection.isSelecting ).to.be.false;
  89. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
  90. const secondEndNode = root.getNodeByPath( [ 0, 0, 2 ] );
  91. tableSelection.stopSelection( secondEndNode );
  92. expect( tableSelection.isSelecting ).to.be.false;
  93. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
  94. } );
  95. it( 'should not update selection if table cell is from another parent', () => {
  96. setData( model, modelTable( [
  97. [ '00[]', '01' ]
  98. ] ) + modelTable( [
  99. [ 'aa', 'bb' ]
  100. ] ) );
  101. tableSelection.startSelection( root.getNodeByPath( [ 0, 0, 0 ] ) );
  102. tableSelection.stopSelection( root.getNodeByPath( [ 1, 0, 1 ] ) );
  103. expect( tableSelection.isSelecting ).to.be.false;
  104. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ root.getNodeByPath( [ 0, 0, 0 ] ) ] );
  105. } );
  106. } );
  107. describe( 'update()', () => {
  108. it( 'should update selection', () => {
  109. setData( model, modelTable( [
  110. [ '00[]', '01', '02' ],
  111. [ '10', '11', '12' ]
  112. ] ) );
  113. const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
  114. const firstEndNode = root.getNodeByPath( [ 0, 0, 1 ] );
  115. tableSelection.startSelection( startNode );
  116. tableSelection.updateSelection( firstEndNode );
  117. expect( tableSelection.isSelecting ).to.be.true;
  118. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
  119. const secondEndNode = root.getNodeByPath( [ 0, 0, 2 ] );
  120. tableSelection.updateSelection( secondEndNode );
  121. expect( tableSelection.isSelecting ).to.be.true;
  122. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode, secondEndNode ] );
  123. } );
  124. it( 'should not update selection if stopped', () => {
  125. setData( model, modelTable( [
  126. [ '00[]', '01', '02' ],
  127. [ '10', '11', '12' ]
  128. ] ) );
  129. const startNode = root.getNodeByPath( [ 0, 0, 0 ] );
  130. const firstEndNode = root.getNodeByPath( [ 0, 0, 1 ] );
  131. tableSelection.startSelection( startNode );
  132. tableSelection.updateSelection( firstEndNode );
  133. expect( tableSelection.isSelecting ).to.be.true;
  134. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
  135. tableSelection.stopSelection( firstEndNode );
  136. expect( tableSelection.isSelecting ).to.be.false;
  137. const secondEndNode = root.getNodeByPath( [ 0, 0, 2 ] );
  138. tableSelection.updateSelection( secondEndNode );
  139. expect( tableSelection.isSelecting ).to.be.false;
  140. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ startNode, firstEndNode ] );
  141. } );
  142. it( 'should not update selection if table cell is from another parent', () => {
  143. setData( model, modelTable( [
  144. [ '00[]', '01' ]
  145. ] ) + modelTable( [
  146. [ 'aa', 'bb' ]
  147. ] ) );
  148. tableSelection.startSelection( root.getNodeByPath( [ 0, 0, 0 ] ) );
  149. tableSelection.updateSelection( root.getNodeByPath( [ 1, 0, 1 ] ) );
  150. expect( tableSelection.isSelecting ).to.be.true;
  151. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [ root.getNodeByPath( [ 0, 0, 0 ] ) ] );
  152. } );
  153. } );
  154. describe( 'getSelection()', () => {
  155. it( 'should return empty array if not started', () => {
  156. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [] );
  157. } );
  158. it( 'should return block of selected nodes', () => {
  159. setData( model, modelTable( [
  160. [ '00[]', '01', '02', '03' ],
  161. [ '10', '11', '12', '13' ],
  162. [ '20', '21', '22', '23' ],
  163. [ '30', '31', '32', '33' ]
  164. ] ) );
  165. tableSelection.startSelection( root.getNodeByPath( [ 0, 1, 1 ] ) );
  166. tableSelection.updateSelection( root.getNodeByPath( [ 0, 2, 2 ] ) );
  167. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [
  168. root.getNodeByPath( [ 0, 1, 1 ] ),
  169. root.getNodeByPath( [ 0, 1, 2 ] ),
  170. root.getNodeByPath( [ 0, 2, 1 ] ),
  171. root.getNodeByPath( [ 0, 2, 2 ] )
  172. ] );
  173. } );
  174. it( 'should return block of selected nodes (inverted selection)', () => {
  175. setData( model, modelTable( [
  176. [ '00[]', '01', '02', '03' ],
  177. [ '10', '11', '12', '13' ],
  178. [ '20', '21', '22', '23' ],
  179. [ '30', '31', '32', '33' ]
  180. ] ) );
  181. tableSelection.startSelection( root.getNodeByPath( [ 0, 2, 2 ] ) );
  182. tableSelection.updateSelection( root.getNodeByPath( [ 0, 1, 1 ] ) );
  183. expect( Array.from( tableSelection.getSelection() ) ).to.deep.equal( [
  184. root.getNodeByPath( [ 0, 1, 1 ] ),
  185. root.getNodeByPath( [ 0, 1, 2 ] ),
  186. root.getNodeByPath( [ 0, 2, 1 ] ),
  187. root.getNodeByPath( [ 0, 2, 2 ] )
  188. ] );
  189. } );
  190. } );
  191. } );