setheadercolumncommand.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/setheadercolumncommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import { updateNumericAttribute } from './utils';
  10. import { getTableCellsContainingSelection } from '../utils';
  11. /**
  12. * The header column command.
  13. *
  14. * The command is registered by {@link module:table/tableediting~TableEditing} as the `'setTableColumnHeader'` editor command.
  15. *
  16. * You can make the column containing the selected cell a [header](https://www.w3.org/TR/html50/tabular-data.html#the-th-element)
  17. * by executing:
  18. *
  19. * editor.execute( 'setTableColumnHeader' );
  20. *
  21. * **Note:** All preceding columns will also become headers. If the current column is already a header, executing this command
  22. * will make it a regular column back again (including the following columns).
  23. *
  24. * @extends module:core/command~Command
  25. */
  26. export default class SetHeaderColumnCommand extends Command {
  27. /**
  28. * @inheritDoc
  29. */
  30. refresh() {
  31. const model = this.editor.model;
  32. const doc = model.document;
  33. const tableCell = getTableCellsContainingSelection( doc.selection )[ 0 ];
  34. const isInTable = !!tableCell;
  35. this.isEnabled = isInTable;
  36. /**
  37. * Flag indicating whether the command is active. The command is active when the
  38. * {@link module:engine/model/selection~Selection} is in a header column.
  39. *
  40. * @observable
  41. * @readonly
  42. * @member {Boolean} #value
  43. */
  44. this.value = isInTable && this._isInHeading( tableCell, tableCell.parent.parent );
  45. }
  46. /**
  47. * Executes the command.
  48. *
  49. * When the selection is in a non-header column, the command will set the `headingColumns` table attribute to cover that column.
  50. *
  51. * When the selection is already in a header column, it will set `headingColumns` so the heading section will end before that column.
  52. *
  53. * @fires execute
  54. * @param {Object} [options]
  55. * @param {Boolean} [options.forceValue] If set, the command will set (`true`) or unset (`false`) the header columns according to
  56. * the `forceValue` parameter instead of the current model state.
  57. */
  58. execute( options = {} ) {
  59. const model = this.editor.model;
  60. const doc = model.document;
  61. const selection = doc.selection;
  62. const tableUtils = this.editor.plugins.get( 'TableUtils' );
  63. const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
  64. const tableRow = tableCell.parent;
  65. const table = tableRow.parent;
  66. const { column: selectionColumn } = tableUtils.getCellLocation( tableCell );
  67. if ( options.forceValue === this.value ) {
  68. return;
  69. }
  70. const headingColumnsToSet = this.value ? selectionColumn : selectionColumn + 1;
  71. model.change( writer => {
  72. updateNumericAttribute( 'headingColumns', headingColumnsToSet, table, writer, 0 );
  73. } );
  74. }
  75. /**
  76. * Checks if a table cell is in the heading section.
  77. *
  78. * @param {module:engine/model/element~Element} tableCell
  79. * @param {module:engine/model/element~Element} table
  80. * @returns {Boolean}
  81. * @private
  82. */
  83. _isInHeading( tableCell, table ) {
  84. const headingColumns = parseInt( table.getAttribute( 'headingColumns' ) || 0 );
  85. const tableUtils = this.editor.plugins.get( 'TableUtils' );
  86. const { column } = tableUtils.getCellLocation( tableCell );
  87. return !!headingColumns && column < headingColumns;
  88. }
  89. }