UNPKG

2.17 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 */
5/**
6 * @module core/multicommand
7 */
8import Command from './command.js';
9import { type PriorityString } from '@ckeditor/ckeditor5-utils';
10/**
11 * A CKEditor command that aggregates other commands.
12 *
13 * This command is used to proxy multiple commands. The multi-command is enabled when
14 * at least one of its registered child commands is enabled.
15 * When executing a multi-command, the first enabled command with highest priority will be executed.
16 *
17 * ```ts
18 * const multiCommand = new MultiCommand( editor );
19 *
20 * const commandFoo = new Command( editor );
21 * const commandBar = new Command( editor );
22 *
23 * // Register a child command.
24 * multiCommand.registerChildCommand( commandFoo );
25 * // Register a child command with a low priority.
26 * multiCommand.registerChildCommand( commandBar, { priority: 'low' } );
27 *
28 * // Enable one of the commands.
29 * commandBar.isEnabled = true;
30 *
31 * multiCommand.execute(); // Will execute commandBar.
32 * ```
33 */
34export default class MultiCommand extends Command {
35 /**
36 * Registered child commands definitions.
37 */
38 private _childCommandsDefinitions;
39 /**
40 * @inheritDoc
41 */
42 refresh(): void;
43 /**
44 * Executes the first enabled command which has the highest priority of all registered child commands.
45 *
46 * @returns The value returned by the {@link module:core/command~Command#execute `command.execute()`}.
47 */
48 execute(...args: Array<unknown>): unknown;
49 /**
50 * Registers a child command.
51 *
52 * @param options An object with configuration options.
53 * @param options.priority Priority of a command to register.
54 */
55 registerChildCommand(command: Command, options?: {
56 priority?: PriorityString;
57 }): void;
58 /**
59 * Checks if any of child commands is enabled.
60 */
61 private _checkEnabled;
62 /**
63 * Returns a first enabled command with the highest priority or `undefined` if none of them is enabled.
64 */
65 private _getFirstEnabledCommand;
66}