8
0

tableselection.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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( 'selection by shift+click', () => {
  34. it( 'should...', () => {
  35. // tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  36. // tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  37. // tableSelection.stopSelection();
  38. // assertSelectedCells( model, [
  39. // [ 1, 1, 0 ],
  40. // [ 0, 0, 0 ],
  41. // [ 0, 0, 0 ]
  42. // ] );
  43. } );
  44. } );
  45. describe( 'selection by mouse drag', () => {
  46. it( 'should...', () => {
  47. // tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  48. // tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  49. // tableSelection.stopSelection();
  50. // assertSelectedCells( model, [
  51. // [ 1, 1, 0 ],
  52. // [ 0, 0, 0 ],
  53. // [ 0, 0, 0 ]
  54. // ] );
  55. } );
  56. } );
  57. describe( 'getSelectedTableCells()', () => {
  58. it( 'should return nothing if selection is not started', () => {
  59. expect( tableSelection.getSelectedTableCells() ).to.be.null;
  60. } );
  61. it( 'should return two table cells', () => {
  62. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  63. const lastCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  64. tableSelection._setCellSelection(
  65. firstCell,
  66. lastCell
  67. );
  68. expect( tableSelection.getSelectedTableCells() ).to.deep.equal( [
  69. firstCell, lastCell
  70. ] );
  71. } );
  72. it( 'should return four table cells for diagonal selection', () => {
  73. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  74. const lastCell = modelRoot.getNodeByPath( [ 0, 1, 1 ] );
  75. tableSelection._setCellSelection(
  76. firstCell,
  77. lastCell
  78. );
  79. expect( tableSelection.getSelectedTableCells() ).to.deep.equal( [
  80. firstCell, modelRoot.getNodeByPath( [ 0, 0, 1 ] ), modelRoot.getNodeByPath( [ 0, 1, 0 ] ), lastCell
  81. ] );
  82. } );
  83. it( 'should return row table cells', () => {
  84. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  85. const lastCell = modelRoot.getNodeByPath( [ 0, 0, 2 ] );
  86. tableSelection._setCellSelection(
  87. firstCell,
  88. lastCell
  89. );
  90. expect( tableSelection.getSelectedTableCells() ).to.deep.equal( [
  91. firstCell, modelRoot.getNodeByPath( [ 0, 0, 1 ] ), lastCell
  92. ] );
  93. } );
  94. it( 'should return column table cells', () => {
  95. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  96. const lastCell = modelRoot.getNodeByPath( [ 0, 2, 1 ] );
  97. tableSelection._setCellSelection( firstCell, lastCell );
  98. expect( tableSelection.getSelectedTableCells() ).to.deep.equal( [
  99. firstCell, modelRoot.getNodeByPath( [ 0, 1, 1 ] ), lastCell
  100. ] );
  101. } );
  102. it( 'should return cells in source order despite backward selection', () => {
  103. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 2 ] );
  104. const lastCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  105. tableSelection._setCellSelection( firstCell, lastCell );
  106. expect( tableSelection.getSelectedTableCells() ).to.deep.equal( [
  107. lastCell, firstCell
  108. ] );
  109. } );
  110. } );
  111. describe( 'getSelectionAsFragment()', () => {
  112. it( 'should return undefined if no table cells are selected', () => {
  113. expect( tableSelection.getSelectionAsFragment() ).to.be.null;
  114. } );
  115. it( 'should return document fragment for selected table cells', () => {
  116. tableSelection._setCellSelection(
  117. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  118. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  119. );
  120. expect( tableSelection.getSelectionAsFragment() ).to.be.instanceOf( DocumentFragment );
  121. } );
  122. it( 'should return cells in the source order in case of forward selection', () => {
  123. tableSelection._setCellSelection(
  124. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  125. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  126. );
  127. expect( stringifyModel( tableSelection.getSelectionAsFragment() ) ).to.equal( modelTable( [
  128. [ '11', '12' ],
  129. [ '21', '22' ]
  130. ] ) );
  131. } );
  132. it( 'should return cells in the source order in case of backward selection', () => {
  133. tableSelection._setCellSelection(
  134. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  135. modelRoot.getNodeByPath( [ 0, 0, 0 ] )
  136. );
  137. expect( editor.model.document.selection.isBackward ).to.be.true;
  138. expect( stringifyModel( tableSelection.getSelectionAsFragment() ) ).to.equal( modelTable( [
  139. [ '11', '12' ],
  140. [ '21', '22' ]
  141. ] ) );
  142. } );
  143. } );
  144. } );