8
0

tableselection-clipboard.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 copy selected table cells as standalone table', done => {
  36. const dataTransferMock = createDataTransfer();
  37. const preventDefaultSpy = sinon.spy();
  38. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  39. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 2 ] ) );
  40. viewDocument.on( 'clipboardOutput', ( evt, data ) => {
  41. expect( preventDefaultSpy.calledOnce ).to.be.true;
  42. expect( data.method ).to.equal( 'copy' );
  43. expect( data.dataTransfer ).to.equal( dataTransferMock );
  44. expect( data.content ).is.instanceOf( ViewDocumentFragment );
  45. expect( stringifyView( data.content ) ).to.equal( viewTable( [
  46. [ '12', '13' ],
  47. [ '22', '23' ]
  48. ] ) );
  49. done();
  50. } );
  51. viewDocument.fire( 'copy', {
  52. dataTransfer: dataTransferMock,
  53. preventDefault: preventDefaultSpy
  54. } );
  55. } );
  56. } );
  57. describe( 'cut', () => {
  58. it( 'is disabled for multi-range selection over a table', () => {
  59. const dataTransferMock = createDataTransfer();
  60. const preventDefaultSpy = sinon.spy();
  61. const spy = sinon.spy();
  62. viewDocument.on( 'clipboardOutput', spy );
  63. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  64. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 2 ] ) );
  65. viewDocument.fire( 'cut', {
  66. dataTransfer: dataTransferMock,
  67. preventDefault: preventDefaultSpy
  68. } );
  69. sinon.assert.notCalled( spy );
  70. sinon.assert.calledOnce( preventDefaultSpy );
  71. } );
  72. it( 'is not disabled normal selection over a table', () => {
  73. const dataTransferMock = createDataTransfer();
  74. const spy = sinon.spy();
  75. viewDocument.on( 'clipboardOutput', spy );
  76. viewDocument.fire( 'cut', {
  77. dataTransfer: dataTransferMock,
  78. preventDefault: sinon.spy()
  79. } );
  80. sinon.assert.calledOnce( spy );
  81. } );
  82. } );
  83. } );
  84. function createDataTransfer() {
  85. const store = new Map();
  86. return {
  87. setData( type, data ) {
  88. store.set( type, data );
  89. },
  90. getData( type ) {
  91. return store.get( type );
  92. }
  93. };
  94. }
  95. } );