tablecellpropertycommand.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 { findAncestor } from '../../commands/utils';
  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 selection = editor.model.document.selection;
  34. const tableCell = findAncestor( 'tableCell', selection.getFirstPosition() );
  35. this.isEnabled = !!tableCell;
  36. this.value = this._getAttribute( tableCell );
  37. }
  38. /**
  39. * Executes the command.
  40. *
  41. * @fires execute
  42. * @param {Object} [options]
  43. * @param {*} [options.value] If set, the command will set the attribute on selected table cells.
  44. * If it is not set, the command will remove the attribute from selected table cells.
  45. * @param {module:engine/model/batch~Batch} [options.batch] Pass the model batch instance to the command to aggregate changes,
  46. * e.g. allow a single undo step for multiple executions.
  47. */
  48. execute( options = {} ) {
  49. const model = this.editor.model;
  50. const selection = model.document.selection;
  51. const { value, batch } = options;
  52. const tableCells = Array.from( selection.getSelectedBlocks() )
  53. .map( element => findAncestor( 'tableCell', model.createPositionAt( element, 0 ) ) );
  54. const valueToSet = this._getValueToSet( value );
  55. model.enqueueChange( batch || 'default', writer => {
  56. if ( valueToSet ) {
  57. tableCells.forEach( tableCell => writer.setAttribute( this.attributeName, valueToSet, tableCell ) );
  58. } else {
  59. tableCells.forEach( tableCell => writer.removeAttribute( this.attributeName, tableCell ) );
  60. }
  61. } );
  62. }
  63. /**
  64. * Returns the attribute value for a table cell.
  65. *
  66. * @param {module:engine/model/element~Element} tableCell
  67. * @returns {String|undefined}
  68. * @private
  69. */
  70. _getAttribute( tableCell ) {
  71. if ( !tableCell ) {
  72. return;
  73. }
  74. return tableCell.getAttribute( this.attributeName );
  75. }
  76. /**
  77. * Returns the proper model value. Can be used to add a default unit to numeric values.
  78. *
  79. * @private
  80. * @param {*} value
  81. * @returns {*}
  82. */
  83. _getValueToSet( value ) {
  84. return value;
  85. }
  86. }