setheadercolumncommand.js 3.4 KB

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