8
0

tableselection-integration.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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._setCellSelection(
  31. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  32. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  33. );
  34. const domEventData = new DomEventData( viewDocument, {
  35. preventDefault: sinon.spy()
  36. }, {
  37. direction: 'backward',
  38. unit: 'character',
  39. sequence: 1
  40. } );
  41. viewDocument.fire( 'delete', domEventData );
  42. assertEqualMarkup( getModelData( model ), modelTable( [
  43. [ '', '', '13' ],
  44. [ '', '[]', '23' ],
  45. [ '31', '32', '33' ]
  46. ] ) );
  47. } );
  48. it( 'should clear contents of the selected table cells and put selection in last cell on forward delete', () => {
  49. tableSelection._setCellSelection(
  50. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  51. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  52. );
  53. const domEventData = new DomEventData( viewDocument, {
  54. preventDefault: sinon.spy()
  55. }, {
  56. direction: 'forward',
  57. unit: 'character',
  58. sequence: 1
  59. } );
  60. viewDocument.fire( 'delete', domEventData );
  61. assertEqualMarkup( getModelData( model ), modelTable( [
  62. [ '[]', '', '13' ],
  63. [ '', '', '23' ],
  64. [ '31', '32', '33' ]
  65. ] ) );
  66. } );
  67. it( 'should not interfere with default key handler if no table selection', () => {
  68. setModelData( model, modelTable( [
  69. [ '11[]', '12', '13' ],
  70. [ '21', '22', '23' ],
  71. [ '31', '32', '33' ]
  72. ] ) );
  73. const domEventData = new DomEventData( viewDocument, {
  74. preventDefault: sinon.spy()
  75. }, {
  76. direction: 'backward',
  77. unit: 'character',
  78. sequence: 1
  79. } );
  80. viewDocument.fire( 'delete', domEventData );
  81. assertEqualMarkup( getModelData( model ), modelTable( [
  82. [ '1[]', '12', '13' ],
  83. [ '21', '22', '23' ],
  84. [ '31', '32', '33' ]
  85. ] ) );
  86. } );
  87. it( 'should work with any arbitrary selection passed to Model#deleteContent() (delete backwards)', () => {
  88. const selection = model.createSelection( [
  89. model.createRange(
  90. model.createPositionFromPath( modelRoot, [ 0, 0, 0 ] ),
  91. model.createPositionFromPath( modelRoot, [ 0, 0, 1 ] )
  92. ),
  93. model.createRange(
  94. model.createPositionFromPath( modelRoot, [ 0, 0, 1 ] ),
  95. model.createPositionFromPath( modelRoot, [ 0, 0, 2 ] )
  96. )
  97. ] );
  98. model.change( writer => {
  99. model.deleteContent( selection );
  100. writer.setSelection( selection );
  101. } );
  102. assertEqualMarkup( getModelData( model ), modelTable( [
  103. [ '', '[]', '13' ],
  104. [ '21', '22', '23' ],
  105. [ '31', '32', '33' ]
  106. ] ) );
  107. } );
  108. it( 'should work with any arbitrary selection passed to Model#deleteContent() (delete forwards)', () => {
  109. const selection = model.createSelection( [
  110. model.createRange(
  111. model.createPositionFromPath( modelRoot, [ 0, 0, 0 ] ),
  112. model.createPositionFromPath( modelRoot, [ 0, 0, 1 ] )
  113. ),
  114. model.createRange(
  115. model.createPositionFromPath( modelRoot, [ 0, 0, 1 ] ),
  116. model.createPositionFromPath( modelRoot, [ 0, 0, 2 ] )
  117. )
  118. ] );
  119. model.change( writer => {
  120. model.deleteContent( selection, {
  121. direction: 'forward'
  122. } );
  123. writer.setSelection( selection );
  124. } );
  125. assertEqualMarkup( getModelData( model ), modelTable( [
  126. [ '[]', '', '13' ],
  127. [ '21', '22', '23' ],
  128. [ '31', '32', '33' ]
  129. ] ) );
  130. } );
  131. } );
  132. describe( 'on user input', () => {
  133. beforeEach( async () => {
  134. await setupEditor( [ Input ] );
  135. } );
  136. it( 'should clear contents of the selected table cells and put selection in last cell on user input', () => {
  137. tableSelection._setCellSelection(
  138. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  139. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  140. );
  141. viewDocument.fire( 'keydown', { keyCode: getCode( 'x' ) } );
  142. // Mutate at the place where the document selection was put; it's more realistic
  143. // than mutating at some arbitrary position.
  144. const placeOfMutation = viewDocument.selection.getFirstRange().start.parent;
  145. viewDocument.fire( 'mutations', [
  146. {
  147. type: 'children',
  148. oldChildren: [],
  149. newChildren: [ new ViewText( viewDocument, 'x' ) ],
  150. node: placeOfMutation
  151. }
  152. ] );
  153. assertEqualMarkup( getModelData( model ), modelTable( [
  154. [ '', '', '13' ],
  155. [ '', 'x[]', '23' ],
  156. [ '31', '32', '33' ]
  157. ] ) );
  158. } );
  159. it( 'should not interfere with default key handler if no table selection', () => {
  160. viewDocument.fire( 'keydown', { keyCode: getCode( 'x' ) } );
  161. // Mutate at the place where the document selection was put; it's more realistic
  162. // than mutating at some arbitrary position.
  163. const placeOfMutation = viewDocument.selection.getFirstRange().start.parent;
  164. viewDocument.fire( 'mutations', [
  165. {
  166. type: 'children',
  167. oldChildren: [],
  168. newChildren: [ new ViewText( viewDocument, 'x' ) ],
  169. node: placeOfMutation
  170. }
  171. ] );
  172. assertEqualMarkup( getModelData( model ), modelTable( [
  173. [ 'x[]11', '12', '13' ],
  174. [ '21', '22', '23' ],
  175. [ '31', '32', '33' ]
  176. ] ) );
  177. } );
  178. } );
  179. } );
  180. async function setupEditor( plugins ) {
  181. element = document.createElement( 'div' );
  182. document.body.appendChild( element );
  183. editor = await ClassicTestEditor.create( element, {
  184. plugins: [ TableEditing, TableSelection, Paragraph, ...plugins ]
  185. } );
  186. model = editor.model;
  187. modelRoot = model.document.getRoot();
  188. viewDocument = editor.editing.view.document;
  189. tableSelection = editor.plugins.get( TableSelection );
  190. setModelData( model, modelTable( [
  191. [ '[]11', '12', '13' ],
  192. [ '21', '22', '23' ],
  193. [ '31', '32', '33' ]
  194. ] ) );
  195. }
  196. } );