Procházet zdrojové kódy

Refactor TableClipboard internal methods.

Maciej Gołaszewski před 6 roky
rodič
revize
575329db8a

+ 14 - 45
packages/ckeditor5-table/src/tableclipboard.js

@@ -51,19 +51,19 @@ export default class TableClipboard extends Plugin {
 		 */
 		 */
 		this._tableSelection = editor.plugins.get( 'TableSelection' );
 		this._tableSelection = editor.plugins.get( 'TableSelection' );
 
 
-		this.listenTo( viewDocument, 'copy', ( evt, data ) => this._onCopy( evt, data ) );
-		this.listenTo( viewDocument, 'cut', ( evt, data ) => this._onCut( evt, data ) );
+		this.listenTo( viewDocument, 'copy', ( evt, data ) => this._onCopyCut( evt, data ) );
+		this.listenTo( viewDocument, 'cut', ( evt, data ) => this._onCopyCut( evt, data ) );
 		this.listenTo( viewDocument, 'clipboardOutput', ( evt, data ) => this._onClipboardOutput( evt, data ) );
 		this.listenTo( viewDocument, 'clipboardOutput', ( evt, data ) => this._onClipboardOutput( evt, data ) );
 	}
 	}
 
 
 	/**
 	/**
-	 * A clipboard "copy" event handler.
+	 * Copies table content to a clipboard on "copy" & "cut" events.
 	 *
 	 *
 	 * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the handled event.
 	 * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the handled event.
 	 * @param {Object} data Clipboard event data.
 	 * @param {Object} data Clipboard event data.
 	 * @private
 	 * @private
 	 */
 	 */
-	_onCopy( evt, data ) {
+	_onCopyCut( evt, data ) {
 		const tableSelection = this._tableSelection;
 		const tableSelection = this._tableSelection;
 
 
 		if ( !tableSelection.hasMultiCellSelection ) {
 		if ( !tableSelection.hasMultiCellSelection ) {
@@ -73,32 +73,20 @@ export default class TableClipboard extends Plugin {
 		data.preventDefault();
 		data.preventDefault();
 		evt.stop();
 		evt.stop();
 
 
-		this._copySelectedCellsToClipboard( tableSelection.getSelectionAsFragment(), data, 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 ) {
-		const tableSelection = this._tableSelection;
-
-		if ( !tableSelection.hasMultiCellSelection ) {
-			return;
-		}
+		const dataController = this.editor.data;
+		const viewDocument = this.editor.editing.view.document;
 
 
-		data.preventDefault();
-		evt.stop();
+		const content = dataController.toView( this._tableSelection.getSelectionAsFragment() );
 
 
-		this._copySelectedCellsToClipboard( tableSelection.getSelectionAsFragment(), data, evt.name );
-		clearTableCellsContents( this.editor.model, tableSelection.getSelectedTableCells() );
+		viewDocument.fire( 'clipboardOutput', {
+			dataTransfer: data.dataTransfer,
+			content,
+			method: evt.name
+		} );
 	}
 	}
 
 
 	/**
 	/**
-	 * Overrides default Clipboard plugin "clipboardOutput" handler. The table contents clearing is on in {@link #_onCut} handler.
+	 * Overrides default Clipboard plugin "clipboardOutput" handler to properly handle clearing selected table contents.
 	 *
 	 *
 	 * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the handled event.
 	 * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the handled event.
 	 * @param {Object} data Clipboard event data.
 	 * @param {Object} data Clipboard event data.
@@ -113,26 +101,7 @@ export default class TableClipboard extends Plugin {
 
 
 		data.dataTransfer.setData( 'text/html', new HtmlDataProcessor().toData( data.content ) );
 		data.dataTransfer.setData( 'text/html', new HtmlDataProcessor().toData( data.content ) );
 		data.dataTransfer.setData( 'text/plain', viewToPlainText( data.content ) );
 		data.dataTransfer.setData( 'text/plain', viewToPlainText( data.content ) );
-	}
-
-	/**
-	 * Handles clipboard output the same way as Clipboard plugin would.
-	 *
-	 * @private
-	 * @param {Array.<module:engine/model/element~Element>} selectedTableCells
-	 * @param {module:clipboard/clipboard~ClipboardOutputEventData} data Event data.
-	 * @param {String} method Copy/cut method.
-	 */
-	_copySelectedCellsToClipboard( selectedTableCells, data, method ) {
-		const dataController = this.editor.data;
-		const viewDocument = this.editor.editing.view.document;
-
-		const content = dataController.toView( this._tableSelection.getSelectionAsFragment() );
 
 
-		viewDocument.fire( 'clipboardOutput', {
-			dataTransfer: data.dataTransfer,
-			content,
-			method
-		} );
+		clearTableCellsContents( this.editor.model, this._tableSelection.getSelectedTableCells() );
 	}
 	}
 }
 }

+ 21 - 5
packages/ckeditor5-table/tests/tableclipboard.js

@@ -13,7 +13,7 @@ import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
 import TableClipboard from '../src/tableclipboard';
 import TableClipboard from '../src/tableclipboard';
 import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
 
-describe.only( 'table clipboard', () => {
+describe( 'table clipboard', () => {
 	let editor, model, modelRoot, tableSelection, viewDocument;
 	let editor, model, modelRoot, tableSelection, viewDocument;
 
 
 	beforeEach( async () => {
 	beforeEach( async () => {
@@ -278,9 +278,25 @@ describe.only( 'table clipboard', () => {
 				expect( dataTransferMock.getData( 'text/html' ) ).to.equal( '00' );
 				expect( dataTransferMock.getData( 'text/html' ) ).to.equal( '00' );
 			} );
 			} );
 
 
+			it( 'should be preventable', () => {
+				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
+				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
+
+				viewDocument.on( 'clipboardOutput', evt => evt.stop(), { priority: 'high' } );
+
+				viewDocument.fire( 'cut', {
+					dataTransfer: createDataTransfer(),
+					preventDefault: sinon.spy()
+				} );
+
+				assertEqualMarkup( getModelData( model ), modelTable( [
+					[ { contents: '00', isSelected: true }, { contents: '01', isSelected: true }, '02' ],
+					[ { contents: '10', isSelected: true }, { contents: '11', isSelected: true }, '12' ],
+					[ '20', '21', '22' ]
+				] ) );
+			} );
+
 			it( 'is clears selected table cells', () => {
 			it( 'is clears selected table cells', () => {
-				const dataTransferMock = createDataTransfer();
-				const preventDefaultSpy = sinon.spy();
 				const spy = sinon.spy();
 				const spy = sinon.spy();
 
 
 				viewDocument.on( 'clipboardOutput', spy );
 				viewDocument.on( 'clipboardOutput', spy );
@@ -289,8 +305,8 @@ describe.only( 'table clipboard', () => {
 				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
 				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
 
 
 				viewDocument.fire( 'cut', {
 				viewDocument.fire( 'cut', {
-					dataTransfer: dataTransferMock,
-					preventDefault: preventDefaultSpy
+					dataTransfer: createDataTransfer(),
+					preventDefault: sinon.spy()
 				} );
 				} );
 
 
 				assertEqualMarkup( getModelData( model ), modelTable( [
 				assertEqualMarkup( getModelData( model ), modelTable( [