setheadercolumncommand.js 3.4 KB

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