8
0

tablepropertycommand.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/tableproperties/commands/tablepropertycommand
  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 property commands.
  14. *
  15. * @extends module:core/command~Command
  16. */
  17. export default class TablePropertyCommand extends Command {
  18. /**
  19. * Creates a new `TablePropertyCommand` 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 table = findAncestor( 'table', selection.getFirstPosition() );
  35. this.isEnabled = !!table;
  36. this.value = this._getValue( table );
  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 the selected table.
  44. * If not set, the command will remove the attribute from the selected table.
  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 table = findAncestor( 'table', selection.getFirstPosition() );
  53. const valueToSet = this._getValueToSet( value );
  54. model.enqueueChange( batch || 'default', writer => {
  55. if ( valueToSet ) {
  56. writer.setAttribute( this.attributeName, valueToSet, table );
  57. } else {
  58. writer.removeAttribute( this.attributeName, table );
  59. }
  60. } );
  61. }
  62. /**
  63. * Returns the attribute value for a table.
  64. *
  65. * @param {module:engine/model/element~Element} table
  66. * @returns {String|undefined}
  67. * @private
  68. */
  69. _getValue( table ) {
  70. if ( !table ) {
  71. return;
  72. }
  73. return table.getAttribute( this.attributeName );
  74. }
  75. /**
  76. * Returns proper model value. Can be used to add default unit to numeric values.
  77. *
  78. * @private
  79. * @param {*} value
  80. * @returns {*}
  81. */
  82. _getValueToSet( value ) {
  83. return value;
  84. }
  85. }