8
0
Просмотр исходного кода

Copy selected table fragment to clipboard.

Maciej Gołaszewski 5 лет назад
Родитель
Сommit
022e8e2cb3

+ 41 - 0
packages/ckeditor5-table/src/tableselection.js

@@ -99,6 +99,21 @@ export default class TableSelection extends Plugin {
 
 		selection.on( 'change:range', () => this._clearSelectionOnExternalChange( selection ) );
 
+		this.listenTo( editor.editing.view.document, 'copy', ( evt, data ) => {
+			if ( !this.hasMultiCellSelection ) {
+				return;
+			}
+
+			const dataTransfer = data.dataTransfer;
+
+			data.preventDefault();
+			evt.stop();
+
+			const content = editor.data.toView( this.getSelectedTableAsFragment() );
+
+			editor.editing.view.document.fire( 'clipboardOutput', { dataTransfer, content, method: evt.name } );
+		}, { priority: 'normal' } );
+
 		this.listenTo( editor.editing.view.document, 'cut', ( evt, data ) => {
 			if ( this.hasMultiCellSelection ) {
 				data.preventDefault();
@@ -190,6 +205,32 @@ export default class TableSelection extends Plugin {
 		this._endElement = undefined;
 	}
 
+	getSelectedTableAsFragment() {
+		return this.editor.model.change( writer => {
+			const fragment = writer.createDocumentFragment();
+
+			const table = writer.createElement( 'table' );
+
+			writer.insert( table, fragment, 0 );
+
+			const rowsMap = new Map();
+
+			for ( const tableCell of this.getSelectedTableCells() ) {
+				const row = tableCell.parent;
+
+				if ( !rowsMap.has( row ) ) {
+					const newRow = row._clone();
+					writer.append( newRow, table );
+					rowsMap.set( row, newRow );
+				}
+
+				writer.append( tableCell._clone( true ), rowsMap.get( row ) );
+			}
+
+			return fragment;
+		} );
+	}
+
 	/**
 	 * Returns iterator for selected table cells.
 	 *

+ 2 - 2
packages/ckeditor5-table/tests/tableselection-clipboard.js

@@ -55,8 +55,8 @@ describe( 'table selection', () => {
 
 					expect( data.content ).is.instanceOf( ViewDocumentFragment );
 					expect( stringifyView( data.content ) ).to.equal( viewTable( [
-						[ '22', '23' ],
-						[ '32', '33' ]
+						[ '12', '13' ],
+						[ '22', '23' ]
 					] ) );
 
 					done();