setheadercolumncommand.js 2.9 KB

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