tablepropertycommand.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /**
  10. * The table cell attribute command.
  11. *
  12. * This command is a base command for other table property commands.
  13. *
  14. * @extends module:core/command~Command
  15. */
  16. export default class TablePropertyCommand extends Command {
  17. /**
  18. * Creates a new `TablePropertyCommand` instance.
  19. *
  20. * @param {module:core/editor/editor~Editor} editor An editor in which this command will be used.
  21. * @param {String} attributeName Table cell attribute name.
  22. */
  23. constructor( editor, attributeName ) {
  24. super( editor );
  25. this.attributeName = attributeName;
  26. }
  27. /**
  28. * @inheritDoc
  29. */
  30. refresh() {
  31. const editor = this.editor;
  32. const selection = editor.model.document.selection;
  33. const table = selection.getFirstPosition().findAncestor( 'table' );
  34. this.isEnabled = !!table;
  35. this.value = this._getValue( table );
  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 the selected table.
  43. * If not set, the command will remove the attribute from the selected table.
  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 model = this.editor.model;
  49. const selection = model.document.selection;
  50. const { value, batch } = options;
  51. const table = selection.getFirstPosition().findAncestor( 'table' );
  52. const valueToSet = this._getValueToSet( value );
  53. model.enqueueChange( batch || 'default', writer => {
  54. if ( valueToSet ) {
  55. writer.setAttribute( this.attributeName, valueToSet, table );
  56. } else {
  57. writer.removeAttribute( this.attributeName, table );
  58. }
  59. } );
  60. }
  61. /**
  62. * Returns the attribute value for a table.
  63. *
  64. * @param {module:engine/model/element~Element} table
  65. * @returns {String|undefined}
  66. * @private
  67. */
  68. _getValue( table ) {
  69. if ( !table ) {
  70. return;
  71. }
  72. return table.getAttribute( this.attributeName );
  73. }
  74. /**
  75. * Returns the proper model value. It can be used to add a default unit to numeric values.
  76. *
  77. * @private
  78. * @param {*} value
  79. * @returns {*}
  80. */
  81. _getValueToSet( value ) {
  82. return value;
  83. }
  84. }