8
0

tableclipboard.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /**
  37. * A table selection plugin instance.
  38. *
  39. * @private
  40. * @readonly
  41. * @member {module:table/tableselection~TableSelection} module:tableclipboard~TableClipboard#_tableSelection
  42. */
  43. this._tableSelection = editor.plugins.get( 'TableSelection' );
  44. this.listenTo( viewDocument, 'copy', ( evt, data ) => this._onCopy( evt, data ), { priority: 'normal' } );
  45. this.listenTo( viewDocument, 'cut', ( evt, data ) => this._onCut( evt, data ), { priority: 'high' } );
  46. }
  47. /**
  48. * A clipboard "copy" event handler.
  49. *
  50. * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the handled event.
  51. * @param {Object} data Clipboard event data.
  52. * @private
  53. */
  54. _onCopy( evt, data ) {
  55. const tableSelection = this._tableSelection;
  56. if ( !tableSelection.hasMultiCellSelection ) {
  57. return;
  58. }
  59. data.preventDefault();
  60. evt.stop();
  61. const dataController = this.editor.data;
  62. const viewDocument = this.editor.editing.view.document;
  63. const content = dataController.toView( tableSelection.getSelectionAsFragment() );
  64. viewDocument.fire( 'clipboardOutput', {
  65. dataTransfer: data.dataTransfer,
  66. content,
  67. method: evt.name
  68. } );
  69. }
  70. /**
  71. * A clipboard "cut" event handler.
  72. *
  73. * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the handled event.
  74. * @param {Object} data Clipboard event data.
  75. * @private
  76. */
  77. _onCut( evt, data ) {
  78. if ( this._tableSelection.hasMultiCellSelection ) {
  79. data.preventDefault();
  80. evt.stop();
  81. }
  82. }
  83. }