setheaderrowcommand.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/setheaderrowcommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import { createEmptyTableCell, findAncestor, updateNumericAttribute } from './utils';
  10. import { getRowIndexes, getSelectionAffectedTableCells } from '../utils';
  11. import TableWalker from '../tablewalker';
  12. /**
  13. * The header row command.
  14. *
  15. * The command is registered by {@link module:table/tableediting~TableEditing} as the `'setTableColumnHeader'` editor command.
  16. *
  17. * You can make the row containing the selected cell a [header](https://www.w3.org/TR/html50/tabular-data.html#the-th-element) by executing:
  18. *
  19. * editor.execute( 'setTableRowHeader' );
  20. *
  21. * **Note:** All preceding rows will also become headers. If the current row is already a header, executing this command
  22. * will make it a regular row back again (including the following rows).
  23. *
  24. * @extends module:core/command~Command
  25. */
  26. export default class SetHeaderRowCommand extends Command {
  27. /**
  28. * @inheritDoc
  29. */
  30. refresh() {
  31. const model = this.editor.model;
  32. const selectedCells = getSelectionAffectedTableCells( model.document.selection );
  33. const isInTable = selectedCells.length > 0;
  34. this.isEnabled = isInTable;
  35. /**
  36. * Flag indicating whether the command is active. The command is active when the
  37. * {@link module:engine/model/selection~Selection} is in a header row.
  38. *
  39. * @observable
  40. * @readonly
  41. * @member {Boolean} #value
  42. */
  43. this.value = isInTable && selectedCells.every( cell => this._isInHeading( cell, cell.parent.parent ) );
  44. }
  45. /**
  46. * Executes the command.
  47. *
  48. * When the selection is in a non-header row, the command will set the `headingRows` table attribute to cover that row.
  49. *
  50. * When the selection is already in a header row, it will set `headingRows` so the heading section will end before that row.
  51. *
  52. * @fires execute
  53. * @param {Object} options
  54. * @param {Boolean} [options.forceValue] If set, the command will set (`true`) or unset (`false`) the header rows according to
  55. * the `forceValue` parameter instead of the current model state.
  56. */
  57. execute( options = {} ) {
  58. if ( options.forceValue === this.value ) {
  59. return;
  60. }
  61. const model = this.editor.model;
  62. const selectedCells = getSelectionAffectedTableCells( model.document.selection );
  63. const table = findAncestor( 'table', selectedCells[ 0 ] );
  64. const { first, last } = getRowIndexes( selectedCells );
  65. const headingRowsToSet = this.value ? first : last + 1;
  66. const currentHeadingRows = table.getAttribute( 'headingRows' ) || 0;
  67. model.change( writer => {
  68. if ( headingRowsToSet ) {
  69. // Changing heading rows requires to check if any of a heading cell is overlapping vertically the table head.
  70. // Any table cell that has a rowspan attribute > 1 will not exceed the table head so we need to fix it in rows below.
  71. const cellsToSplit = getOverlappingCells( table, headingRowsToSet, currentHeadingRows );
  72. for ( const cell of cellsToSplit ) {
  73. splitHorizontally( cell, headingRowsToSet, writer );
  74. }
  75. }
  76. updateNumericAttribute( 'headingRows', headingRowsToSet, table, writer, 0 );
  77. } );
  78. }
  79. /**
  80. * Checks if a table cell is in the heading section.
  81. *
  82. * @param {module:engine/model/element~Element} tableCell
  83. * @param {module:engine/model/element~Element} table
  84. * @returns {Boolean}
  85. * @private
  86. */
  87. _isInHeading( tableCell, table ) {
  88. const headingRows = parseInt( table.getAttribute( 'headingRows' ) || 0 );
  89. return !!headingRows && tableCell.parent.index < headingRows;
  90. }
  91. }
  92. // Returns cells that span beyond the new heading section.
  93. //
  94. // @param {module:engine/model/element~Element} table The table to check.
  95. // @param {Number} headingRowsToSet New heading rows attribute.
  96. // @param {Number} currentHeadingRows Current heading rows attribute.
  97. // @returns {Array.<module:engine/model/element~Element>}
  98. function getOverlappingCells( table, headingRowsToSet, currentHeadingRows ) {
  99. const cellsToSplit = [];
  100. const startAnalysisRow = headingRowsToSet > currentHeadingRows ? currentHeadingRows : 0;
  101. // We're analyzing only when headingRowsToSet > 0.
  102. const endAnalysisRow = headingRowsToSet - 1;
  103. const tableWalker = new TableWalker( table, { startRow: startAnalysisRow, endRow: endAnalysisRow } );
  104. for ( const { row, rowspan, cell } of tableWalker ) {
  105. if ( rowspan > 1 && row + rowspan > headingRowsToSet ) {
  106. cellsToSplit.push( cell );
  107. }
  108. }
  109. return cellsToSplit;
  110. }
  111. // Splits the table cell horizontally.
  112. //
  113. // @param {module:engine/model/element~Element} tableCell
  114. // @param {Number} headingRows
  115. // @param {module:engine/model/writer~Writer} writer
  116. function splitHorizontally( tableCell, headingRows, writer ) {
  117. const tableRow = tableCell.parent;
  118. const table = tableRow.parent;
  119. const rowIndex = tableRow.index;
  120. const rowspan = parseInt( tableCell.getAttribute( 'rowspan' ) );
  121. const newRowspan = headingRows - rowIndex;
  122. const attributes = {};
  123. const spanToSet = rowspan - newRowspan;
  124. if ( spanToSet > 1 ) {
  125. attributes.rowspan = spanToSet;
  126. }
  127. const colspan = parseInt( tableCell.getAttribute( 'colspan' ) || 1 );
  128. if ( colspan > 1 ) {
  129. attributes.colspan = colspan;
  130. }
  131. const startRow = table.getChildIndex( tableRow );
  132. const endRow = startRow + newRowspan;
  133. const tableMap = [ ...new TableWalker( table, { startRow, endRow, includeSpanned: true } ) ];
  134. let columnIndex;
  135. for ( const { row, column, cell, cellIndex } of tableMap ) {
  136. if ( cell === tableCell && columnIndex === undefined ) {
  137. columnIndex = column;
  138. }
  139. if ( columnIndex !== undefined && columnIndex === column && row === endRow ) {
  140. const tableRow = table.getChild( row );
  141. const tableCellPosition = writer.createPositionAt( tableRow, cellIndex );
  142. createEmptyTableCell( writer, tableCellPosition, attributes );
  143. }
  144. }
  145. // Update the rowspan attribute after updating table.
  146. updateNumericAttribute( 'rowspan', newRowspan, tableCell, writer );
  147. }