tableselection.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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, stringify as stringifyModel } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import TableEditing from '../src/tableediting';
  9. import TableSelection from '../src/tableselection';
  10. // import { assertSelectedCells, modelTable, viewTable } from './_utils/utils';
  11. import { modelTable } from './_utils/utils';
  12. // import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  13. // import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  14. import DocumentFragment from '@ckeditor/ckeditor5-engine/src/model/documentfragment';
  15. describe( 'table selection', () => {
  16. let editor, model, tableSelection, modelRoot;
  17. beforeEach( async () => {
  18. editor = await VirtualTestEditor.create( {
  19. plugins: [ TableEditing, TableSelection, Paragraph ]
  20. } );
  21. model = editor.model;
  22. modelRoot = model.document.getRoot();
  23. tableSelection = editor.plugins.get( TableSelection );
  24. setModelData( model, modelTable( [
  25. [ '11[]', '12', '13' ],
  26. [ '21', '22', '23' ],
  27. [ '31', '32', '33' ]
  28. ] ) );
  29. } );
  30. afterEach( async () => {
  31. await editor.destroy();
  32. } );
  33. describe( 'init()', () => {
  34. describe( 'plugin disabling support', () => {
  35. let plugin;
  36. beforeEach( () => {
  37. plugin = editor.plugins.get( TableSelection );
  38. } );
  39. it( 'should collapse multi-cell selection when the plugin gets disabled', () => {
  40. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  41. const lastCell = modelRoot.getNodeByPath( [ 0, 1, 1 ] );
  42. tableSelection._setCellSelection(
  43. firstCell,
  44. lastCell
  45. );
  46. plugin.forceDisabled( 'foo' );
  47. const ranges = [ ...model.document.selection.getRanges() ];
  48. expect( ranges ).to.have.length( 1 );
  49. expect( ranges[ 0 ].isCollapsed ).to.be.true;
  50. expect( ranges[ 0 ].start.path ).to.deep.equal( [ 0, 0, 0, 0, 0 ] );
  51. } );
  52. it( 'should do nothing if there were no multi-cell selections', () => {
  53. plugin.forceDisabled( 'foo' );
  54. const ranges = [ ...model.document.selection.getRanges() ];
  55. expect( ranges ).to.have.length( 1 );
  56. expect( ranges[ 0 ].isCollapsed ).to.be.true;
  57. expect( ranges[ 0 ].start.path ).to.deep.equal( [ 0, 0, 0, 0, 2 ] );
  58. } );
  59. } );
  60. } );
  61. describe( 'selection by shift+click', () => {
  62. it( 'should...', () => {
  63. // tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  64. // tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  65. // tableSelection.stopSelection();
  66. // assertSelectedCells( model, [
  67. // [ 1, 1, 0 ],
  68. // [ 0, 0, 0 ],
  69. // [ 0, 0, 0 ]
  70. // ] );
  71. } );
  72. } );
  73. describe( 'selection by mouse drag', () => {
  74. it( 'should...', () => {
  75. // tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  76. // tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  77. // tableSelection.stopSelection();
  78. // assertSelectedCells( model, [
  79. // [ 1, 1, 0 ],
  80. // [ 0, 0, 0 ],
  81. // [ 0, 0, 0 ]
  82. // ] );
  83. } );
  84. } );
  85. describe( 'getSelectedTableCells()', () => {
  86. it( 'should return nothing if selection is not started', () => {
  87. expect( tableSelection.getSelectedTableCells() ).to.be.null;
  88. } );
  89. it( 'should return two table cells', () => {
  90. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  91. const lastCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  92. tableSelection._setCellSelection(
  93. firstCell,
  94. lastCell
  95. );
  96. expect( tableSelection.getSelectedTableCells() ).to.deep.equal( [
  97. firstCell, lastCell
  98. ] );
  99. } );
  100. it( 'should return four table cells for diagonal selection', () => {
  101. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  102. const lastCell = modelRoot.getNodeByPath( [ 0, 1, 1 ] );
  103. tableSelection._setCellSelection(
  104. firstCell,
  105. lastCell
  106. );
  107. expect( tableSelection.getSelectedTableCells() ).to.deep.equal( [
  108. firstCell, modelRoot.getNodeByPath( [ 0, 0, 1 ] ), modelRoot.getNodeByPath( [ 0, 1, 0 ] ), lastCell
  109. ] );
  110. } );
  111. it( 'should return row table cells', () => {
  112. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  113. const lastCell = modelRoot.getNodeByPath( [ 0, 0, 2 ] );
  114. tableSelection._setCellSelection(
  115. firstCell,
  116. lastCell
  117. );
  118. expect( tableSelection.getSelectedTableCells() ).to.deep.equal( [
  119. firstCell, modelRoot.getNodeByPath( [ 0, 0, 1 ] ), lastCell
  120. ] );
  121. } );
  122. it( 'should return column table cells', () => {
  123. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  124. const lastCell = modelRoot.getNodeByPath( [ 0, 2, 1 ] );
  125. tableSelection._setCellSelection( firstCell, lastCell );
  126. expect( tableSelection.getSelectedTableCells() ).to.deep.equal( [
  127. firstCell, modelRoot.getNodeByPath( [ 0, 1, 1 ] ), lastCell
  128. ] );
  129. } );
  130. it( 'should return cells in source order despite backward selection', () => {
  131. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 2 ] );
  132. const lastCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  133. tableSelection._setCellSelection( firstCell, lastCell );
  134. expect( tableSelection.getSelectedTableCells() ).to.deep.equal( [
  135. lastCell, firstCell
  136. ] );
  137. } );
  138. } );
  139. describe( 'getSelectionAsFragment()', () => {
  140. it( 'should return undefined if no table cells are selected', () => {
  141. expect( tableSelection.getSelectionAsFragment() ).to.be.null;
  142. } );
  143. it( 'should return document fragment for selected table cells', () => {
  144. tableSelection._setCellSelection(
  145. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  146. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  147. );
  148. expect( tableSelection.getSelectionAsFragment() ).to.be.instanceOf( DocumentFragment );
  149. } );
  150. it( 'should return cells in the source order in case of forward selection', () => {
  151. tableSelection._setCellSelection(
  152. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  153. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  154. );
  155. expect( stringifyModel( tableSelection.getSelectionAsFragment() ) ).to.equal( modelTable( [
  156. [ '11', '12' ],
  157. [ '21', '22' ]
  158. ] ) );
  159. } );
  160. it( 'should return cells in the source order in case of backward selection', () => {
  161. tableSelection._setCellSelection(
  162. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  163. modelRoot.getNodeByPath( [ 0, 0, 0 ] )
  164. );
  165. expect( editor.model.document.selection.isBackward ).to.be.true;
  166. expect( stringifyModel( tableSelection.getSelectionAsFragment() ) ).to.equal( modelTable( [
  167. [ '11', '12' ],
  168. [ '21', '22' ]
  169. ] ) );
  170. } );
  171. } );
  172. } );