8
0

commandcollection.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 core/commandcollection
  7. */
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. /**
  10. * Collection of commands. Its instance is available in {@link module:core/editor/editor~Editor#commands `editor.commands`}.
  11. */
  12. export default class CommandCollection {
  13. /**
  14. * Creates collection instance.
  15. */
  16. constructor() {
  17. /**
  18. * Command map.
  19. *
  20. * @private
  21. * @member {Map}
  22. */
  23. this._commands = new Map();
  24. }
  25. /**
  26. * Registers a new command.
  27. *
  28. * @param {String} commandName The name of the command.
  29. * @param {module:core/command~Command} command
  30. */
  31. add( commandName, command ) {
  32. this._commands.set( commandName, command );
  33. }
  34. /**
  35. * Retrieves a command from the collection.
  36. *
  37. * @param {String} commandName The name of the command.
  38. * @returns {module:core/command~Command}
  39. */
  40. get( commandName ) {
  41. return this._commands.get( commandName );
  42. }
  43. /**
  44. * Executes a command.
  45. *
  46. * @param {String} commandName The name of the command.
  47. * @param {*} [...commandParams] Command parameters.
  48. * @returns {*} The value returned by the {@link module:core/command~Command#execute `command.execute()`}.
  49. */
  50. execute( commandName, ...args ) {
  51. const command = this.get( commandName );
  52. if ( !command ) {
  53. /**
  54. * Command does not exist.
  55. *
  56. * @error commandcollection-command-not-found
  57. * @param {String} commandName Name of the command.
  58. */
  59. throw new CKEditorError( 'commandcollection-command-not-found: Command does not exist.', this, { commandName } );
  60. }
  61. return command.execute( ...args );
  62. }
  63. /**
  64. * Returns iterator of command names.
  65. *
  66. * @returns {Iterable.<String>}
  67. */
  68. * names() {
  69. yield* this._commands.keys();
  70. }
  71. /**
  72. * Returns iterator of command instances.
  73. *
  74. * @returns {Iterable.<module:core/command~Command>}
  75. */
  76. * commands() {
  77. yield* this._commands.values();
  78. }
  79. /**
  80. * Iterable interface.
  81. *
  82. * Returns `[ commandName, commandInstance ]` pairs.
  83. *
  84. * @returns {Iterable.<Array>}
  85. */
  86. [ Symbol.iterator ]() {
  87. return this._commands[ Symbol.iterator ]();
  88. }
  89. /**
  90. * Destroys all collection commands.
  91. */
  92. destroy() {
  93. for ( const command of this.commands() ) {
  94. command.destroy();
  95. }
  96. }
  97. }