tableselection.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 } 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( Array.from( 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( Array.from( 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( Array.from( 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( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
  99. firstCell, modelRoot.getNodeByPath( [ 0, 1, 1 ] ), lastCell
  100. ] );
  101. } );
  102. } );
  103. describe( 'getSelectionAsFragment()', () => {
  104. it( 'should return undefined if no table cells are selected', () => {
  105. expect( tableSelection.getSelectionAsFragment() ).to.be.null;
  106. } );
  107. it( 'should return document fragment for selected table cells', () => {
  108. tableSelection._setCellSelection(
  109. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  110. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  111. );
  112. expect( tableSelection.getSelectionAsFragment() ).to.be.instanceOf( DocumentFragment );
  113. } );
  114. } );
  115. } );