UNPKG

1.39 kBJavaScriptView Raw
1/*
2 * Copyright (c) Jupyter Development Team.
3 * Distributed under the terms of the Modified BSD License.
4 */
5/**
6 * Semantic group of commands
7 */
8class SemanticCommand {
9 constructor() {
10 this._commands = new Array();
11 }
12 /**
13 * Add a command to the semantic group
14 *
15 * @param command Command to add
16 */
17 add(command) {
18 if (this._commands.map(c => c.id).includes(command.id)) {
19 throw Error(`Command ${command.id} is already defined.`);
20 }
21 this._commands.push({
22 isEnabled: () => true,
23 rank: SemanticCommand.DEFAULT_RANK,
24 ...command
25 });
26 }
27 /**
28 * Get the command id of the enabled command from this group
29 * for the given widget.
30 *
31 * @param widget Widget
32 * @returns Command id
33 */
34 getActiveCommandId(widget) {
35 var _a;
36 const commands = this._commands
37 .filter(c => c.isEnabled(widget))
38 .sort((a, b) => {
39 const rankDelta = a.rank - b.rank;
40 return rankDelta || (a.id < b.id ? -1 : 1);
41 });
42 const command = (_a = commands[0]) !== null && _a !== void 0 ? _a : { id: null };
43 return command.id;
44 }
45}
46/**
47 * Default rank for semantic command
48 */
49SemanticCommand.DEFAULT_RANK = 500;
50export { SemanticCommand };
51//# sourceMappingURL=semanticCommand.js.map
\No newline at end of file