removecolumncommand.js 4.3 KB

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