8
0

setheadercolumncommand.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 { findAncestor, isHeadingColumnCell, updateNumericAttribute } from './utils';
  10. import { getColumnIndexes, getSelectionAffectedTableCells } 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 selectedCells = getSelectionAffectedTableCells( model.document.selection );
  33. const tableUtils = this.editor.plugins.get( 'TableUtils' );
  34. const isInTable = selectedCells.length > 0;
  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 && selectedCells.every( cell => isHeadingColumnCell( tableUtils, cell ) );
  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. if ( options.forceValue === this.value ) {
  60. return;
  61. }
  62. const model = this.editor.model;
  63. const selectedCells = getSelectionAffectedTableCells( model.document.selection );
  64. const { first, last } = getColumnIndexes( selectedCells );
  65. const headingColumnsToSet = this.value ? first : last + 1;
  66. model.change( writer => {
  67. const table = findAncestor( 'table', selectedCells[ 0 ] );
  68. updateNumericAttribute( 'headingColumns', headingColumnsToSet, table, writer, 0 );
  69. } );
  70. }
  71. }