removecolumncommand.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/removecolumncommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import TableWalker from '../tablewalker';
  10. import { getColumnIndexes, getSelectionAffectedTableCells } from '../utils';
  11. import { findAncestor } from './utils';
  12. /**
  13. * The remove column command.
  14. *
  15. * The command is registered by {@link module:table/tableediting~TableEditing} as the `'removeTableColumn'` editor command.
  16. *
  17. * To remove the column containing the selected cell, execute the command:
  18. *
  19. * editor.execute( 'removeTableColumn' );
  20. *
  21. * @extends module:core/command~Command
  22. */
  23. export default class RemoveColumnCommand extends Command {
  24. /**
  25. * @inheritDoc
  26. */
  27. refresh() {
  28. const selectedCells = getSelectionAffectedTableCells( this.editor.model.document.selection );
  29. const firstCell = selectedCells[ 0 ];
  30. if ( firstCell ) {
  31. const table = findAncestor( 'table', firstCell );
  32. const tableColumnCount = this.editor.plugins.get( 'TableUtils' ).getColumns( table );
  33. const { first, last } = getColumnIndexes( selectedCells );
  34. this.isEnabled = last - first < ( tableColumnCount - 1 );
  35. } else {
  36. this.isEnabled = false;
  37. }
  38. }
  39. /**
  40. * @inheritDoc
  41. */
  42. execute() {
  43. const [ firstCell, lastCell ] = getBoundaryCells( this.editor.model.document.selection );
  44. const table = firstCell.parent.parent;
  45. // Cache the table before removing or updating colspans.
  46. const tableMap = [ ...new TableWalker( table ) ];
  47. // Store column indexes of removed columns.
  48. const removedColumnIndexes = {
  49. first: tableMap.find( value => value.cell === firstCell ).column,
  50. last: tableMap.find( value => value.cell === lastCell ).column
  51. };
  52. const cellToFocus = getCellToFocus( tableMap, firstCell, lastCell, removedColumnIndexes );
  53. this.editor.model.change( writer => {
  54. const columnsToRemove = removedColumnIndexes.last - removedColumnIndexes.first + 1;
  55. this.editor.plugins.get( 'TableUtils' ).removeColumns( table, {
  56. at: removedColumnIndexes.first,
  57. columns: columnsToRemove
  58. } );
  59. writer.setSelection( writer.createPositionAt( cellToFocus, 0 ) );
  60. } );
  61. }
  62. }
  63. // Returns a proper table cell to focus after removing a column.
  64. // - selection is on last table cell it will return previous cell.
  65. function getCellToFocus( tableMap, firstCell, lastCell, removedColumnIndexes ) {
  66. const colspan = parseInt( lastCell.getAttribute( 'colspan' ) || 1 );
  67. // If the table cell is spanned over 2+ columns - it will be truncated so the selection should
  68. // stay in that cell.
  69. if ( colspan > 1 ) {
  70. return lastCell;
  71. }
  72. // Normally, look for the cell in the same row that precedes the first cell to put selection there ("column on the left").
  73. // If the deleted column is the first column of the table, there will be no predecessor: use the cell
  74. // from the column that follows then (also in the same row).
  75. else if ( firstCell.previousSibling || lastCell.nextSibling ) {
  76. return lastCell.nextSibling || firstCell.previousSibling;
  77. }
  78. // It can happen that table cells have no siblings in a row, for instance, when there are row spans
  79. // in the table (in the previous row). Then just look for the closest cell that is in a column
  80. // that will not be removed to put the selection there.
  81. else {
  82. // Look for any cell in a column that precedes the first removed column.
  83. if ( removedColumnIndexes.first ) {
  84. return tableMap.reverse().find( ( { column } ) => {
  85. return column < removedColumnIndexes.first;
  86. } ).cell;
  87. }
  88. // If the first removed column is the first column of the table, then
  89. // look for any cell that is in a column that follows the last removed column.
  90. else {
  91. return tableMap.reverse().find( ( { column } ) => {
  92. return column > removedColumnIndexes.last;
  93. } ).cell;
  94. }
  95. }
  96. }
  97. // Returns helper object returning the first and the last cell contained in given selection, based on DOM order.
  98. function getBoundaryCells( selection ) {
  99. const referenceCells = getSelectionAffectedTableCells( selection );
  100. const firstCell = referenceCells[ 0 ];
  101. const lastCell = referenceCells.pop();
  102. const returnValue = [ firstCell, lastCell ];
  103. return firstCell.isBefore( lastCell ) ? returnValue : returnValue.reverse();
  104. }