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

MergeCellsCommand should provide the same output if selection is reversed.

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

+ 2 - 2
packages/ckeditor5-table/src/commands/mergecellscommand.js

@@ -52,11 +52,11 @@ export default class MergeCellsCommand extends Command {
 		const tableUtils = this.editor.plugins.get( TableUtils );
 
 		model.change( writer => {
-			const selectedTableCells = [ ... this.editor.model.document.selection.getRanges() ].map( range => range.start.nodeAfter );
+			const selectedTableCells = getSelectionAffectedTableCells( model.document.selection );
 
 			const firstTableCell = selectedTableCells.shift();
 
-			// TODO: this shouldn't be necessary (right now the selection could overlap existing.
+			// This prevents the "model-selection-range-intersects" error, caused by removing row selected cells.
 			writer.setSelection( firstTableCell, 'in' );
 
 			const { row, column } = tableUtils.getCellLocation( firstTableCell );

+ 56 - 9
packages/ckeditor5-table/tests/commands/mergecellscommand.js

@@ -14,7 +14,7 @@ import TableEditing from '../../src/tableediting';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 
 describe( 'MergeCellsCommand', () => {
-	let editor, model, command, root;
+	let editor, model, command, root, tableSelection;
 
 	beforeEach( async () => {
 		editor = await ModelTestEditor.create( {
@@ -23,6 +23,7 @@ describe( 'MergeCellsCommand', () => {
 
 		model = editor.model;
 		root = model.document.getRoot( 'main' );
+		tableSelection = editor.plugins.get( TableSelection );
 
 		command = new MergeCellsCommand( editor );
 	} );
@@ -227,12 +228,9 @@ describe( 'MergeCellsCommand', () => {
 				[ '10', '11' ]
 			], { headingRows: 1 } ) );
 
-			const tableSelection = editor.plugins.get( TableSelection );
-			const modelRoot = model.document.getRoot();
-
 			tableSelection._setCellSelection(
-				modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
-				modelRoot.getNodeByPath( [ 0, 1, 0 ] )
+				root.getNodeByPath( [ 0, 0, 0 ] ),
+				root.getNodeByPath( [ 0, 1, 0 ] )
 			);
 
 			expect( command.isEnabled ).to.be.false;
@@ -245,9 +243,10 @@ describe( 'MergeCellsCommand', () => {
 				[ '[]00', '01' ]
 			] ) );
 
-			selectNodes( [
-				[ 0, 0, 0 ], [ 0, 0, 1 ]
-			] );
+			tableSelection._setCellSelection(
+				root.getNodeByPath( [ 0, 0, 0 ] ),
+				root.getNodeByPath( [ 0, 0, 1 ] )
+			);
 
 			command.execute();
 
@@ -256,6 +255,54 @@ describe( 'MergeCellsCommand', () => {
 			] ) );
 		} );
 
+		it( 'should merge selection with a cell with rowspan in the selection', () => {
+			setData( model, modelTable( [
+				[ '[]00', '01', '02' ],
+				[ '10', { contents: '11', rowspan: 2 }, '12' ],
+				[ '20', '22' ]
+			] ) );
+
+			tableSelection._setCellSelection(
+				root.getNodeByPath( [ 0, 1, 0 ] ),
+				root.getNodeByPath( [ 0, 2, 1 ] )
+			);
+
+			command.execute();
+
+			assertEqualMarkup( getData( model ), modelTable( [
+				[ '00', '01', '02' ],
+				[ {
+					colspan: 3,
+					contents: '<paragraph>[10</paragraph><paragraph>11</paragraph><paragraph>12</paragraph>' +
+						'<paragraph>20</paragraph><paragraph>22]</paragraph>'
+				} ]
+			] ) );
+		} );
+
+		it( 'should merge selection with a cell with rowspan in the selection (reverse selection)', () => {
+			setData( model, modelTable( [
+				[ '[]00', '01', '02' ],
+				[ '10', { contents: '11', rowspan: 2 }, '12' ],
+				[ '20', '22' ]
+			] ) );
+
+			tableSelection._setCellSelection(
+				root.getNodeByPath( [ 0, 2, 1 ] ),
+				root.getNodeByPath( [ 0, 1, 0 ] )
+			);
+
+			command.execute();
+
+			assertEqualMarkup( getData( model ), modelTable( [
+				[ '00', '01', '02' ],
+				[ {
+					colspan: 3,
+					contents: '<paragraph>[10</paragraph><paragraph>11</paragraph><paragraph>12</paragraph>' +
+						'<paragraph>20</paragraph><paragraph>22]</paragraph>'
+				} ]
+			] ) );
+		} );
+
 		it( 'should merge table cells - extend colspan attribute', () => {
 			setData( model, modelTable( [
 				[ { colspan: 2, contents: '00' }, '02', '03' ],