setheadercolumncommand.js 3.4 KB

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