tableselection-clipboard.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 { modelTable, viewTable } from './_utils/utils';
  11. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  12. import ViewDocumentFragment from '@ckeditor/ckeditor5-engine/src/view/documentfragment';
  13. import { stringify as stringifyView } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  14. describe( 'table selection', () => {
  15. let editor, model, modelRoot, tableSelection, viewDocument;
  16. beforeEach( async () => {
  17. editor = await VirtualTestEditor.create( {
  18. plugins: [ TableEditing, TableSelection, Paragraph, Clipboard ]
  19. } );
  20. model = editor.model;
  21. modelRoot = model.document.getRoot();
  22. viewDocument = editor.editing.view.document;
  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( 'Clipboard integration', () => {
  34. describe( 'copy', () => {
  35. it( 'should to nothing for normal selection in table', () => {
  36. const dataTransferMock = createDataTransfer();
  37. const spy = sinon.spy();
  38. viewDocument.on( 'clipboardOutput', spy );
  39. viewDocument.fire( 'copy', {
  40. dataTransfer: dataTransferMock,
  41. preventDefault: sinon.spy()
  42. } );
  43. sinon.assert.calledOnce( spy );
  44. } );
  45. it( 'should copy selected table cells as standalone table', done => {
  46. const dataTransferMock = createDataTransfer();
  47. const preventDefaultSpy = sinon.spy();
  48. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  49. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 2 ] ) );
  50. viewDocument.on( 'clipboardOutput', ( evt, data ) => {
  51. expect( preventDefaultSpy.calledOnce ).to.be.true;
  52. expect( data.method ).to.equal( 'copy' );
  53. expect( data.dataTransfer ).to.equal( dataTransferMock );
  54. expect( data.content ).is.instanceOf( ViewDocumentFragment );
  55. expect( stringifyView( data.content ) ).to.equal( viewTable( [
  56. [ '12', '13' ],
  57. [ '22', '23' ]
  58. ] ) );
  59. done();
  60. } );
  61. viewDocument.fire( 'copy', {
  62. dataTransfer: dataTransferMock,
  63. preventDefault: preventDefaultSpy
  64. } );
  65. } );
  66. it( 'should fix selected table (cell has colspan that exceeds rectangular selection by 1)', done => {
  67. setModelData( model, modelTable( [
  68. [ '11[]', '12', '13' ],
  69. [ '21', { contents: '22', colspan: 2 } ],
  70. [ '31', '32', '33' ]
  71. ] ) );
  72. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  73. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) );
  74. viewDocument.on( 'clipboardOutput', ( evt, data ) => {
  75. expect( stringifyView( data.content ) ).to.equal( viewTable( [
  76. [ '11', '12' ],
  77. [ '21', '22' ],
  78. [ '31', '32' ]
  79. ] ) );
  80. done();
  81. } );
  82. viewDocument.fire( 'copy', {
  83. dataTransfer: createDataTransfer(),
  84. preventDefault: sinon.spy()
  85. } );
  86. } );
  87. it( 'should fix selected table (cell has colspan that exceeds rectangular selection but spans over selection)', done => {
  88. setModelData( model, modelTable( [
  89. [ '11[]', '12', '13' ],
  90. [ { contents: '21', colspan: 3 } ],
  91. [ '31', '32', '33' ]
  92. ] ) );
  93. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  94. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) );
  95. viewDocument.on( 'clipboardOutput', ( evt, data ) => {
  96. expect( stringifyView( data.content ) ).to.equal( viewTable( [
  97. [ '11', '12' ],
  98. [ { contents: '21', colspan: 2 } ],
  99. [ '31', '32' ]
  100. ] ) );
  101. done();
  102. } );
  103. viewDocument.fire( 'copy', {
  104. dataTransfer: createDataTransfer(),
  105. preventDefault: sinon.spy()
  106. } );
  107. } );
  108. it( 'should fix selected table (cell has rowspan that exceeds rectangular selection by 1)', done => {
  109. setModelData( model, modelTable( [
  110. [ '11[]', '12', '13' ],
  111. [ '21', { contents: '22', rowspan: 2 }, '23' ],
  112. [ '31', '32', '33' ]
  113. ] ) );
  114. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  115. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 2 ] ) );
  116. viewDocument.on( 'clipboardOutput', ( evt, data ) => {
  117. expect( stringifyView( data.content ) ).to.equal( viewTable( [
  118. [ '11', '12', '13' ],
  119. [ '21', '22', '23' ]
  120. ] ) );
  121. done();
  122. } );
  123. viewDocument.fire( 'copy', {
  124. dataTransfer: createDataTransfer(),
  125. preventDefault: sinon.spy()
  126. } );
  127. } );
  128. it( 'should fix selected table (cell has rowspan that exceeds rectangular selection but spans over selection)', done => {
  129. setModelData( model, modelTable( [
  130. [ '11[]', { contents: '12', rowspan: 3 }, '13' ],
  131. [ '21', '23' ],
  132. [ '31', '33' ]
  133. ] ) );
  134. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  135. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  136. viewDocument.on( 'clipboardOutput', ( evt, data ) => {
  137. expect( stringifyView( data.content ) ).to.equal( viewTable( [
  138. [ '11', { contents: '12', rowspan: 2 }, '13' ],
  139. [ '21', '23' ]
  140. ] ) );
  141. done();
  142. } );
  143. viewDocument.fire( 'copy', {
  144. dataTransfer: createDataTransfer(),
  145. preventDefault: sinon.spy()
  146. } );
  147. } );
  148. } );
  149. describe( 'cut', () => {
  150. it( 'is disabled for multi-range selection over a table', () => {
  151. const dataTransferMock = createDataTransfer();
  152. const preventDefaultSpy = sinon.spy();
  153. const spy = sinon.spy();
  154. viewDocument.on( 'clipboardOutput', spy );
  155. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  156. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 2 ] ) );
  157. viewDocument.fire( 'cut', {
  158. dataTransfer: dataTransferMock,
  159. preventDefault: preventDefaultSpy
  160. } );
  161. sinon.assert.notCalled( spy );
  162. sinon.assert.calledOnce( preventDefaultSpy );
  163. } );
  164. it( 'is not disabled normal selection over a table', () => {
  165. const dataTransferMock = createDataTransfer();
  166. const spy = sinon.spy();
  167. viewDocument.on( 'clipboardOutput', spy );
  168. viewDocument.fire( 'cut', {
  169. dataTransfer: dataTransferMock,
  170. preventDefault: sinon.spy()
  171. } );
  172. sinon.assert.calledOnce( spy );
  173. } );
  174. } );
  175. } );
  176. function createDataTransfer() {
  177. const store = new Map();
  178. return {
  179. setData( type, data ) {
  180. store.set( type, data );
  181. },
  182. getData( type ) {
  183. return store.get( type );
  184. }
  185. };
  186. }
  187. } );