mergecellcommand.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module table/commands/mergecellcommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  10. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  11. import TableWalker from '../tablewalker';
  12. /**
  13. * The merge cell command.
  14. *
  15. * The command is registered by {@link module:table/tableediting~TableEditing} as `'mergeTableCellRight'`, `'mergeTableCellLeft'`,
  16. * `'mergeTableCellUp'` and `'mergeTableCellDown'` editor commands.
  17. *
  18. * To merge a table cell at the current selection with another cell, execute the command corresponding with the preferred direction.
  19. *
  20. * For example, to merge with a cell to the right:
  21. *
  22. * editor.execute( 'mergeTableCellRight' );
  23. *
  24. * **Note**: If a table cell has a different [`rowspan`](https://www.w3.org/TR/html50/tabular-data.html#attr-tdth-rowspan)
  25. * (for `'mergeTableCellRight'` and `'mergeTableCellLeft'`) or [`colspan`](https://www.w3.org/TR/html50/tabular-data.html#attr-tdth-colspan)
  26. * (for `'mergeTableCellUp'` and `'mergeTableCellDown'`), the command will be disabled.
  27. *
  28. * @extends module:core/command~Command
  29. */
  30. export default class MergeCellCommand extends Command {
  31. /**
  32. * Creates a new `MergeCellCommand` instance.
  33. *
  34. * @param {module:core/editor/editor~Editor} editor The editor on which this command will be used.
  35. * @param {Object} options
  36. * @param {String} options.direction Indicates which cell to merge with the currently selected one.
  37. * Possible values are: `'left'`, `'right'`, `'up'` and `'down'`.
  38. */
  39. constructor( editor, options ) {
  40. super( editor );
  41. /**
  42. * The direction that indicates which cell will be merged with the currently selected one.
  43. *
  44. * @readonly
  45. * @member {String} #direction
  46. */
  47. this.direction = options.direction;
  48. /**
  49. * Whether the merge is horizontal (left/right) or vertical (up/down).
  50. *
  51. * @readonly
  52. * @member {Boolean} #isHorizontal
  53. */
  54. this.isHorizontal = this.direction == 'right' || this.direction == 'left';
  55. }
  56. /**
  57. * @inheritDoc
  58. */
  59. refresh() {
  60. const cellToMerge = this._getMergeableCell();
  61. this.isEnabled = !!cellToMerge;
  62. // In order to check if currently selected cell can be merged with one defined by #direction some computation are done beforehand.
  63. // As such we can cache it as a command's value.
  64. this.value = cellToMerge;
  65. }
  66. /**
  67. * Executes the command.
  68. *
  69. * Depending on the command's {@link #direction} value, it will merge the cell that is to the `'left'`, `'right'`, `'up'` or `'down'`.
  70. *
  71. * @fires execute
  72. */
  73. execute() {
  74. const model = this.editor.model;
  75. const doc = model.document;
  76. const tableCell = doc.selection.getFirstPosition().parent;
  77. const cellToMerge = this.value;
  78. const direction = this.direction;
  79. model.change( writer => {
  80. const isMergeNext = direction == 'right' || direction == 'down';
  81. // The merge mechanism is always the same so sort cells to be merged.
  82. const cellToExpand = isMergeNext ? tableCell : cellToMerge;
  83. const cellToRemove = isMergeNext ? cellToMerge : tableCell;
  84. writer.move( Range.createIn( cellToRemove ), Position.createAt( cellToExpand, 'end' ) );
  85. writer.remove( cellToRemove );
  86. const spanAttribute = this.isHorizontal ? 'colspan' : 'rowspan';
  87. const cellSpan = parseInt( tableCell.getAttribute( spanAttribute ) || 1 );
  88. const cellToMergeSpan = parseInt( cellToMerge.getAttribute( spanAttribute ) || 1 );
  89. writer.setAttribute( spanAttribute, cellSpan + cellToMergeSpan, cellToExpand );
  90. writer.setSelection( Range.createIn( cellToExpand ) );
  91. } );
  92. }
  93. /**
  94. * Returns a cell that can be merged with the current cell depending on the command's direction.
  95. *
  96. * @returns {module:engine/model/element|undefined}
  97. * @private
  98. */
  99. _getMergeableCell() {
  100. const model = this.editor.model;
  101. const doc = model.document;
  102. const element = doc.selection.getFirstPosition().parent;
  103. if ( !element.is( 'tableCell' ) ) {
  104. return;
  105. }
  106. // First get the cell on proper direction.
  107. const cellToMerge = this.isHorizontal ? getHorizontalCell( element, this.direction ) : getVerticalCell( element, this.direction );
  108. if ( !cellToMerge ) {
  109. return;
  110. }
  111. // If found check if the span perpendicular to merge direction is equal on both cells.
  112. const spanAttribute = this.isHorizontal ? 'rowspan' : 'colspan';
  113. const span = parseInt( element.getAttribute( spanAttribute ) || 1 );
  114. const cellToMergeSpan = parseInt( cellToMerge.getAttribute( spanAttribute ) || 1 );
  115. if ( cellToMergeSpan === span ) {
  116. return cellToMerge;
  117. }
  118. }
  119. }
  120. // Returns the cell that can be merged horizontally.
  121. //
  122. // @param {module:engine/model/element~Element} tableCell
  123. // @param {String} direction
  124. // @returns {module:engine/model/node~Node|null}
  125. function getHorizontalCell( tableCell, direction ) {
  126. return direction == 'right' ? tableCell.nextSibling : tableCell.previousSibling;
  127. }
  128. // Returns the cell that can be merged vertically.
  129. //
  130. // @param {module:engine/model/element~Element} tableCell
  131. // @param {String} direction
  132. // @returns {module:engine/model/node~Node|null}
  133. function getVerticalCell( tableCell, direction ) {
  134. const tableRow = tableCell.parent;
  135. const table = tableRow.parent;
  136. const rowIndex = table.getChildIndex( tableRow );
  137. // Don't search for mergeable cell if direction points out of the table.
  138. if ( ( direction == 'down' && rowIndex === table.childCount - 1 ) || ( direction == 'up' && rowIndex === 0 ) ) {
  139. return;
  140. }
  141. const headingRows = table.getAttribute( 'headingRows' ) || 0;
  142. // Don't search for mergeable cell if direction points out of the current table section.
  143. if ( headingRows && ( ( direction == 'down' && rowIndex === headingRows - 1 ) || ( direction == 'up' && rowIndex === headingRows ) ) ) {
  144. return;
  145. }
  146. const currentCellRowSpan = parseInt( tableCell.getAttribute( 'rowspan' ) || 1 );
  147. const rowOfCellToMerge = direction == 'down' ? rowIndex + currentCellRowSpan : rowIndex;
  148. const tableMap = [ ...new TableWalker( table, { endRow: rowOfCellToMerge } ) ];
  149. const currentCellData = tableMap.find( value => value.cell === tableCell );
  150. const mergeColumn = currentCellData.column;
  151. const cellToMergeData = tableMap.find( ( { row, rowspan, column } ) => {
  152. if ( column !== mergeColumn ) {
  153. return false;
  154. }
  155. if ( direction == 'down' ) {
  156. // If merging a cell below the mergeRow is already calculated.
  157. return row === rowOfCellToMerge;
  158. } else {
  159. // If merging a cell above calculate if it spans to mergeRow.
  160. return rowOfCellToMerge === row + rowspan;
  161. }
  162. } );
  163. return cellToMergeData && cellToMergeData.cell;
  164. }