浏览代码

Add tests for simple pasting scenarios when pasted table exceeds selection.

Maciej Gołaszewski 5 年之前
父节点
当前提交
67761ce109
共有 2 个文件被更改,包括 29 次插入20 次删除
  1. 10 7
      packages/ckeditor5-table/src/tableclipboard.js
  2. 19 13
      packages/ckeditor5-table/tests/tableclipboard.js

+ 10 - 7
packages/ckeditor5-table/src/tableclipboard.js

@@ -12,6 +12,7 @@ import TableSelection from './tableselection';
 import { getColumnIndexes, getRowIndexes, isSelectionRectangular } from './utils';
 import TableWalker from './tablewalker';
 import { findAncestor, updateNumericAttribute } from './commands/utils';
+import { cropTableToDimensions } from './tableselection/croptable';
 
 /**
  * This plugin adds support for copying/cutting/pasting fragments of tables.
@@ -102,7 +103,8 @@ export default class TableClipboard extends Plugin {
 			return;
 		}
 
-		const table = getTable( content );
+		// We might need to crop table before inserting.
+		let table = getTable( content );
 
 		if ( !table ) {
 			return;
@@ -124,6 +126,7 @@ export default class TableClipboard extends Plugin {
 		const insertHeight = tableUtils.getRows( table );
 		const insertWidth = tableUtils.getColumns( table );
 		const contentTable = findAncestor( 'table', selectedTableCells[ 0 ] );
+
 		if ( !isSelectionRectangular( this.editor.model.document.selection, tableUtils ) ) {
 			// @if CK_DEBUG // console.log( 'Selection is not rectangular (non-mergeable) - pasting disabled.' );
 
@@ -136,15 +139,15 @@ export default class TableClipboard extends Plugin {
 			return;
 		}
 
-		if ( selectionHeight < insertHeight || selectionWidth < insertWidth ) {
-			// @if CK_DEBUG // console.log( 'Pasted table extends selection area.' );
-
-			return;
-		}
-
 		const model = this.editor.model;
 
 		model.change( writer => {
+			if ( selectionHeight < insertHeight || selectionWidth < insertWidth ) {
+				// @if CK_DEBUG // console.log( 'Pasted table extends selection area.' );
+
+				table = cropTableToDimensions( table, 0, 0, selectionHeight - 1, selectionWidth - 1, tableUtils, writer );
+			}
+
 			const insertionMap = new Map();
 
 			for ( const { column, row, cell } of new TableWalker( table ) ) {

+ 19 - 13
packages/ckeditor5-table/tests/tableclipboard.js

@@ -1199,8 +1199,9 @@ describe( 'table clipboard', () => {
 						);
 
 						pasteTable( [
-							[ 'aa', 'ab' ],
-							[ 'ba', 'bb' ]
+							[ 'aa', 'ab', 'ac' ],
+							[ 'ba', 'bb', 'bc' ],
+							[ 'ca', 'cb', 'cc' ]
 						] );
 
 						assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
@@ -1225,8 +1226,9 @@ describe( 'table clipboard', () => {
 						);
 
 						pasteTable( [
-							[ 'aa', 'ab' ],
-							[ 'ba', 'bb' ]
+							[ 'aa', 'ab', 'ac' ],
+							[ 'ba', 'bb', 'bc' ],
+							[ 'ca', 'cb', 'cc' ]
 						] );
 
 						assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
@@ -1244,14 +1246,16 @@ describe( 'table clipboard', () => {
 						] );
 					} );
 
-					it( 'handles simple row paste to a simple row fragment - in the middle of a table', () => {
+					it( 'handles paste to a simple row fragment - in the middle of a table', () => {
 						tableSelection.setCellSelection(
 							modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
 							modelRoot.getNodeByPath( [ 0, 1, 2 ] )
 						);
 
 						pasteTable( [
-							[ 'aa', 'ab' ]
+							[ 'aa', 'ab', 'ac' ],
+							[ 'ba', 'bb', 'bc' ],
+							[ 'ca', 'cb', 'cc' ]
 						] );
 
 						assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
@@ -1269,15 +1273,16 @@ describe( 'table clipboard', () => {
 						] );
 					} );
 
-					it( 'handles simple column paste to a simple column fragment - in the middle of a table', () => {
+					it( 'handles paste to a simple column fragment - in the middle of a table', () => {
 						tableSelection.setCellSelection(
 							modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
 							modelRoot.getNodeByPath( [ 0, 2, 1 ] )
 						);
 
 						pasteTable( [
-							[ 'aa' ],
-							[ 'ba' ]
+							[ 'aa', 'ab', 'ac' ],
+							[ 'ba', 'bb', 'bc' ],
+							[ 'ca', 'cb', 'cc' ]
 						] );
 
 						assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
@@ -1302,10 +1307,11 @@ describe( 'table clipboard', () => {
 						);
 
 						pasteTable( [
-							[ 'aa', 'ab', 'ac', 'ad' ],
-							[ 'ba', 'bb', 'bc', 'bd' ],
-							[ 'ca', 'cb', 'cc', 'cd' ],
-							[ 'da', 'db', 'dc', 'dd' ]
+							[ 'aa', 'ab', 'ac', 'ad', 'ae' ],
+							[ 'ba', 'bb', 'bc', 'bd', 'be' ],
+							[ 'ca', 'cb', 'cc', 'cd', 'ce' ],
+							[ 'da', 'db', 'dc', 'dd', 'de' ],
+							[ 'ea', 'eb', 'ec', 'ed', 'ee' ]
 						] );
 
 						assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [