UNPKG

1.76 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 */
8export class SemanticCommand {
9 constructor() {
10 this._commands = new Array();
11 }
12 /**
13 * The command IDs used by this semantic command.
14 */
15 get ids() {
16 return this._commands.map(c => c.id);
17 }
18 /**
19 * Add a command to the semantic group
20 *
21 * @param command Command to add
22 */
23 add(command) {
24 if (this._commands.map(c => c.id).includes(command.id)) {
25 throw Error(`Command ${command.id} is already defined.`);
26 }
27 this._commands.push({
28 isEnabled: () => true,
29 rank: SemanticCommand.DEFAULT_RANK,
30 ...command
31 });
32 }
33 /**
34 * Get the command id of the enabled command from this group
35 * for the given widget.
36 *
37 * @param widget Widget
38 * @returns Command id
39 */
40 getActiveCommandId(widget) {
41 var _a;
42 const commands = this._commands
43 .filter(c => c.isEnabled(widget))
44 .sort((a, b) => {
45 const rankDelta = a.rank - b.rank;
46 return rankDelta || (a.id < b.id ? -1 : 1);
47 });
48 const command = (_a = commands[0]) !== null && _a !== void 0 ? _a : { id: null };
49 return command.id;
50 }
51 /**
52 * Remove a command ID.
53 *
54 * @param id Command ID to remove
55 */
56 remove(id) {
57 const index = this._commands.findIndex(c => c.id === id);
58 if (index >= 0) {
59 this._commands.splice(index, 1);
60 }
61 }
62}
63/**
64 * Default rank for semantic command
65 */
66SemanticCommand.DEFAULT_RANK = 500;
67//# sourceMappingURL=semanticCommand.js.map
\No newline at end of file