tableclipboard.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. * This plugin adds support for copying/cutting/pasting fragments of tables.
  12. * It is loaded automatically by the {@link module:table/table~Table} plugin.
  13. *
  14. * @extends module:core/plugin~Plugin
  15. */
  16. export default class TableClipboard extends Plugin {
  17. /**
  18. * @inheritDoc
  19. */
  20. static get pluginName() {
  21. return 'TableClipboard';
  22. }
  23. /**
  24. * @inheritDoc
  25. */
  26. static get requires() {
  27. return [ TableSelection ];
  28. }
  29. /**
  30. * @inheritDoc
  31. */
  32. init() {
  33. const editor = this.editor;
  34. const viewDocument = editor.editing.view.document;
  35. this.listenTo( viewDocument, 'copy', ( evt, data ) => this._onCopyCut( evt, data ) );
  36. this.listenTo( viewDocument, 'cut', ( evt, data ) => this._onCopyCut( evt, data ) );
  37. }
  38. /**
  39. * Copies table content to a clipboard on "copy" & "cut" events.
  40. *
  41. * @private
  42. * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the handled event.
  43. * @param {Object} data Clipboard event data.
  44. */
  45. _onCopyCut( evt, data ) {
  46. const tableSelection = this.editor.plugins.get( 'TableSelection' );
  47. if ( !tableSelection.getSelectedTableCells() ) {
  48. return;
  49. }
  50. if ( evt.name == 'cut' && this.editor.isReadOnly ) {
  51. return;
  52. }
  53. data.preventDefault();
  54. evt.stop();
  55. const dataController = this.editor.data;
  56. const viewDocument = this.editor.editing.view.document;
  57. const content = dataController.toView( tableSelection.getSelectionAsFragment() );
  58. viewDocument.fire( 'clipboardOutput', {
  59. dataTransfer: data.dataTransfer,
  60. content,
  61. method: evt.name
  62. } );
  63. }
  64. }