tableselection-integration.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. it( 'should work with any arbitrary selection passed to Model#deleteContent() (delete backwards)', () => {
  84. const selection = model.createSelection( [
  85. model.createRange(
  86. model.createPositionFromPath( modelRoot, [ 0, 0, 0 ] ),
  87. model.createPositionFromPath( modelRoot, [ 0, 0, 1 ] )
  88. ),
  89. model.createRange(
  90. model.createPositionFromPath( modelRoot, [ 0, 0, 1 ] ),
  91. model.createPositionFromPath( modelRoot, [ 0, 0, 2 ] )
  92. )
  93. ] );
  94. model.change( writer => {
  95. model.deleteContent( selection );
  96. writer.setSelection( selection );
  97. } );
  98. assertEqualMarkup( getModelData( model ), modelTable( [
  99. [ '', '[]', '13' ],
  100. [ '21', '22', '23' ],
  101. [ '31', '32', '33' ]
  102. ] ) );
  103. } );
  104. it( 'should work with any arbitrary selection passed to Model#deleteContent() (delete forwards)', () => {
  105. const selection = model.createSelection( [
  106. model.createRange(
  107. model.createPositionFromPath( modelRoot, [ 0, 0, 0 ] ),
  108. model.createPositionFromPath( modelRoot, [ 0, 0, 1 ] )
  109. ),
  110. model.createRange(
  111. model.createPositionFromPath( modelRoot, [ 0, 0, 1 ] ),
  112. model.createPositionFromPath( modelRoot, [ 0, 0, 2 ] )
  113. )
  114. ] );
  115. model.change( writer => {
  116. model.deleteContent( selection, {
  117. direction: 'forward'
  118. } );
  119. writer.setSelection( selection );
  120. } );
  121. assertEqualMarkup( getModelData( model ), modelTable( [
  122. [ '[]', '', '13' ],
  123. [ '21', '22', '23' ],
  124. [ '31', '32', '33' ]
  125. ] ) );
  126. } );
  127. } );
  128. describe( 'on user input', () => {
  129. beforeEach( async () => {
  130. await setupEditor( [ Input ] );
  131. } );
  132. it( 'should clear contents of the selected table cells and put selection in last cell on user input', () => {
  133. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  134. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  135. viewDocument.fire( 'keydown', { keyCode: getCode( 'x' ) } );
  136. // Mutate at the place where the document selection was put; it's more realistic
  137. // than mutating at some arbitrary position.
  138. const placeOfMutation = viewDocument.selection.getFirstRange().start.parent;
  139. viewDocument.fire( 'mutations', [
  140. {
  141. type: 'children',
  142. oldChildren: [],
  143. newChildren: [ new ViewText( viewDocument, 'x' ) ],
  144. node: placeOfMutation
  145. }
  146. ] );
  147. assertEqualMarkup( getModelData( model ), modelTable( [
  148. [ '', '', '13' ],
  149. [ '', 'x[]', '23' ],
  150. [ '31', '32', '33' ]
  151. ] ) );
  152. } );
  153. it( 'should not interfere with default key handler if no table selection', () => {
  154. viewDocument.fire( 'keydown', { keyCode: getCode( 'x' ) } );
  155. // Mutate at the place where the document selection was put; it's more realistic
  156. // than mutating at some arbitrary position.
  157. const placeOfMutation = viewDocument.selection.getFirstRange().start.parent;
  158. viewDocument.fire( 'mutations', [
  159. {
  160. type: 'children',
  161. oldChildren: [],
  162. newChildren: [ new ViewText( viewDocument, 'x' ) ],
  163. node: placeOfMutation
  164. }
  165. ] );
  166. assertEqualMarkup( getModelData( model ), modelTable( [
  167. [ 'x[]11', '12', '13' ],
  168. [ '21', '22', '23' ],
  169. [ '31', '32', '33' ]
  170. ] ) );
  171. } );
  172. } );
  173. } );
  174. async function setupEditor( plugins ) {
  175. element = document.createElement( 'div' );
  176. document.body.appendChild( element );
  177. editor = await ClassicTestEditor.create( element, {
  178. plugins: [ TableEditing, TableSelection, Paragraph, ...plugins ]
  179. } );
  180. model = editor.model;
  181. modelRoot = model.document.getRoot();
  182. viewDocument = editor.editing.view.document;
  183. tableSelection = editor.plugins.get( TableSelection );
  184. setModelData( model, modelTable( [
  185. [ '[]11', '12', '13' ],
  186. [ '21', '22', '23' ],
  187. [ '31', '32', '33' ]
  188. ] ) );
  189. }
  190. } );