tableselection.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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( 'TableSelection', () => {
  34. describe( 'selection by shift+click', () => {
  35. it( 'should...', () => {
  36. // tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  37. // tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  38. // tableSelection.stopSelection();
  39. // assertSelectedCells( model, [
  40. // [ 1, 1, 0 ],
  41. // [ 0, 0, 0 ],
  42. // [ 0, 0, 0 ]
  43. // ] );
  44. } );
  45. } );
  46. describe( 'selection by mouse drag', () => {
  47. it( 'should...', () => {
  48. // tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  49. // tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  50. // tableSelection.stopSelection();
  51. // assertSelectedCells( model, [
  52. // [ 1, 1, 0 ],
  53. // [ 0, 0, 0 ],
  54. // [ 0, 0, 0 ]
  55. // ] );
  56. } );
  57. } );
  58. describe( 'getSelectedTableCells()', () => {
  59. it( 'should return nothing if selection is not started', () => {
  60. expect( tableSelection.getSelectedTableCells() ).to.be.null;
  61. } );
  62. it( 'should return two table cells', () => {
  63. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  64. const lastCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  65. tableSelection._setCellSelection(
  66. firstCell,
  67. lastCell
  68. );
  69. expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
  70. firstCell, lastCell
  71. ] );
  72. } );
  73. it( 'should return four table cells for diagonal selection', () => {
  74. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  75. const lastCell = modelRoot.getNodeByPath( [ 0, 1, 1 ] );
  76. tableSelection._setCellSelection(
  77. firstCell,
  78. lastCell
  79. );
  80. expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
  81. firstCell, modelRoot.getNodeByPath( [ 0, 0, 1 ] ), modelRoot.getNodeByPath( [ 0, 1, 0 ] ), lastCell
  82. ] );
  83. } );
  84. it( 'should return row table cells', () => {
  85. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  86. const lastCell = modelRoot.getNodeByPath( [ 0, 0, 2 ] );
  87. tableSelection._setCellSelection(
  88. firstCell,
  89. lastCell
  90. );
  91. expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
  92. firstCell, modelRoot.getNodeByPath( [ 0, 0, 1 ] ), lastCell
  93. ] );
  94. } );
  95. it( 'should return column table cells', () => {
  96. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  97. const lastCell = modelRoot.getNodeByPath( [ 0, 2, 1 ] );
  98. tableSelection._setCellSelection( firstCell, lastCell );
  99. expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
  100. firstCell, modelRoot.getNodeByPath( [ 0, 1, 1 ] ), lastCell
  101. ] );
  102. } );
  103. } );
  104. describe( 'getSelectionAsFragment()', () => {
  105. it( 'should return undefined if no table cells are selected', () => {
  106. expect( tableSelection.getSelectionAsFragment() ).to.be.null;
  107. } );
  108. it( 'should return document fragment for selected table cells', () => {
  109. tableSelection._setCellSelection(
  110. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  111. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  112. );
  113. expect( tableSelection.getSelectionAsFragment() ).to.be.instanceOf( DocumentFragment );
  114. } );
  115. } );
  116. } );
  117. } );