Browse Source

Handle "cut" event in table clipboard.

Maciej Gołaszewski 5 years ago
parent
commit
26ebc2144d

+ 30 - 16
packages/ckeditor5-table/src/tableclipboard.js

@@ -10,6 +10,8 @@
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import TableSelection from './tableselection';
 import { clearTableCellsContents } from './tableselection/utils';
+import viewToPlainText from '@ckeditor/ckeditor5-clipboard/src/utils/viewtoplaintext';
+import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
 
 /**
  * The table clipboard integration plugin.
@@ -49,8 +51,8 @@ export default class TableClipboard extends Plugin {
 		 */
 		this._tableSelection = editor.plugins.get( 'TableSelection' );
 
-		this.listenTo( viewDocument, 'copy', ( evt, data ) => this._onCopy( evt, data ), { priority: 'normal' } );
-		this.listenTo( viewDocument, 'cut', ( evt, data ) => this._onCut( evt, data ), { priority: 'high' } );
+		this.listenTo( viewDocument, 'copy', ( evt, data ) => this._onCopy( evt, data ) );
+		this.listenTo( viewDocument, 'cut', ( evt, data ) => this._onCut( evt, data ) );
 	}
 
 	/**
@@ -70,16 +72,7 @@ export default class TableClipboard extends Plugin {
 		data.preventDefault();
 		evt.stop();
 
-		const dataController = this.editor.data;
-		const viewDocument = this.editor.editing.view.document;
-
-		const content = dataController.toView( tableSelection.getSelectionAsFragment() );
-
-		viewDocument.fire( 'clipboardOutput', {
-			dataTransfer: data.dataTransfer,
-			content,
-			method: evt.name
-		} );
+		this._copySelectedCellsToClipboard( tableSelection.getSelectionAsFragment(), data );
 	}
 
 	/**
@@ -90,11 +83,32 @@ export default class TableClipboard extends Plugin {
 	 * @private
 	 */
 	_onCut( evt, data ) {
-		if ( this._tableSelection.hasMultiCellSelection ) {
-			data.preventDefault();
-			evt.stop();
+		const tableSelection = this._tableSelection;
 
-			clearTableCellsContents( this.editor.model, this._tableSelection.getSelectedTableCells() );
+		if ( !tableSelection.hasMultiCellSelection ) {
+			return;
 		}
+
+		data.preventDefault();
+		evt.stop();
+
+		this._copySelectedCellsToClipboard( tableSelection.getSelectionAsFragment(), data );
+		clearTableCellsContents( this.editor.model, tableSelection.getSelectedTableCells() );
+	}
+
+	/**
+	 * 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.
+	 */
+	_copySelectedCellsToClipboard( selectedTableCells, data ) {
+		const dataController = this.editor.data;
+
+		const content = dataController.toView( selectedTableCells );
+
+		data.dataTransfer.setData( 'text/html', new HtmlDataProcessor().toData( content ) );
+		data.dataTransfer.setData( 'text/plain', viewToPlainText( content ) );
 	}
 }

+ 69 - 64
packages/ckeditor5-table/tests/tableclipboard.js

@@ -10,8 +10,6 @@ import { getData as getModelData, setData as setModelData } from '@ckeditor/cked
 import TableEditing from '../src/tableediting';
 import { modelTable, viewTable } from './_utils/utils';
 import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
-import ViewDocumentFragment from '@ckeditor/ckeditor5-engine/src/view/documentfragment';
-import { stringify as stringifyView } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
 import TableClipboard from '../src/tableclipboard';
 import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
@@ -55,35 +53,26 @@ describe( 'table clipboard', () => {
 				sinon.assert.calledOnce( spy );
 			} );
 
-			it( 'should copy selected table cells as standalone table', done => {
-				const dataTransferMock = createDataTransfer();
+			it( 'should copy selected table cells as a standalone table', () => {
 				const preventDefaultSpy = sinon.spy();
 
 				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
 				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 2 ] ) );
 
-				viewDocument.on( 'clipboardOutput', ( evt, data ) => {
-					expect( preventDefaultSpy.calledOnce ).to.be.true;
-					expect( data.method ).to.equal( 'copy' );
-
-					expect( data.dataTransfer ).to.equal( dataTransferMock );
-
-					expect( data.content ).is.instanceOf( ViewDocumentFragment );
-					expect( stringifyView( data.content ) ).to.equal( viewTable( [
-						[ '01', '02' ],
-						[ '11', '12' ]
-					] ) );
-
-					done();
-				} );
-
-				viewDocument.fire( 'copy', {
-					dataTransfer: dataTransferMock,
+				const data = {
+					dataTransfer: createDataTransfer(),
 					preventDefault: preventDefaultSpy
-				} );
+				};
+				viewDocument.fire( 'copy', data );
+
+				sinon.assert.calledOnce( preventDefaultSpy );
+				expect( data.dataTransfer.getData( 'text/html' ) ).to.equal( viewTable( [
+					[ '01', '02' ],
+					[ '11', '12' ]
+				] ) );
 			} );
 
-			it( 'should trim selected table to a selection rectangle (inner cell with colspan, no colspan after trim)', done => {
+			it( 'should trim selected table to a selection rectangle (inner cell with colspan, no colspan after trim)', () => {
 				setModelData( model, modelTable( [
 					[ '00[]', '01', '02' ],
 					[ '10', { contents: '11', colspan: 2 } ],
@@ -93,14 +82,14 @@ describe( 'table clipboard', () => {
 				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
 				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) );
 
-				assertClipboardCopy( viewTable( [
+				assertClipboardContentOnMethod( 'copy', viewTable( [
 					[ '00', '01' ],
 					[ '10', '11' ],
 					[ '20', '21' ]
-				] ), done );
+				] ) );
 			} );
 
-			it( 'should trim selected table to a selection rectangle (inner cell with colspan, has colspan after trim)', done => {
+			it( 'should trim selected table to a selection rectangle (inner cell with colspan, has colspan after trim)', () => {
 				setModelData( model, modelTable( [
 					[ '00[]', '01', '02' ],
 					[ { contents: '10', colspan: 3 } ],
@@ -110,14 +99,14 @@ describe( 'table clipboard', () => {
 				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
 				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) );
 
-				assertClipboardCopy( viewTable( [
+				assertClipboardContentOnMethod( 'copy', viewTable( [
 					[ '00', '01' ],
 					[ { contents: '10', colspan: 2 } ],
 					[ '20', '21' ]
-				] ), done );
+				] ) );
 			} );
 
-			it( 'should trim selected table to a selection rectangle (inner cell with rowspan, no colspan after trim)', done => {
+			it( 'should trim selected table to a selection rectangle (inner cell with rowspan, no colspan after trim)', () => {
 				setModelData( model, modelTable( [
 					[ '00[]', '01', '02' ],
 					[ '10', { contents: '11', rowspan: 2 }, '12' ],
@@ -127,13 +116,13 @@ describe( 'table clipboard', () => {
 				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
 				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 2 ] ) );
 
-				assertClipboardCopy( viewTable( [
+				assertClipboardContentOnMethod( 'copy', viewTable( [
 					[ '00', '01', '02' ],
 					[ '10', '11', '12' ]
-				] ), done );
+				] ) );
 			} );
 
-			it( 'should trim selected table to a selection rectangle (inner cell with rowspan, has rowspan after trim)', done => {
+			it( 'should trim selected table to a selection rectangle (inner cell with rowspan, has rowspan after trim)', () => {
 				setModelData( model, modelTable( [
 					[ '00[]', { contents: '01', rowspan: 3 }, '02' ],
 					[ '10', '12' ],
@@ -143,13 +132,13 @@ describe( 'table clipboard', () => {
 				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
 				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
 
-				assertClipboardCopy( viewTable( [
+				assertClipboardContentOnMethod( 'copy', viewTable( [
 					[ '00', { contents: '01', rowspan: 2 }, '02' ],
 					[ '10', '12' ]
-				] ), done );
+				] ) );
 			} );
 
-			it( 'should prepend spanned columns with empty cells (outside cell with colspan)', done => {
+			it( 'should prepend spanned columns with empty cells (outside cell with colspan)', () => {
 				setModelData( model, modelTable( [
 					[ '00[]', '01', '02' ],
 					[ { contents: '10', colspan: 2 }, '12' ],
@@ -159,14 +148,14 @@ describe( 'table clipboard', () => {
 				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
 				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 2 ] ) );
 
-				assertClipboardCopy( viewTable( [
+				assertClipboardContentOnMethod( 'copy', viewTable( [
 					[ '01', '02' ],
-					[ '', '12' ],
+					[ '&nbsp;', '12' ],
 					[ '21', '22' ]
-				] ), done );
+				] ) );
 			} );
 
-			it( 'should prepend spanned columns with empty cells (outside cell with rowspan)', done => {
+			it( 'should prepend spanned columns with empty cells (outside cell with rowspan)', () => {
 				setModelData( model, modelTable( [
 					[ '00[]', { contents: '01', rowspan: 2 }, '02' ],
 					[ '10', '12' ],
@@ -176,13 +165,13 @@ describe( 'table clipboard', () => {
 				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) );
 				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 2 ] ) );
 
-				assertClipboardCopy( viewTable( [
-					[ '10', '', '12' ],
+				assertClipboardContentOnMethod( 'copy', viewTable( [
+					[ '10', '&nbsp;', '12' ],
 					[ '20', '21', '22' ]
-				] ), done );
+				] ) );
 			} );
 
-			it( 'should fix selected table to a selection rectangle (hardcore case)', done => {
+			it( 'should fix selected table to a selection rectangle (hardcore case)', () => {
 				// This test check how previous simple rules run together (mixed prepending and trimming).
 				// In the example below a selection is set from cell "32" to "88"
 				//
@@ -223,17 +212,17 @@ describe( 'table clipboard', () => {
 				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) );
 				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 7, 6 ] ) );
 
-				assertClipboardCopy( viewTable( [
-					[ '21', '', '23', '', '', { contents: '27', colspan: 2 } ],
-					[ '31', '', '33', '', '', '37', '37' ],
-					[ '', '', '', '', '', '47', '47' ],
-					[ '51', '52', '53', '', '', { contents: '57', rowspan: 3 }, '57' ],
-					[ { contents: '61', colspan: 3 }, '', '', '', '67' ],
+				assertClipboardContentOnMethod( 'copy', viewTable( [
+					[ '21', '&nbsp;', '23', '&nbsp;', '&nbsp;', { contents: '27', colspan: 2 } ],
+					[ '31', '&nbsp;', '33', '&nbsp;', '&nbsp;', '37', '37' ],
+					[ '&nbsp;', '&nbsp;', '&nbsp;', '&nbsp;', '&nbsp;', '47', '47' ],
+					[ '51', '52', '53', '&nbsp;', '&nbsp;', { contents: '57', rowspan: 3 }, '57' ],
+					[ { contents: '61', colspan: 3 }, '&nbsp;', '&nbsp;', '&nbsp;', '67' ],
 					[ '71', '72', '73', '74', '75', '77' ]
-				] ), done );
+				] ) );
 			} );
 
-			it( 'should update table heading attributes (selection with headings)', done => {
+			it( 'should update table heading attributes (selection with headings)', () => {
 				setModelData( model, modelTable( [
 					[ '00', '01', '02', '03', '04' ],
 					[ '10', '11', '12', '13', '14' ],
@@ -245,14 +234,14 @@ describe( 'table clipboard', () => {
 				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
 				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 3, 3 ] ) );
 
-				assertClipboardCopy( viewTable( [
+				assertClipboardContentOnMethod( 'copy', viewTable( [
 					[ '11', '12', '13' ],
 					[ '21', '22', '23' ],
 					[ { contents: '31', isHeading: true }, '32', '33' ] // TODO: bug in viewTable
-				], { headingRows: 2, headingColumns: 1 } ), done );
+				], { headingRows: 2, headingColumns: 1 } ) );
 			} );
 
-			it( 'should update table heading attributes (selection without headings)', done => {
+			it( 'should update table heading attributes (selection without headings)', () => {
 				setModelData( model, modelTable( [
 					[ '00', '01', '02', '03', '04' ],
 					[ '10', '11', '12', '13', '14' ],
@@ -264,10 +253,10 @@ describe( 'table clipboard', () => {
 				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 3, 2 ] ) );
 				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 4, 4 ] ) );
 
-				assertClipboardCopy( viewTable( [
+				assertClipboardContentOnMethod( 'copy', viewTable( [
 					[ '32', '33', '34' ],
 					[ '42', '43', '44' ]
-				] ), done );
+				] ) );
 			} );
 		} );
 
@@ -307,20 +296,36 @@ describe( 'table clipboard', () => {
 					[ '20', '21', '22' ]
 				] ) );
 			} );
-		} );
-	} );
 
-	function assertClipboardCopy( expectedViewTable, callback ) {
-		viewDocument.on( 'clipboardOutput', ( evt, data ) => {
-			expect( stringifyView( data.content ) ).to.equal( expectedViewTable );
+			it( 'should copy selected table cells as a standalone table', () => {
+				const preventDefaultSpy = sinon.spy();
 
-			callback();
+				tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
+				tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 2 ] ) );
+
+				const data = {
+					dataTransfer: createDataTransfer(),
+					preventDefault: preventDefaultSpy
+				};
+				viewDocument.fire( 'cut', data );
+
+				sinon.assert.calledOnce( preventDefaultSpy );
+				expect( data.dataTransfer.getData( 'text/html' ) ).to.equal( viewTable( [
+					[ '01', '02' ],
+					[ '11', '12' ]
+				] ) );
+			} );
 		} );
+	} );
 
-		viewDocument.fire( 'copy', {
+	function assertClipboardContentOnMethod( method, expectedViewTable ) {
+		const data = {
 			dataTransfer: createDataTransfer(),
 			preventDefault: sinon.spy()
-		} );
+		};
+		viewDocument.fire( method, data );
+
+		expect( data.dataTransfer.getData( 'text/html' ) ).to.equal( expectedViewTable );
 	}
 
 	function createDataTransfer() {