setheadercolumncommand.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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. */
  56. execute() {
  57. const model = this.editor.model;
  58. const doc = model.document;
  59. const selection = doc.selection;
  60. const tableUtils = this.editor.plugins.get( 'TableUtils' );
  61. const position = selection.getFirstPosition();
  62. const tableCell = findAncestor( 'tableCell', position.parent );
  63. const tableRow = tableCell.parent;
  64. const table = tableRow.parent;
  65. const currentHeadingColumns = parseInt( table.getAttribute( 'headingColumns' ) || 0 );
  66. const { column: selectionColumn } = tableUtils.getCellLocation( tableCell );
  67. const headingColumnsToSet = currentHeadingColumns > selectionColumn ? selectionColumn : selectionColumn + 1;
  68. model.change( writer => {
  69. updateNumericAttribute( 'headingColumns', headingColumnsToSet, table, writer, 0 );
  70. } );
  71. }
  72. /**
  73. * Checks if a table cell is in the heading section.
  74. *
  75. * @param {module:engine/model/element~Element} tableCell
  76. * @param {module:engine/model/element~Element} table
  77. * @returns {Boolean}
  78. * @private
  79. */
  80. _isInHeading( tableCell, table ) {
  81. const headingColumns = parseInt( table.getAttribute( 'headingColumns' ) || 0 );
  82. const tableUtils = this.editor.plugins.get( 'TableUtils' );
  83. const { column } = tableUtils.getCellLocation( tableCell );
  84. return !!headingColumns && column < headingColumns;
  85. }
  86. }