UNPKG

2.45 kBTypeScriptView Raw
1/**
2 * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4 */
5import type Command from './command.js';
6/**
7 * Collection of commands. Its instance is available in {@link module:core/editor/editor~Editor#commands `editor.commands`}.
8 */
9export default class CommandCollection implements Iterable<[string, Command]> {
10 /**
11 * Command map.
12 */
13 private _commands;
14 /**
15 * Creates collection instance.
16 */
17 constructor();
18 /**
19 * Registers a new command.
20 *
21 * @param commandName The name of the command.
22 */
23 add<TName extends string>(commandName: TName, command: CommandsMap[TName]): void;
24 /**
25 * Retrieves a command from the collection.
26 *
27 * @param commandName The name of the command.
28 */
29 get<TName extends string>(commandName: TName): CommandsMap[TName] | undefined;
30 /**
31 * Executes a command.
32 *
33 * @param commandName The name of the command.
34 * @param commandParams Command parameters.
35 * @returns The value returned by the {@link module:core/command~Command#execute `command.execute()`}.
36 */
37 execute<TName extends string>(commandName: TName, ...commandParams: Parameters<CommandsMap[TName]['execute']>): ReturnType<CommandsMap[TName]['execute']>;
38 /**
39 * Returns iterator of command names.
40 */
41 names(): IterableIterator<string>;
42 /**
43 * Returns iterator of command instances.
44 */
45 commands(): IterableIterator<Command>;
46 /**
47 * Iterable interface.
48 *
49 * Returns `[ commandName, commandInstance ]` pairs.
50 */
51 [Symbol.iterator](): Iterator<[string, Command]>;
52 /**
53 * Destroys all collection commands.
54 */
55 destroy(): void;
56}
57/**
58 * Helper type that maps command names to their types.
59 * It is meant to be extended with module augmentation.
60 *
61 * ```ts
62 * class MyCommand extends Command {
63 * public execute( parameter: A ): B {
64 * // ...
65 * }
66 * }
67 *
68 * declare module '@ckeditor/ckeditor5-core' {
69 * interface CommandsMap {
70 * myCommand: MyCommand;
71 * }
72 * }
73 *
74 * // Returns `MyCommand | undefined`.
75 * const myCommand = editor.commands.get( 'myCommand' );
76 *
77 * // Expects `A` type as parameter and returns `B`.
78 * const value = editor.commands.execute( 'myCommand', new A() );
79 * ```
80 */
81export interface CommandsMap {
82 [name: string]: Command;
83}
84
\No newline at end of file