mergecellscommand.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/mergecellscommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import TableUtils from '../tableutils';
  10. import { getSelectedTableCells, isSelectionRectangular } from '../utils/selection';
  11. import { findAncestor, updateNumericAttribute } from '../utils/common';
  12. import { removeEmptyRowsColumns } from '../utils/structure';
  13. /**
  14. * The merge cells command.
  15. *
  16. * The command is registered by {@link module:table/tableediting~TableEditing} as the `'mergeTableCells'` editor command.
  17. *
  18. * For example, to merge selected table cells:
  19. *
  20. * editor.execute( 'mergeTableCells' );
  21. *
  22. * @extends module:core/command~Command
  23. */
  24. export default class MergeCellsCommand extends Command {
  25. /**
  26. * @inheritDoc
  27. */
  28. refresh() {
  29. const selectedTableCells = getSelectedTableCells( this.editor.model.document.selection );
  30. this.isEnabled = isSelectionRectangular( selectedTableCells, this.editor.plugins.get( TableUtils ) );
  31. }
  32. /**
  33. * Executes the command.
  34. *
  35. * @fires execute
  36. */
  37. execute() {
  38. const model = this.editor.model;
  39. const tableUtils = this.editor.plugins.get( TableUtils );
  40. model.change( writer => {
  41. const selectedTableCells = getSelectedTableCells( model.document.selection );
  42. // All cells will be merged into the first one.
  43. const firstTableCell = selectedTableCells.shift();
  44. // Set the selection in cell that other cells are being merged to prevent model-selection-range-intersects error in undo.
  45. // See https://github.com/ckeditor/ckeditor5/issues/6634.
  46. // May be fixed by: https://github.com/ckeditor/ckeditor5/issues/6639.
  47. writer.setSelection( firstTableCell, 0 );
  48. // Update target cell dimensions.
  49. const { mergeWidth, mergeHeight } = getMergeDimensions( firstTableCell, selectedTableCells, tableUtils );
  50. updateNumericAttribute( 'colspan', mergeWidth, firstTableCell, writer );
  51. updateNumericAttribute( 'rowspan', mergeHeight, firstTableCell, writer );
  52. for ( const tableCell of selectedTableCells ) {
  53. mergeTableCells( tableCell, firstTableCell, writer );
  54. }
  55. const table = findAncestor( 'table', firstTableCell );
  56. // Remove rows and columns that become empty (have no anchored cells).
  57. removeEmptyRowsColumns( table, tableUtils, writer.batch );
  58. writer.setSelection( firstTableCell, 'in' );
  59. } );
  60. }
  61. }
  62. // Merges two table cells. It will ensure that after merging cells with empty paragraphs the resulting table cell will only have one
  63. // paragraph. If one of the merged table cells is empty, the merged table cell will have contents of the non-empty table cell.
  64. // If both are empty, the merged table cell will have only one empty paragraph.
  65. //
  66. // @param {module:engine/model/element~Element} cellBeingMerged
  67. // @param {module:engine/model/element~Element} targetCell
  68. // @param {module:engine/model/writer~Writer} writer
  69. function mergeTableCells( cellBeingMerged, targetCell, writer ) {
  70. if ( !isEmpty( cellBeingMerged ) ) {
  71. if ( isEmpty( targetCell ) ) {
  72. writer.remove( writer.createRangeIn( targetCell ) );
  73. }
  74. writer.move( writer.createRangeIn( cellBeingMerged ), writer.createPositionAt( targetCell, 'end' ) );
  75. }
  76. // Remove merged table cell.
  77. writer.remove( cellBeingMerged );
  78. }
  79. // Checks if the passed table cell contains an empty paragraph.
  80. //
  81. // @param {module:engine/model/element~Element} tableCell
  82. // @returns {Boolean}
  83. function isEmpty( tableCell ) {
  84. return tableCell.childCount == 1 && tableCell.getChild( 0 ).is( 'paragraph' ) && tableCell.getChild( 0 ).isEmpty;
  85. }
  86. function getMergeDimensions( firstTableCell, selectedTableCells, tableUtils ) {
  87. let maxWidthOffset = 0;
  88. let maxHeightOffset = 0;
  89. for ( const tableCell of selectedTableCells ) {
  90. const { row, column } = tableUtils.getCellLocation( tableCell );
  91. maxWidthOffset = getMaxOffset( tableCell, column, maxWidthOffset, 'colspan' );
  92. maxHeightOffset = getMaxOffset( tableCell, row, maxHeightOffset, 'rowspan' );
  93. }
  94. // Update table cell span attribute and merge set selection on a merged contents.
  95. const { row: firstCellRow, column: firstCellColumn } = tableUtils.getCellLocation( firstTableCell );
  96. const mergeWidth = maxWidthOffset - firstCellColumn;
  97. const mergeHeight = maxHeightOffset - firstCellRow;
  98. return { mergeWidth, mergeHeight };
  99. }
  100. function getMaxOffset( tableCell, start, currentMaxOffset, which ) {
  101. const dimensionValue = parseInt( tableCell.getAttribute( which ) || 1 );
  102. return Math.max( currentMaxOffset, start + dimensionValue );
  103. }