tableselection-integration.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. /* globals document */
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import { getData as getModelData, 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 { modelTable } from './_utils/utils';
  11. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  12. import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';
  13. import Delete from '@ckeditor/ckeditor5-typing/src/delete';
  14. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  15. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  16. import Input from '@ckeditor/ckeditor5-typing/src/input';
  17. import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
  18. describe( 'table selection', () => {
  19. let editor, model, tableSelection, modelRoot, element, viewDocument;
  20. describe( 'TableSelection - input integration', () => {
  21. afterEach( async () => {
  22. element.remove();
  23. await editor.destroy();
  24. } );
  25. describe( 'on delete', () => {
  26. beforeEach( async () => {
  27. await setupEditor( [ Delete ] );
  28. } );
  29. it( 'should clear contents of the selected table cells and put selection in last cell on backward delete', () => {
  30. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  31. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  32. const domEventData = new DomEventData( viewDocument, {
  33. preventDefault: sinon.spy()
  34. }, {
  35. direction: 'backward',
  36. unit: 'character',
  37. sequence: 1
  38. } );
  39. viewDocument.fire( 'delete', domEventData );
  40. assertEqualMarkup( getModelData( model ), modelTable( [
  41. [ '', '', '13' ],
  42. [ '', '[]', '23' ],
  43. [ '31', '32', '33' ]
  44. ] ) );
  45. } );
  46. it( 'should clear contents of the selected table cells and put selection in last cell on forward delete', () => {
  47. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  48. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  49. const domEventData = new DomEventData( viewDocument, {
  50. preventDefault: sinon.spy()
  51. }, {
  52. direction: 'forward',
  53. unit: 'character',
  54. sequence: 1
  55. } );
  56. viewDocument.fire( 'delete', domEventData );
  57. assertEqualMarkup( getModelData( model ), modelTable( [
  58. [ '[]', '', '13' ],
  59. [ '', '', '23' ],
  60. [ '31', '32', '33' ]
  61. ] ) );
  62. } );
  63. it( 'should not interfere with default key handler if no table selection', () => {
  64. setModelData( model, modelTable( [
  65. [ '11[]', '12', '13' ],
  66. [ '21', '22', '23' ],
  67. [ '31', '32', '33' ]
  68. ] ) );
  69. const domEventData = new DomEventData( viewDocument, {
  70. preventDefault: sinon.spy()
  71. }, {
  72. direction: 'backward',
  73. unit: 'character',
  74. sequence: 1
  75. } );
  76. viewDocument.fire( 'delete', domEventData );
  77. assertEqualMarkup( getModelData( model ), modelTable( [
  78. [ '1[]', '12', '13' ],
  79. [ '21', '22', '23' ],
  80. [ '31', '32', '33' ]
  81. ] ) );
  82. } );
  83. } );
  84. describe( 'on user input', () => {
  85. beforeEach( async () => {
  86. await setupEditor( [ Input ] );
  87. } );
  88. it( 'should clear contents of the selected table cells and put selection in last cell on user input', () => {
  89. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  90. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  91. viewDocument.fire( 'keydown', { keyCode: getCode( 'x' ) } );
  92. // Mutate at the place where the document selection was put; it's more realistic
  93. // than mutating at some arbitrary position.
  94. const placeOfMutation = viewDocument.selection.getFirstRange().start.parent;
  95. viewDocument.fire( 'mutations', [
  96. {
  97. type: 'children',
  98. oldChildren: [],
  99. newChildren: [ new ViewText( 'x' ) ],
  100. node: placeOfMutation
  101. }
  102. ] );
  103. assertEqualMarkup( getModelData( model ), modelTable( [
  104. [ '', '', '13' ],
  105. [ '', 'x[]', '23' ],
  106. [ '31', '32', '33' ]
  107. ] ) );
  108. } );
  109. it( 'should not interfere with default key handler if no table selection', () => {
  110. viewDocument.fire( 'keydown', { keyCode: getCode( 'x' ) } );
  111. // Mutate at the place where the document selection was put; it's more realistic
  112. // than mutating at some arbitrary position.
  113. const placeOfMutation = viewDocument.selection.getFirstRange().start.parent;
  114. viewDocument.fire( 'mutations', [
  115. {
  116. type: 'children',
  117. oldChildren: [],
  118. newChildren: [ new ViewText( 'x' ) ],
  119. node: placeOfMutation
  120. }
  121. ] );
  122. assertEqualMarkup( getModelData( model ), modelTable( [
  123. [ 'x[]11', '12', '13' ],
  124. [ '21', '22', '23' ],
  125. [ '31', '32', '33' ]
  126. ] ) );
  127. } );
  128. } );
  129. } );
  130. async function setupEditor( plugins ) {
  131. element = document.createElement( 'div' );
  132. document.body.appendChild( element );
  133. editor = await ClassicTestEditor.create( element, {
  134. plugins: [ TableEditing, TableSelection, Paragraph, ...plugins ]
  135. } );
  136. model = editor.model;
  137. modelRoot = model.document.getRoot();
  138. viewDocument = editor.editing.view.document;
  139. tableSelection = editor.plugins.get( TableSelection );
  140. setModelData( model, modelTable( [
  141. [ '[]11', '12', '13' ],
  142. [ '21', '22', '23' ],
  143. [ '31', '32', '33' ]
  144. ] ) );
  145. }
  146. } );