浏览代码

Fix: Remove empty row in MergeCellCommand.

Maciej Gołaszewski 7 年之前
父节点
当前提交
d1ed509f29

+ 32 - 0
packages/ckeditor5-table/src/commands/mergecellcommand.js

@@ -11,6 +11,7 @@ import Command from '@ckeditor/ckeditor5-core/src/command';
 import Position from '@ckeditor/ckeditor5-engine/src/model/position';
 import Range from '@ckeditor/ckeditor5-engine/src/model/range';
 import TableWalker from '../tablewalker';
+import { updateNumericAttribute } from './utils';
 
 /**
  * The merge cell command.
@@ -80,6 +81,13 @@ export default class MergeCellCommand extends Command {
 			const cellToRemove = isMergeNext ? cellToMerge : tableCell;
 
 			writer.move( Range.createIn( cellToRemove ), Position.createAt( cellToExpand, 'end' ) );
+
+			let rowToCheck;
+
+			if ( !this.isHorizontal ) {
+				rowToCheck = checkEmptyRows( cellToRemove );
+			}
+
 			writer.remove( cellToRemove );
 
 			const spanAttribute = this.isHorizontal ? 'colspan' : 'rowspan';
@@ -89,6 +97,22 @@ export default class MergeCellCommand extends Command {
 			writer.setAttribute( spanAttribute, cellSpan + cellToMergeSpan, cellToExpand );
 
 			writer.setSelection( Range.createIn( cellToExpand ) );
+
+			if ( rowToCheck ) {
+				const table = rowToCheck.parent;
+
+				const removedRowIndex = table.getChildIndex( rowToCheck );
+
+				for ( const { cell, row, rowspan } of new TableWalker( table, { endRow: removedRowIndex } ) ) {
+					const overlapsRemovedRow = row + rowspan - 1 >= removedRowIndex;
+
+					if ( overlapsRemovedRow ) {
+						updateNumericAttribute( 'rowspan', rowspan - 1, cell, writer );
+					}
+				}
+
+				writer.remove( rowToCheck );
+			}
 		} );
 	}
 
@@ -182,3 +206,11 @@ function getVerticalCell( tableCell, direction ) {
 
 	return cellToMergeData && cellToMergeData.cell;
 }
+
+function checkEmptyRows( tableCell ) {
+	const tableRow = tableCell.parent;
+
+	if ( tableRow.childCount == 1 ) {
+		return tableRow;
+	}
+}

+ 16 - 1
packages/ckeditor5-table/tests/commands/mergecellcommand.js

@@ -380,7 +380,7 @@ describe( 'MergeCellCommand', () => {
 			} );
 		} );
 
-		describe( 'execute()', () => {
+		describe.only( 'execute()', () => {
 			it( 'should merge table cells ', () => {
 				setData( model, modelTable( [
 					[ '00', '01[]' ],
@@ -394,6 +394,21 @@ describe( 'MergeCellCommand', () => {
 					[ '10' ]
 				] ) );
 			} );
+
+			it( 'should remove empty row if merging table cells ', () => {
+				setData( model, modelTable( [
+					[ { rowspan: 2, contents: '00' }, '01[]', { rowspan: 3, contents: '02' } ],
+					[ '11' ],
+					[ '20', '21' ]
+				] ) );
+
+				command.execute();
+
+				expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
+					[ '00', '[0111]', { rowspan: 2, contents: '02' } ],
+					[ '20', '21' ]
+				] ) );
+			} );
 		} );
 	} );