1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | export class SemanticCommand {
|
9 | constructor() {
|
10 | this._commands = new Array();
|
11 | }
|
12 | |
13 |
|
14 |
|
15 | get ids() {
|
16 | return this._commands.map(c => c.id);
|
17 | }
|
18 | |
19 |
|
20 |
|
21 |
|
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 |
|
35 |
|
36 |
|
37 |
|
38 |
|
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 |
|
53 |
|
54 |
|
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 |
|
65 |
|
66 | SemanticCommand.DEFAULT_RANK = 500;
|
67 |
|
\ | No newline at end of file |