removerowcommand.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module table/commands/removerowcommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import TableWalker from '../tablewalker';
  10. import { findAncestor, updateNumericAttribute } from './utils';
  11. /**
  12. * The remove row command.
  13. *
  14. * The command is registered by {@link module:table/tableediting~TableEditing} as the `'removeTableRow'` editor command.
  15. *
  16. * To remove the row containing the selected cell, execute the command:
  17. *
  18. * editor.execute( 'removeTableRow' );
  19. *
  20. * @extends module:core/command~Command
  21. */
  22. export default class RemoveRowCommand extends Command {
  23. /**
  24. * @inheritDoc
  25. */
  26. refresh() {
  27. const firstCell = this._getReferenceCells().next().value;
  28. this.isEnabled = !!firstCell && firstCell.parent.parent.childCount > 1;
  29. }
  30. /**
  31. * @inheritDoc
  32. */
  33. execute() {
  34. const referenceCells = Array.from( this._getReferenceCells() );
  35. const removedRowIndexes = {
  36. first: referenceCells[ 0 ].parent.index,
  37. last: referenceCells[ referenceCells.length - 1 ].parent.index
  38. };
  39. const firstCell = referenceCells[ 0 ];
  40. const table = firstCell.parent.parent;
  41. const tableMap = [ ...new TableWalker( table, { endRow: removedRowIndexes.last } ) ];
  42. this.editor.model.change( writer => {
  43. // Temporary workaround to avoid the "model-selection-range-intersects" error.
  44. writer.setSelection( writer.createSelection( table, 'on' ) );
  45. const firstCellData = tableMap.find( value => value.cell === firstCell );
  46. const columnToFocus = firstCellData.column;
  47. let cellToFocus;
  48. for ( let i = removedRowIndexes.last; i >= removedRowIndexes.first; i-- ) {
  49. const removedRowIndex = i;
  50. this._removeRow( removedRowIndex, table, writer, tableMap );
  51. cellToFocus = getCellToFocus( table, removedRowIndex, columnToFocus );
  52. }
  53. writer.setSelection( writer.createPositionAt( cellToFocus, 0 ) );
  54. } );
  55. }
  56. /**
  57. * @private
  58. */
  59. _removeRow( removedRowIndex, table, writer, tableMap ) {
  60. const cellsToMove = new Map();
  61. const tableRow = table.getChild( removedRowIndex );
  62. const headingRows = table.getAttribute( 'headingRows' ) || 0;
  63. if ( headingRows && removedRowIndex <= headingRows ) {
  64. updateNumericAttribute( 'headingRows', headingRows - 1, table, writer, 0 );
  65. }
  66. // Get cells from removed row that are spanned over multiple rows.
  67. tableMap
  68. .filter( ( { row, rowspan } ) => row === removedRowIndex && rowspan > 1 )
  69. .forEach( ( { column, cell, rowspan } ) => cellsToMove.set( column, { cell, rowspanToSet: rowspan - 1 } ) );
  70. // Reduce rowspan on cells that are above removed row and overlaps removed row.
  71. tableMap
  72. .filter( ( { row, rowspan } ) => row <= removedRowIndex - 1 && row + rowspan > removedRowIndex )
  73. .forEach( ( { cell, rowspan } ) => updateNumericAttribute( 'rowspan', rowspan - 1, cell, writer ) );
  74. // Move cells to another row.
  75. const targetRow = removedRowIndex + 1;
  76. const tableWalker = new TableWalker( table, { includeSpanned: true, startRow: targetRow, endRow: targetRow } );
  77. let previousCell;
  78. for ( const { row, column, cell } of [ ...tableWalker ] ) {
  79. if ( cellsToMove.has( column ) ) {
  80. const { cell: cellToMove, rowspanToSet } = cellsToMove.get( column );
  81. const targetPosition = previousCell ?
  82. writer.createPositionAfter( previousCell ) :
  83. writer.createPositionAt( table.getChild( row ), 0 );
  84. writer.move( writer.createRangeOn( cellToMove ), targetPosition );
  85. updateNumericAttribute( 'rowspan', rowspanToSet, cellToMove, writer );
  86. previousCell = cellToMove;
  87. }
  88. else {
  89. previousCell = cell;
  90. }
  91. }
  92. writer.remove( tableRow );
  93. }
  94. /**
  95. * Returns cells that are selected and are a reference to removing rows.
  96. *
  97. * @private
  98. * @returns {Iterable.<module:engine/model/element~Element>} Generates `tableCell` elements.
  99. */
  100. * _getReferenceCells() {
  101. const plugins = this.editor.plugins;
  102. if ( plugins.has( 'TableSelection' ) ) {
  103. const selectedCells = plugins.get( 'TableSelection' ).getSelectedTableCells();
  104. if ( selectedCells ) {
  105. for ( const cell of selectedCells ) {
  106. yield cell;
  107. }
  108. return;
  109. }
  110. }
  111. const selection = this.editor.model.document.selection;
  112. yield findAncestor( 'tableCell', selection.getFirstPosition() );
  113. }
  114. }
  115. // Returns a cell that should be focused before removing the row, belonging to the same column as the currently focused cell.
  116. function getCellToFocus( table, removedRowIndex, columnToFocus ) {
  117. const row = table.getChild( removedRowIndex );
  118. // Default to first table cell.
  119. let cellToFocus = row.getChild( 0 );
  120. let column = 0;
  121. for ( const tableCell of row.getChildren() ) {
  122. if ( column > columnToFocus ) {
  123. return cellToFocus;
  124. }
  125. cellToFocus = tableCell;
  126. column += parseInt( tableCell.getAttribute( 'colspan' ) || 1 );
  127. }
  128. }