8
0

tableselection-clipboard.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. } );
  67. describe( 'cut', () => {
  68. it( 'is disabled for multi-range selection over a table', () => {
  69. const dataTransferMock = createDataTransfer();
  70. const preventDefaultSpy = sinon.spy();
  71. const spy = sinon.spy();
  72. viewDocument.on( 'clipboardOutput', spy );
  73. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  74. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 2 ] ) );
  75. viewDocument.fire( 'cut', {
  76. dataTransfer: dataTransferMock,
  77. preventDefault: preventDefaultSpy
  78. } );
  79. sinon.assert.notCalled( spy );
  80. sinon.assert.calledOnce( preventDefaultSpy );
  81. } );
  82. it( 'is not disabled normal selection over a table', () => {
  83. const dataTransferMock = createDataTransfer();
  84. const spy = sinon.spy();
  85. viewDocument.on( 'clipboardOutput', spy );
  86. viewDocument.fire( 'cut', {
  87. dataTransfer: dataTransferMock,
  88. preventDefault: sinon.spy()
  89. } );
  90. sinon.assert.calledOnce( spy );
  91. } );
  92. } );
  93. } );
  94. function createDataTransfer() {
  95. const store = new Map();
  96. return {
  97. setData( type, data ) {
  98. store.set( type, data );
  99. },
  100. getData( type ) {
  101. return store.get( type );
  102. }
  103. };
  104. }
  105. } );