8
0

multicommand.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. import Command from './command';
  6. /**
  7. * @module core/multicommand
  8. */
  9. /**
  10. * A CKEditor command that aggregates other commands.
  11. *
  12. * This command is used to proxy multiple commands. The multi-command is enabled when
  13. * at least one of its registered child commands is enabled.
  14. * When executing a multi-command the first command that is enabled will be executed.
  15. *
  16. * const multiCommand = new MultiCommand( editor );
  17. *
  18. * const commandFoo = new Command( editor );
  19. * const commandBar = new Command( editor );
  20. *
  21. * // Register child commands.
  22. * multiCommand.registerChildCommand( commandFoo );
  23. * multiCommand.registerChildCommand( commandBar );
  24. *
  25. * // Enable one of the commands.
  26. * commandBar.isEnabled = true;
  27. *
  28. * multiCommand.execute(); // Will execute commandBar.
  29. *
  30. * @extends module:core/command~Command
  31. */
  32. export default class MultiCommand extends Command {
  33. /**
  34. * @inheritDoc
  35. */
  36. constructor( editor ) {
  37. super( editor );
  38. /**
  39. * Registered child commands.
  40. *
  41. * @type {Array.<module:core/command~Command>}
  42. * @private
  43. */
  44. this._childCommands = [];
  45. }
  46. /**
  47. * @inheritDoc
  48. */
  49. refresh() {
  50. // Override base command refresh(): the command's state is changed when one of child commands changes states.
  51. }
  52. /**
  53. * Executes the first of it registered child commands.
  54. */
  55. execute( ...args ) {
  56. const command = this._getFirstEnabledCommand();
  57. command.execute( args );
  58. }
  59. /**
  60. * Registers a child command.
  61. *
  62. * @param {module:core/command~Command} command
  63. */
  64. registerChildCommand( command ) {
  65. this._childCommands.push( command );
  66. // Change multi command enabled state when one of registered commands changes state.
  67. command.on( 'change:isEnabled', () => this._checkEnabled() );
  68. this._checkEnabled();
  69. }
  70. /**
  71. * Checks if any of child commands is enabled.
  72. *
  73. * @private
  74. */
  75. _checkEnabled() {
  76. this.isEnabled = !!this._getFirstEnabledCommand();
  77. }
  78. /**
  79. * Returns a first enabled command or undefined if none of them is enabled.
  80. *
  81. * @returns {module:core/command~Command|undefined}
  82. * @private
  83. */
  84. _getFirstEnabledCommand() {
  85. return this._childCommands.find( command => command.isEnabled );
  86. }
  87. }