Browse Source

Internal: Remove row command enabled state for multiple cell selection is now consistent with collapsed caret selection.

Now if you make a selection that contains all the rows, the command state is disabled.
Marek Lewandowski 5 years ago
parent
commit
2f58811a89

+ 13 - 1
packages/ckeditor5-table/src/commands/removerowcommand.js

@@ -30,7 +30,19 @@ export default class RemoveRowCommand extends Command {
 	refresh() {
 		const firstCell = this._getReferenceCells().next().value;
 
-		this.isEnabled = !!firstCell && firstCell.parent.parent.childCount > 1;
+		if ( firstCell ) {
+			const table = firstCell.parent.parent;
+			const tableUtils = this.editor.plugins.get( 'TableUtils' );
+			const tableRowCount = table && tableUtils.getRows( table );
+
+			const tableMap = [ ...new TableWalker( table ) ];
+			const selectedCells = Array.from( this._getReferenceCells() );
+			const rowIndexes = tableMap.filter( entry => selectedCells.includes( entry.cell ) ).map( el => el.row );
+
+			this.isEnabled = Math.max.apply( null, rowIndexes ) - Math.min.apply( null, rowIndexes ) < ( tableRowCount - 1 );
+		} else {
+			this.isEnabled = false;
+		}
 	}
 
 	/**

+ 18 - 1
packages/ckeditor5-table/tests/commands/removerowcommand.js

@@ -43,7 +43,8 @@ describe( 'RemoveRowCommand', () => {
 		it( 'should be true if selection contains multiple cells', () => {
 			setData( model, modelTable( [
 				[ '00', '01' ],
-				[ '10', '11' ]
+				[ '10', '11' ],
+				[ '20', '21' ]
 			] ) );
 
 			const tableSelection = editor.plugins.get( TableSelection );
@@ -64,6 +65,22 @@ describe( 'RemoveRowCommand', () => {
 			expect( command.isEnabled ).to.be.false;
 		} );
 
+		it( 'should be false if all the rows are selected', () => {
+			setData( model, modelTable( [
+				[ '00', '01' ],
+				[ '10', '11' ]
+			] ) );
+
+			const tableSelection = editor.plugins.get( TableSelection );
+			const modelRoot = model.document.getRoot();
+			tableSelection._setCellSelection(
+				modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
+				modelRoot.getNodeByPath( [ 0, 1, 0 ] )
+			);
+
+			expect( command.isEnabled ).to.be.false;
+		} );
+
 		it( 'should be false if selection is outside a table', () => {
 			setData( model, '<paragraph>11[]</paragraph>' );