8
0
Quellcode durchsuchen

Internal: Column remove command should not be active if your selection contains all the columns in the table.

Marek Lewandowski vor 5 Jahren
Ursprung
Commit
29f8c9017f

+ 13 - 2
packages/ckeditor5-table/src/commands/removecolumncommand.js

@@ -28,10 +28,21 @@ export default class RemoveColumnCommand extends Command {
 	 * @inheritDoc
 	 * @inheritDoc
 	 */
 	 */
 	refresh() {
 	refresh() {
-		const tableUtils = this.editor.plugins.get( 'TableUtils' );
 		const firstCell = this._getReferenceCells().next().value;
 		const firstCell = this._getReferenceCells().next().value;
 
 
-		this.isEnabled = !!firstCell && tableUtils.getColumns( firstCell.parent.parent ) > 1;
+		if ( firstCell ) {
+			const table = firstCell.parent.parent;
+			const tableUtils = this.editor.plugins.get( 'TableUtils' );
+			const tableColumnCount = table && tableUtils.getColumns( table );
+
+			const tableMap = [ ...new TableWalker( table ) ];
+			const selectedCells = Array.from( this._getReferenceCells() );
+			const columnIndexes = tableMap.filter( entry => selectedCells.includes( entry.cell ) ).map( el => el.column );
+
+			this.isEnabled = Math.max.apply( null, columnIndexes ) - Math.min.apply( null, columnIndexes ) < ( tableColumnCount - 1 );
+		} else {
+			this.isEnabled = false;
+		}
 	}
 	}
 
 
 	/**
 	/**

+ 16 - 2
packages/ckeditor5-table/tests/commands/removecolumncommand.js

@@ -46,8 +46,8 @@ describe( 'RemoveColumnCommand', () => {
 
 
 		it( 'should be true if selection contains multiple cells', () => {
 		it( 'should be true if selection contains multiple cells', () => {
 			setData( model, modelTable( [
 			setData( model, modelTable( [
-				[ '00', '01' ],
-				[ '10', '11' ]
+				[ '00', '01', '02' ],
+				[ '10', '11', '12' ]
 			] ) );
 			] ) );
 
 
 			const tableSelection = editor.plugins.get( TableSelection );
 			const tableSelection = editor.plugins.get( TableSelection );
@@ -68,6 +68,20 @@ describe( 'RemoveColumnCommand', () => {
 			expect( command.isEnabled ).to.be.false;
 			expect( command.isEnabled ).to.be.false;
 		} );
 		} );
 
 
+		it( 'should be false if all columns are selected', () => {
+			setData( model, modelTable( [
+				[ '00', '01', '02' ],
+				[ '10', '11', '12' ]
+			] ) );
+
+			const tableSelection = editor.plugins.get( TableSelection );
+			const modelRoot = model.document.getRoot();
+			tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
+			tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 2 ] ) );
+
+			expect( command.isEnabled ).to.be.false;
+		} );
+
 		it( 'should be false if selection is outside a table', () => {
 		it( 'should be false if selection is outside a table', () => {
 			setData( model, '<paragraph>11[]</paragraph>' );
 			setData( model, '<paragraph>11[]</paragraph>' );