mergecellscommand.js 4.2 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/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. // Update target cell dimensions.
  45. const { mergeWidth, mergeHeight } = getMergeDimensions( firstTableCell, selectedTableCells, tableUtils );
  46. updateNumericAttribute( 'colspan', mergeWidth, firstTableCell, writer );
  47. updateNumericAttribute( 'rowspan', mergeHeight, firstTableCell, writer );
  48. for ( const tableCell of selectedTableCells ) {
  49. mergeTableCells( tableCell, firstTableCell, writer );
  50. }
  51. const table = findAncestor( 'table', firstTableCell );
  52. // Remove rows and columns that become empty (have no anchored cells).
  53. removeEmptyRowsColumns( table, tableUtils, writer.batch );
  54. writer.setSelection( firstTableCell, 'in' );
  55. } );
  56. }
  57. }
  58. // Merges two table cells. It will ensure that after merging cells with empty paragraphs the resulting table cell will only have one
  59. // paragraph. If one of the merged table cells is empty, the merged table cell will have contents of the non-empty table cell.
  60. // If both are empty, the merged table cell will have only one empty paragraph.
  61. //
  62. // @param {module:engine/model/element~Element} cellBeingMerged
  63. // @param {module:engine/model/element~Element} targetCell
  64. // @param {module:engine/model/writer~Writer} writer
  65. function mergeTableCells( cellBeingMerged, targetCell, writer ) {
  66. if ( !isEmpty( cellBeingMerged ) ) {
  67. if ( isEmpty( targetCell ) ) {
  68. writer.remove( writer.createRangeIn( targetCell ) );
  69. }
  70. writer.move( writer.createRangeIn( cellBeingMerged ), writer.createPositionAt( targetCell, 'end' ) );
  71. }
  72. // Remove merged table cell.
  73. writer.remove( cellBeingMerged );
  74. }
  75. // Checks if the passed table cell contains an empty paragraph.
  76. //
  77. // @param {module:engine/model/element~Element} tableCell
  78. // @returns {Boolean}
  79. function isEmpty( tableCell ) {
  80. return tableCell.childCount == 1 && tableCell.getChild( 0 ).is( 'paragraph' ) && tableCell.getChild( 0 ).isEmpty;
  81. }
  82. function getMergeDimensions( firstTableCell, selectedTableCells, tableUtils ) {
  83. let maxWidthOffset = 0;
  84. let maxHeightOffset = 0;
  85. for ( const tableCell of selectedTableCells ) {
  86. const { row, column } = tableUtils.getCellLocation( tableCell );
  87. maxWidthOffset = getMaxOffset( tableCell, column, maxWidthOffset, 'colspan' );
  88. maxHeightOffset = getMaxOffset( tableCell, row, maxHeightOffset, 'rowspan' );
  89. }
  90. // Update table cell span attribute and merge set selection on a merged contents.
  91. const { row: firstCellRow, column: firstCellColumn } = tableUtils.getCellLocation( firstTableCell );
  92. const mergeWidth = maxWidthOffset - firstCellColumn;
  93. const mergeHeight = maxHeightOffset - firstCellRow;
  94. return { mergeWidth, mergeHeight };
  95. }
  96. function getMaxOffset( tableCell, start, currentMaxOffset, which ) {
  97. const dimensionValue = parseInt( tableCell.getAttribute( which ) || 1 );
  98. return Math.max( currentMaxOffset, start + dimensionValue );
  99. }