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

Handle simple table paste over simple selection of same size.

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

+ 32 - 3
packages/ckeditor5-table/src/tableclipboard.js

@@ -10,6 +10,8 @@
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import TableSelection from './tableselection';
 import { getColumnIndexes, getRowIndexes } from './utils';
+import TableWalker from './tablewalker';
+import { findAncestor } from './commands/utils';
 
 /**
  * This plugin adds support for copying/cutting/pasting fragments of tables.
@@ -120,9 +122,36 @@ export default class TableClipboard extends Plugin {
 			const insertHeight = tableUtils.getRows( table );
 			const insertWidth = tableUtils.getColumns( table );
 
-			if ( selectionHeight === insertHeight && selectionWidth === insertHeight ) {
-				// @if CK_DEBUG // console.log( 'Pasted table and selection area are the same.' );
+			const contentTable = findAncestor( 'table', selectedTableCells[ 0 ] );
 
+			if ( selectionHeight === insertHeight && selectionWidth === insertWidth ) {
+				const model = this.editor.model;
+
+				model.change( writer => {
+					const insertionMap = new Map();
+
+					for ( const { column, row, cell } of new TableWalker( table ) ) {
+						insertionMap.set( `${ row }x${ column }`, cell );
+					}
+
+					for ( const { column, row, cell } of new TableWalker( contentTable, {
+						startRow: rowIndexes.first,
+						endRow: rowIndexes.last
+					} ) ) {
+						if ( column < columnIndexes.first || column > columnIndexes.last ) {
+							continue;
+						}
+
+						const toGet = `${ row - rowIndexes.first }x${ column - columnIndexes.first }`;
+
+						const cellToInsert = insertionMap.get( toGet );
+						writer.remove( writer.createRangeIn( cell ) );
+
+						for ( const child of cellToInsert.getChildren() ) {
+							writer.insert( child, cell, 'end' );
+						}
+					}
+				} );
 				return;
 			}
 
@@ -136,7 +165,7 @@ export default class TableClipboard extends Plugin {
 }
 
 function getTable( content ) {
-	for ( const child of content ) {
+	for ( const child of Array.from( content ) ) {
 		if ( child.is( 'table' ) ) {
 			return child;
 		}

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

@@ -408,7 +408,7 @@ describe( 'table clipboard', () => {
 				] ) );
 			} );
 
-			describe( 'pasted table is equal ot selected area', () => {
+			describe( 'pasted table is equal to the selected area', () => {
 				it( 'should be disabled in a readonly mode', () => {
 					editor.isReadOnly = true;
 
@@ -453,7 +453,7 @@ describe( 'table clipboard', () => {
 					] ) );
 				} );
 
-				it( 'pastes simple table to a simple table fragment - at the beginning of a table', () => {
+				it( 'inserts simple table to a simple table fragment - at the beginning of a table', () => {
 					tableSelection._setCellSelection(
 						modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
 						modelRoot.getNodeByPath( [ 0, 1, 1 ] )
@@ -470,6 +470,7 @@ describe( 'table clipboard', () => {
 						[ '20', '21', '22', '23' ],
 						[ '30', '31', '32', '33' ]
 					] ) );
+
 					assertSelectedCells( model, [
 						[ 1, 1, 0, 0 ],
 						[ 1, 1, 0, 0 ],
@@ -478,7 +479,7 @@ describe( 'table clipboard', () => {
 					] );
 				} );
 
-				it( 'pastes simple table to a simple table fragment - at the end of a table', () => {
+				it( 'inserts simple table to a simple table fragment - at the end of a table', () => {
 					tableSelection._setCellSelection(
 						modelRoot.getNodeByPath( [ 0, 2, 2 ] ),
 						modelRoot.getNodeByPath( [ 0, 3, 3 ] )
@@ -503,7 +504,7 @@ describe( 'table clipboard', () => {
 					] );
 				} );
 
-				it( 'pastes simple table to a simple table fragment - in the middle of a table', () => {
+				it( 'inserts simple table to a simple table fragment - in the middle of a table', () => {
 					tableSelection._setCellSelection(
 						modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
 						modelRoot.getNodeByPath( [ 0, 2, 2 ] )
@@ -528,7 +529,7 @@ describe( 'table clipboard', () => {
 					] );
 				} );
 
-				it( 'pastes simple table to a simple table fragment - whole table selected', () => {
+				it( 'inserts simple table to a simple table fragment - whole table selected', () => {
 					tableSelection._setCellSelection(
 						modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
 						modelRoot.getNodeByPath( [ 0, 3, 3 ] )