Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Extract single column removal to own method in remove column command.
Browse files Browse the repository at this point in the history
  • Loading branch information
jodator committed Mar 25, 2020
1 parent 8f6460a commit a9dfb77
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions src/commands/removecolumncommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,40 @@ export default class RemoveColumnCommand extends Command {
removedColumnIndex >= removedColumnIndexes.first;
removedColumnIndex--
) {
for ( const { cell, column, colspan } of new TableWalker( table ) ) {
// If colspaned cell overlaps removed column decrease its span.
if ( column <= removedColumnIndex && colspan > 1 && column + colspan > removedColumnIndex ) {
updateNumericAttribute( 'colspan', colspan - 1, cell, writer );
} else if ( column === removedColumnIndex ) {
const cellRow = cell.parent;

// The cell in removed column has colspan of 1.
writer.remove( cell );

// If the cell was the last one in the row, get rid of the entire row.
// https://github.com/ckeditor/ckeditor5/issues/6429
if ( !cellRow.childCount ) {
writer.remove( cellRow );
}
}
}
this._removeColumn( removedColumnIndex, table, writer );
}

writer.setSelection( writer.createPositionAt( cellToFocus, 0 ) );
} );
}

/**
* Removes a column from the given `table`.
*
* @private
* @param {Number} removedColumnIndex Index of the column that should be removed.
* @param {module:engine/model/element~Element} table
* @param {module:engine/model/writer~Writer} writer
*/
_removeColumn( removedColumnIndex, table, writer ) {
for ( const { cell, column, colspan } of new TableWalker( table ) ) {
// If colspaned cell overlaps removed column decrease its span.
if ( column <= removedColumnIndex && colspan > 1 && column + colspan > removedColumnIndex ) {
updateNumericAttribute( 'colspan', colspan - 1, cell, writer );
} else if ( column === removedColumnIndex ) {
const cellRow = cell.parent;

// The cell in removed column has colspan of 1.
writer.remove( cell );

// If the cell was the last one in the row, get rid of the entire row.
// https://github.com/ckeditor/ckeditor5/issues/6429
if ( !cellRow.childCount ) {
writer.remove( cellRow );
}
}
}
}
}

// Updates heading columns attribute if removing a row from head section.
Expand Down

0 comments on commit a9dfb77

Please sign in to comment.