setheaderrowcommand.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 { updateNumericAttribute } from '../utils/common';
  10. import { getRowIndexes, getSelectionAffectedTableCells } from '../utils/selection';
  11. import { getVerticallyOverlappingCells, splitHorizontally } from '../utils/structure';
  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 = selectedCells[ 0 ].findAncestor( 'table' );
  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 startRow = headingRowsToSet > currentHeadingRows ? currentHeadingRows : 0;
  72. const overlappingCells = getVerticallyOverlappingCells( table, headingRowsToSet, startRow );
  73. for ( const { cell } of overlappingCells ) {
  74. splitHorizontally( cell, headingRowsToSet, writer );
  75. }
  76. }
  77. updateNumericAttribute( 'headingRows', headingRowsToSet, table, writer, 0 );
  78. } );
  79. }
  80. /**
  81. * Checks if a table cell is in the heading section.
  82. *
  83. * @param {module:engine/model/element~Element} tableCell
  84. * @param {module:engine/model/element~Element} table
  85. * @returns {Boolean}
  86. * @private
  87. */
  88. _isInHeading( tableCell, table ) {
  89. const headingRows = parseInt( table.getAttribute( 'headingRows' ) || 0 );
  90. return !!headingRows && tableCell.parent.index < headingRows;
  91. }
  92. }