Przeglądaj źródła

Extract TableClipboard plugin.

Maciej Gołaszewski 6 lat temu
rodzic
commit
322847d0f9

+ 39 - 17
packages/ckeditor5-table/src/tableclipboard.js

@@ -39,24 +39,29 @@ export default class TableClipboard extends Plugin {
 		const editor = this.editor;
 		const viewDocument = editor.editing.view.document;
 
-		const tableSelection = editor.plugins.get( 'TableSelection' );
+		/**
+		 * A table selection plugin instance.
+		 *
+		 * @private
+		 * @readonly
+		 * @member {module:table/tableselection~TableSelection} module:tableclipboard~TableClipboard#_tableSelection
+		 */
+		this._tableSelection = editor.plugins.get( 'TableSelection' );
 
-		this.listenTo( viewDocument, 'copy', createTableCopyHandler( tableSelection, editor ), { priority: 'normal' } );
-		this.listenTo( viewDocument, 'cut', createPreventTableCutHandler( tableSelection ), { priority: 'high' } );
+		this.listenTo( viewDocument, 'copy', ( evt, data ) => this._onCopy( evt, data ), { priority: 'normal' } );
+		this.listenTo( viewDocument, 'cut', ( evt, data ) => this._onCut( evt, data ), { priority: 'high' } );
 	}
-}
 
-function createPreventTableCutHandler( tableSelection ) {
-	return ( evt, data ) => {
-		if ( tableSelection.hasMultiCellSelection ) {
-			data.preventDefault();
-			evt.stop();
-		}
-	};
-}
+	/**
+	 * A clipboard "copy" event handler.
+	 *
+	 * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the handled event.
+	 * @param {Object} data Clipboard event data.
+	 * @private
+	 */
+	_onCopy( evt, data ) {
+		const tableSelection = this._tableSelection;
 
-function createTableCopyHandler( tableSelection, editor ) {
-	return ( evt, data ) => {
 		if ( !tableSelection.hasMultiCellSelection ) {
 			return;
 		}
@@ -64,12 +69,29 @@ function createTableCopyHandler( tableSelection, editor ) {
 		data.preventDefault();
 		evt.stop();
 
-		const content = editor.data.toView( tableSelection.getSelectionAsFragment() );
+		const dataController = this.editor.data;
+		const viewDocument = this.editor.editing.view.document;
+
+		const content = dataController.toView( tableSelection.getSelectionAsFragment() );
 
-		editor.editing.view.document.fire( 'clipboardOutput', {
+		viewDocument.fire( 'clipboardOutput', {
 			dataTransfer: data.dataTransfer,
 			content,
 			method: evt.name
 		} );
-	};
+	}
+
+	/**
+	 * A clipboard "cut" event handler.
+	 *
+	 * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the handled event.
+	 * @param {Object} data Clipboard event data.
+	 * @private
+	 */
+	_onCut( evt, data ) {
+		if ( this._tableSelection.hasMultiCellSelection ) {
+			data.preventDefault();
+			evt.stop();
+		}
+	}
 }

+ 1 - 1
packages/ckeditor5-table/tests/tableclipboard.js

@@ -14,7 +14,7 @@ import ViewDocumentFragment from '@ckeditor/ckeditor5-engine/src/view/documentfr
 import { stringify as stringifyView } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
 import TableClipboard from '../src/tableclipboard';
 
-describe( 'table copy-paste', () => {
+describe.only( 'table clipboard', () => {
 	let editor, model, modelRoot, tableSelection, viewDocument;
 
 	beforeEach( async () => {