tableclipboard.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. /**
  6. * @module table/tableclipboard
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import TableSelection from './tableselection';
  10. /**
  11. * The table clipboard integration plugin.
  12. *
  13. * It introduces the ability to copy selected table cells.
  14. *
  15. * @extends module:core/plugin~Plugin
  16. */
  17. export default class TableClipboard extends Plugin {
  18. /**
  19. * @inheritDoc
  20. */
  21. static get pluginName() {
  22. return 'TableClipboard';
  23. }
  24. /**
  25. * @inheritDoc
  26. */
  27. static get requires() {
  28. return [ TableSelection ];
  29. }
  30. /**
  31. * @inheritDoc
  32. */
  33. init() {
  34. const editor = this.editor;
  35. const viewDocument = editor.editing.view.document;
  36. this.listenTo( viewDocument, 'copy', ( evt, data ) => this._onCopyCut( evt, data ) );
  37. this.listenTo( viewDocument, 'cut', ( evt, data ) => this._onCopyCut( evt, data ) );
  38. }
  39. /**
  40. * Copies table content to a clipboard on "copy" & "cut" events.
  41. *
  42. * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the handled event.
  43. * @param {Object} data Clipboard event data.
  44. * @private
  45. */
  46. _onCopyCut( evt, data ) {
  47. const tableSelection = this.editor.plugins.get( 'TableSelection' );
  48. if ( !tableSelection.getSelectedTableCells() ) {
  49. return;
  50. }
  51. data.preventDefault();
  52. evt.stop();
  53. const dataController = this.editor.data;
  54. const viewDocument = this.editor.editing.view.document;
  55. const content = dataController.toView( tableSelection.getSelectionAsFragment() );
  56. viewDocument.fire( 'clipboardOutput', {
  57. dataTransfer: data.dataTransfer,
  58. content,
  59. method: evt.name
  60. } );
  61. }
  62. }