8
0

setheadercolumncommand.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. isHeadingColumnCell,
  11. updateNumericAttribute
  12. } from '../utils/common';
  13. import { getColumnIndexes, getSelectionAffectedTableCells } from '../utils/selection';
  14. import { getHorizontallyOverlappingCells, splitVertically } from '../utils/structure';
  15. /**
  16. * The header column command.
  17. *
  18. * The command is registered by {@link module:table/tableediting~TableEditing} as the `'setTableColumnHeader'` editor command.
  19. *
  20. * You can make the column containing the selected cell a [header](https://www.w3.org/TR/html50/tabular-data.html#the-th-element)
  21. * by executing:
  22. *
  23. * editor.execute( 'setTableColumnHeader' );
  24. *
  25. * **Note:** All preceding columns will also become headers. If the current column is already a header, executing this command
  26. * will make it a regular column back again (including the following columns).
  27. *
  28. * @extends module:core/command~Command
  29. */
  30. export default class SetHeaderColumnCommand extends Command {
  31. /**
  32. * @inheritDoc
  33. */
  34. refresh() {
  35. const model = this.editor.model;
  36. const selectedCells = getSelectionAffectedTableCells( model.document.selection );
  37. const tableUtils = this.editor.plugins.get( 'TableUtils' );
  38. const isInTable = selectedCells.length > 0;
  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 && selectedCells.every( cell => isHeadingColumnCell( tableUtils, cell ) );
  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. if ( options.forceValue === this.value ) {
  64. return;
  65. }
  66. const model = this.editor.model;
  67. const selectedCells = getSelectionAffectedTableCells( model.document.selection );
  68. const table = selectedCells[ 0 ].findAncestor( 'table' );
  69. const { first, last } = getColumnIndexes( selectedCells );
  70. const headingColumnsToSet = this.value ? first : last + 1;
  71. model.change( writer => {
  72. if ( headingColumnsToSet ) {
  73. // Changing heading columns requires to check if any of a heading cell is overlapping horizontally the table head.
  74. // Any table cell that has a colspan attribute > 1 will not exceed the table head so we need to fix it in columns before.
  75. const overlappingCells = getHorizontallyOverlappingCells( table, headingColumnsToSet );
  76. for ( const { cell, column } of overlappingCells ) {
  77. splitVertically( cell, column, headingColumnsToSet, writer );
  78. }
  79. }
  80. updateNumericAttribute( 'headingColumns', headingColumnsToSet, table, writer, 0 );
  81. } );
  82. }
  83. }