setheadercolumncommand.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 { getSelectionAffectedTableCells } 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 selectedCells = getSelectionAffectedTableCells( model.document.selection );
  36. const tableUtils = this.editor.plugins.get( 'TableUtils' );
  37. const isInTable = selectedCells.length > 0;
  38. this.isEnabled = isInTable;
  39. /**
  40. * Flag indicating whether the command is active. The command is active when the
  41. * {@link module:engine/model/selection~Selection} is in a header column.
  42. *
  43. * @observable
  44. * @readonly
  45. * @member {Boolean} #value
  46. */
  47. this.value = isInTable && selectedCells.every( cell => isHeadingColumnCell( tableUtils, cell ) );
  48. }
  49. /**
  50. * Executes the command.
  51. *
  52. * When the selection is in a non-header column, the command will set the `headingColumns` table attribute to cover that column.
  53. *
  54. * When the selection is already in a header column, it will set `headingColumns` so the heading section will end before that column.
  55. *
  56. * @fires execute
  57. * @param {Object} [options]
  58. * @param {Boolean} [options.forceValue] If set, the command will set (`true`) or unset (`false`) the header columns according to
  59. * the `forceValue` parameter instead of the current model state.
  60. */
  61. execute( options = {} ) {
  62. const model = this.editor.model;
  63. const tableUtils = this.editor.plugins.get( 'TableUtils' );
  64. const selectedCells = getSelectionAffectedTableCells( model.document.selection );
  65. const firstCell = selectedCells[ 0 ];
  66. const lastCell = selectedCells[ selectedCells.length - 1 ];
  67. const tableRow = firstCell.parent;
  68. const table = tableRow.parent;
  69. const [ selectedColumnMin, selectedColumnMax ] =
  70. // Returned cells might not necessary be in order, so make sure to sort it.
  71. [ tableUtils.getCellLocation( firstCell ).column, tableUtils.getCellLocation( lastCell ).column ].sort();
  72. if ( options.forceValue === this.value ) {
  73. return;
  74. }
  75. const headingColumnsToSet = this.value ? selectedColumnMin : selectedColumnMax + 1;
  76. model.change( writer => {
  77. updateNumericAttribute( 'headingColumns', headingColumnsToSet, table, writer, 0 );
  78. } );
  79. }
  80. }