tablecellpropertycommand.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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/tablecellproperties/commands/tablecellpropertycommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import { getSelectionAffectedTableCells } from '../../utils/selection';
  10. /**
  11. * The table cell attribute command.
  12. *
  13. * The command is a base command for other table cell property commands.
  14. *
  15. * @extends module:core/command~Command
  16. */
  17. export default class TableCellPropertyCommand extends Command {
  18. /**
  19. * Creates a new `TableCellPropertyCommand` instance.
  20. *
  21. * @param {module:core/editor/editor~Editor} editor An editor in which this command will be used.
  22. * @param {String} attributeName Table cell attribute name.
  23. */
  24. constructor( editor, attributeName ) {
  25. super( editor );
  26. this.attributeName = attributeName;
  27. }
  28. /**
  29. * @inheritDoc
  30. */
  31. refresh() {
  32. const editor = this.editor;
  33. const selectedTableCells = getSelectionAffectedTableCells( editor.model.document.selection );
  34. this.isEnabled = !!selectedTableCells.length;
  35. this.value = this._getSingleValue( selectedTableCells );
  36. }
  37. /**
  38. * Executes the command.
  39. *
  40. * @fires execute
  41. * @param {Object} [options]
  42. * @param {*} [options.value] If set, the command will set the attribute on selected table cells.
  43. * If it is not set, the command will remove the attribute from the selected table cells.
  44. * @param {module:engine/model/batch~Batch} [options.batch] Pass the model batch instance to the command to aggregate changes,
  45. * for example to allow a single undo step for multiple executions.
  46. */
  47. execute( options = {} ) {
  48. const { value, batch } = options;
  49. const model = this.editor.model;
  50. const tableCells = getSelectionAffectedTableCells( model.document.selection );
  51. const valueToSet = this._getValueToSet( value );
  52. model.enqueueChange( batch || 'default', writer => {
  53. if ( valueToSet ) {
  54. tableCells.forEach( tableCell => writer.setAttribute( this.attributeName, valueToSet, tableCell ) );
  55. } else {
  56. tableCells.forEach( tableCell => writer.removeAttribute( this.attributeName, tableCell ) );
  57. }
  58. } );
  59. }
  60. /**
  61. * Returns the attribute value for a table cell.
  62. *
  63. * @param {module:engine/model/element~Element} tableCell
  64. * @returns {String|undefined}
  65. * @private
  66. */
  67. _getAttribute( tableCell ) {
  68. if ( !tableCell ) {
  69. return;
  70. }
  71. return tableCell.getAttribute( this.attributeName );
  72. }
  73. /**
  74. * Returns the proper model value. It can be used to add a default unit to numeric values.
  75. *
  76. * @private
  77. * @param {*} value
  78. * @returns {*}
  79. */
  80. _getValueToSet( value ) {
  81. return value;
  82. }
  83. /**
  84. * Returns a single value for all selected table cells. If the value is the same for all cells,
  85. * it will be returned (`undefined` otherwise).
  86. *
  87. * @param {Array.<module:engine/model/element~Element>} tableCell
  88. * @returns {*}
  89. * @private
  90. */
  91. _getSingleValue( tableCell ) {
  92. const firstCellValue = this._getAttribute( tableCell[ 0 ] );
  93. const everyCellHasAttribute = tableCell.every( tableCell => this._getAttribute( tableCell ) === firstCellValue );
  94. return everyCellHasAttribute ? firstCellValue : undefined;
  95. }
  96. }