removecolumncommand.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 { updateNumericAttribute } from './utils';
  11. import { getSelectionAffectedTableCells } 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 = firstCell.parent.parent;
  32. const tableColumnCount = this.editor.plugins.get( 'TableUtils' ).getColumns( table );
  33. const tableMap = [ ...new TableWalker( table ) ];
  34. const columnIndexes = tableMap.filter( entry => selectedCells.includes( entry.cell ) ).map( el => el.column ).sort();
  35. const minColumnIndex = columnIndexes[ 0 ];
  36. const maxColumnIndex = columnIndexes[ columnIndexes.length - 1 ];
  37. this.isEnabled = maxColumnIndex - minColumnIndex < ( tableColumnCount - 1 );
  38. } else {
  39. this.isEnabled = false;
  40. }
  41. }
  42. /**
  43. * @inheritDoc
  44. */
  45. execute() {
  46. const [ firstCell, lastCell ] = getBoundaryCells( this.editor.model.document.selection );
  47. const table = firstCell.parent.parent;
  48. // Cache the table before removing or updating colspans.
  49. const tableMap = [ ...new TableWalker( table ) ];
  50. // Store column indexes of removed columns.
  51. const removedColumnIndexes = {
  52. first: tableMap.find( value => value.cell === firstCell ).column,
  53. last: tableMap.find( value => value.cell === lastCell ).column
  54. };
  55. const cellsToFocus = getCellToFocus( firstCell, lastCell );
  56. this.editor.model.change( writer => {
  57. // A temporary workaround to avoid the "model-selection-range-intersects" error.
  58. writer.setSelection( writer.createRangeOn( table ) );
  59. adjustHeadingColumns( table, removedColumnIndexes, writer );
  60. for (
  61. let removedColumnIndex = removedColumnIndexes.last;
  62. removedColumnIndex >= removedColumnIndexes.first;
  63. removedColumnIndex--
  64. ) {
  65. for ( const { cell, column, colspan } of tableMap ) {
  66. // If colspaned cell overlaps removed column decrease its span.
  67. if ( column <= removedColumnIndex && colspan > 1 && column + colspan > removedColumnIndex ) {
  68. updateNumericAttribute( 'colspan', colspan - 1, cell, writer );
  69. } else if ( column === removedColumnIndex ) {
  70. // The cell in removed column has colspan of 1.
  71. writer.remove( cell );
  72. }
  73. }
  74. }
  75. writer.setSelection( writer.createPositionAt( cellsToFocus.reverse().filter( item => item != null )[ 0 ], 0 ) );
  76. } );
  77. }
  78. }
  79. // Updates heading columns attribute if removing a row from head section.
  80. function adjustHeadingColumns( table, removedColumnIndexes, writer ) {
  81. const headingColumns = table.getAttribute( 'headingColumns' ) || 0;
  82. if ( headingColumns && removedColumnIndexes.first <= headingColumns ) {
  83. const headingsRemoved = Math.min( headingColumns - 1 /* Other numbers are 0-based */, removedColumnIndexes.last ) -
  84. removedColumnIndexes.first + 1;
  85. writer.setAttribute( 'headingColumns', headingColumns - headingsRemoved, table );
  86. }
  87. }
  88. // Returns a proper table cell to focus after removing a column. It should be a next sibling to selection visually stay in place but:
  89. // - selection is on last table cell it will return previous cell.
  90. // - table cell is spanned over 2+ columns - it will be truncated so the selection should stay in that cell.
  91. function getCellToFocus( firstCell, lastCell ) {
  92. const colspan = parseInt( lastCell.getAttribute( 'colspan' ) || 1 );
  93. if ( colspan > 1 ) {
  94. return [ firstCell, lastCell ];
  95. }
  96. // return lastCell.nextSibling ? lastCell.nextSibling : lastCell.previousSibling;
  97. return [ firstCell.previousSibling, lastCell.nextSibling ];
  98. }
  99. // Returns helper object returning the first and the last cell contained in given selection, based on DOM order.
  100. function getBoundaryCells( selection ) {
  101. const referenceCells = getSelectionAffectedTableCells( selection );
  102. const firstCell = referenceCells[ 0 ];
  103. const lastCell = referenceCells.pop();
  104. const returnValue = [ firstCell, lastCell ];
  105. return firstCell.isBefore( lastCell ) ? returnValue : returnValue.reverse();
  106. }