-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
index.ts
1649 lines (1505 loc) · 46.7 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
/*-----------------------------------------------------------------------------
| Copyright (c) 2014-2017, PhosphorJS Contributors
|
| Distributed under the terms of the BSD 3-Clause License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
/**
* @packageDocumentation
* @module commands
*/
import { ArrayExt } from '@lumino/algorithm';
import {
JSONExt,
ReadonlyJSONObject,
ReadonlyPartialJSONObject
} from '@lumino/coreutils';
import { DisposableDelegate, IDisposable } from '@lumino/disposable';
import { Platform, Selector } from '@lumino/domutils';
import { getKeyboardLayout } from '@lumino/keyboard';
import { ISignal, Signal } from '@lumino/signaling';
import { VirtualElement } from '@lumino/virtualdom';
/**
* An object which manages a collection of commands.
*
* #### Notes
* A command registry can be used to populate a variety of action-based
* widgets, such as command palettes, menus, and toolbars.
*/
export class CommandRegistry {
/**
* A signal emitted when a command has changed.
*
* #### Notes
* This signal is useful for visual representations of commands which
* need to refresh when the state of a relevant command has changed.
*/
get commandChanged(): ISignal<this, CommandRegistry.ICommandChangedArgs> {
return this._commandChanged;
}
/**
* A signal emitted when a command has executed.
*
* #### Notes
* Care should be taken when consuming this signal. The command system is used
* by many components for many user actions. Handlers registered with this
* signal must return quickly to ensure the overall application remains responsive.
*/
get commandExecuted(): ISignal<this, CommandRegistry.ICommandExecutedArgs> {
return this._commandExecuted;
}
/**
* A signal emitted when a key binding is changed.
*/
get keyBindingChanged(): ISignal<
this,
CommandRegistry.IKeyBindingChangedArgs
> {
return this._keyBindingChanged;
}
/**
* A read-only array of the key bindings in the registry.
*/
get keyBindings(): ReadonlyArray<CommandRegistry.IKeyBinding> {
return this._keyBindings;
}
/**
* List the ids of the registered commands.
*
* @returns A new array of the registered command ids.
*/
listCommands(): string[] {
return Array.from(this._commands.keys());
}
/**
* Test whether a specific command is registered.
*
* @param id - The id of the command of interest.
*
* @returns `true` if the command is registered, `false` otherwise.
*/
hasCommand(id: string): boolean {
return this._commands.has(id);
}
/**
* Add a command to the registry.
*
* @param id - The unique id of the command.
*
* @param options - The options for the command.
*
* @returns A disposable which will remove the command.
*
* @throws An error if the given `id` is already registered.
*/
addCommand(
id: string,
options: CommandRegistry.ICommandOptions
): IDisposable {
// Throw an error if the id is already registered.
if (this._commands.has(id)) {
throw new Error(`Command '${id}' already registered.`);
}
// Add the command to the registry.
this._commands.set(id, Private.createCommand(options));
// Emit the `commandChanged` signal.
this._commandChanged.emit({ id, type: 'added' });
// Return a disposable which will remove the command.
return new DisposableDelegate(() => {
// Remove the command from the registry.
this._commands.delete(id);
// Emit the `commandChanged` signal.
this._commandChanged.emit({ id, type: 'removed' });
});
}
/**
* Notify listeners that the state of a command has changed.
*
* @param id - The id of the command which has changed. If more than
* one command has changed, this argument should be omitted.
*
* @throws An error if the given `id` is not registered.
*
* #### Notes
* This method should be called by the command author whenever the
* application state changes such that the results of the command
* metadata functions may have changed.
*
* This will cause the `commandChanged` signal to be emitted.
*/
notifyCommandChanged(id?: string): void {
if (id !== undefined && !this._commands.has(id)) {
throw new Error(`Command '${id}' is not registered.`);
}
this._commandChanged.emit({ id, type: id ? 'changed' : 'many-changed' });
}
/**
* Get the description for a specific command.
*
* @param id - The id of the command of interest.
*
* @param args - The arguments for the command.
*
* @returns The description for the command.
*/
describedBy(
id: string,
args: ReadonlyPartialJSONObject = JSONExt.emptyObject
): Promise<CommandRegistry.Description> {
let cmd = this._commands.get(id);
return Promise.resolve(
cmd?.describedBy.call(undefined, args) ?? { args: null }
);
}
/**
* Get the display label for a specific command.
*
* @param id - The id of the command of interest.
*
* @param args - The arguments for the command.
*
* @returns The display label for the command, or an empty string
* if the command is not registered.
*/
label(
id: string,
args: ReadonlyPartialJSONObject = JSONExt.emptyObject
): string {
let cmd = this._commands.get(id);
return cmd?.label.call(undefined, args) ?? '';
}
/**
* Get the mnemonic index for a specific command.
*
* @param id - The id of the command of interest.
*
* @param args - The arguments for the command.
*
* @returns The mnemonic index for the command, or `-1` if the
* command is not registered.
*/
mnemonic(
id: string,
args: ReadonlyPartialJSONObject = JSONExt.emptyObject
): number {
let cmd = this._commands.get(id);
return cmd ? cmd.mnemonic.call(undefined, args) : -1;
}
/**
* Get the icon renderer for a specific command.
*
* DEPRECATED: if set to a string value, the .icon field will
* function as an alias for the .iconClass field, for backwards
* compatibility. In the future when this is removed, the default
* return type will become undefined.
*
* @param id - The id of the command of interest.
*
* @param args - The arguments for the command.
*
* @returns The icon renderer for the command or `undefined`.
*/
icon(
id: string,
args: ReadonlyPartialJSONObject = JSONExt.emptyObject
): VirtualElement.IRenderer | undefined {
return this._commands.get(id)?.icon.call(undefined, args);
}
/**
* Get the icon class for a specific command.
*
* @param id - The id of the command of interest.
*
* @param args - The arguments for the command.
*
* @returns The icon class for the command, or an empty string if
* the command is not registered.
*/
iconClass(
id: string,
args: ReadonlyPartialJSONObject = JSONExt.emptyObject
): string {
let cmd = this._commands.get(id);
return cmd ? cmd.iconClass.call(undefined, args) : '';
}
/**
* Get the icon label for a specific command.
*
* @param id - The id of the command of interest.
*
* @param args - The arguments for the command.
*
* @returns The icon label for the command, or an empty string if
* the command is not registered.
*/
iconLabel(
id: string,
args: ReadonlyPartialJSONObject = JSONExt.emptyObject
): string {
let cmd = this._commands.get(id);
return cmd ? cmd.iconLabel.call(undefined, args) : '';
}
/**
* Get the short form caption for a specific command.
*
* @param id - The id of the command of interest.
*
* @param args - The arguments for the command.
*
* @returns The caption for the command, or an empty string if the
* command is not registered.
*/
caption(
id: string,
args: ReadonlyPartialJSONObject = JSONExt.emptyObject
): string {
let cmd = this._commands.get(id);
return cmd ? cmd.caption.call(undefined, args) : '';
}
/**
* Get the usage help text for a specific command.
*
* @param id - The id of the command of interest.
*
* @param args - The arguments for the command.
*
* @returns The usage text for the command, or an empty string if
* the command is not registered.
*/
usage(
id: string,
args: ReadonlyPartialJSONObject = JSONExt.emptyObject
): string {
let cmd = this._commands.get(id);
return cmd ? cmd.usage.call(undefined, args) : '';
}
/**
* Get the extra class name for a specific command.
*
* @param id - The id of the command of interest.
*
* @param args - The arguments for the command.
*
* @returns The class name for the command, or an empty string if
* the command is not registered.
*/
className(
id: string,
args: ReadonlyPartialJSONObject = JSONExt.emptyObject
): string {
let cmd = this._commands.get(id);
return cmd ? cmd.className.call(undefined, args) : '';
}
/**
* Get the dataset for a specific command.
*
* @param id - The id of the command of interest.
*
* @param args - The arguments for the command.
*
* @returns The dataset for the command, or an empty dataset if
* the command is not registered.
*/
dataset(
id: string,
args: ReadonlyPartialJSONObject = JSONExt.emptyObject
): CommandRegistry.Dataset {
let cmd = this._commands.get(id);
return cmd ? cmd.dataset.call(undefined, args) : {};
}
/**
* Test whether a specific command is enabled.
*
* @param id - The id of the command of interest.
*
* @param args - The arguments for the command.
*
* @returns A boolean indicating whether the command is enabled,
* or `false` if the command is not registered.
*/
isEnabled(
id: string,
args: ReadonlyPartialJSONObject = JSONExt.emptyObject
): boolean {
let cmd = this._commands.get(id);
return cmd ? cmd.isEnabled.call(undefined, args) : false;
}
/**
* Test whether a specific command is toggled.
*
* @param id - The id of the command of interest.
*
* @param args - The arguments for the command.
*
* @returns A boolean indicating whether the command is toggled,
* or `false` if the command is not registered.
*/
isToggled(
id: string,
args: ReadonlyPartialJSONObject = JSONExt.emptyObject
): boolean {
let cmd = this._commands.get(id);
return cmd ? cmd.isToggled.call(undefined, args) : false;
}
/**
* Test whether a specific command is toggleable.
*
* @param id - The id of the command of interest.
*
* @param args - The arguments for the command.
*
* @returns A boolean indicating whether the command is toggleable,
* or `false` if the command is not registered.
*/
isToggleable(
id: string,
args: ReadonlyJSONObject = JSONExt.emptyObject
): boolean {
let cmd = this._commands.get(id);
return cmd ? cmd.isToggleable : false;
}
/**
* Test whether a specific command is visible.
*
* @param id - The id of the command of interest.
*
* @param args - The arguments for the command.
*
* @returns A boolean indicating whether the command is visible,
* or `false` if the command is not registered.
*/
isVisible(
id: string,
args: ReadonlyPartialJSONObject = JSONExt.emptyObject
): boolean {
let cmd = this._commands.get(id);
return cmd ? cmd.isVisible.call(undefined, args) : false;
}
/**
* Execute a specific command.
*
* @param id - The id of the command of interest.
*
* @param args - The arguments for the command.
*
* @returns A promise which resolves with the result of the command.
*
* #### Notes
* The promise will reject if the command throws an exception,
* or if the command is not registered.
*/
execute(
id: string,
args: ReadonlyPartialJSONObject = JSONExt.emptyObject
): Promise<any> {
// Reject if the command is not registered.
let cmd = this._commands.get(id);
if (!cmd) {
return Promise.reject(new Error(`Command '${id}' not registered.`));
}
// Execute the command and reject if an exception is thrown.
let value: any;
try {
value = cmd.execute.call(undefined, args);
} catch (err) {
value = Promise.reject(err);
}
// Create the return promise which resolves the result.
let result = Promise.resolve(value);
// Emit the command executed signal.
this._commandExecuted.emit({ id, args, result });
// Return the result promise to the caller.
return result;
}
/**
* Add a key binding to the registry.
*
* @param options - The options for creating the key binding.
*
* @returns A disposable which removes the added key binding.
*
* #### Notes
* If multiple key bindings are registered for the same sequence, the
* binding with the highest selector specificity is executed first. A
* tie is broken by using the most recently added key binding.
*
* Ambiguous key bindings are resolved with a timeout. As an example,
* suppose two key bindings are registered: one with the key sequence
* `['Ctrl D']`, and another with `['Ctrl D', 'Ctrl W']`. If the user
* presses `Ctrl D`, the first binding cannot be immediately executed
* since the user may intend to complete the chord with `Ctrl W`. For
* such cases, a timer is used to allow the chord to be completed. If
* the chord is not completed before the timeout, the first binding
* is executed.
*/
addKeyBinding(options: CommandRegistry.IKeyBindingOptions): IDisposable {
// Create the binding for the given options.
let binding = Private.createKeyBinding(options);
// Add the key binding to the bindings array.
this._keyBindings.push(binding);
// Emit the `bindingChanged` signal.
this._keyBindingChanged.emit({ binding, type: 'added' });
// Return a disposable which will remove the binding.
return new DisposableDelegate(() => {
// Remove the binding from the array.
ArrayExt.removeFirstOf(this._keyBindings, binding);
// Emit the `bindingChanged` signal.
this._keyBindingChanged.emit({ binding, type: 'removed' });
});
}
/**
* Process a `'keydown'` event and invoke a matching key binding.
*
* @param event - The event object for a `'keydown'` event.
*
* #### Notes
* This should be called in response to a `'keydown'` event in order
* to invoke the command for the best matching key binding.
*
* The registry **does not** install its own listener for `'keydown'`
* events. This allows the application full control over the nodes
* and phase for which the registry processes `'keydown'` events.
*
* When the keydown event is processed, if the event target or any of its
* ancestor nodes has a `data-lm-suppress-shortcuts` attribute, its keydown
* events will not invoke commands.
*/
processKeydownEvent(event: KeyboardEvent): void {
// Bail immediately if playing back keystrokes
// or if the event has been processed
if (
event.defaultPrevented ||
this._replaying ||
CommandRegistry.isModifierKeyPressed(event)
) {
return;
}
// Get the normalized keystroke for the event.
let keystroke = CommandRegistry.keystrokeForKeydownEvent(event);
// If the keystroke is not valid for the keyboard layout, replay
// any suppressed events and clear the pending state.
if (!keystroke) {
this._replayKeydownEvents();
this._clearPendingState();
return;
}
// Add the keystroke to the current key sequence.
this._keystrokes.push(keystroke);
// Find the exact and partial matches for the key sequence.
let { exact, partial } = Private.matchKeyBinding(
this._keyBindings,
this._keystrokes,
event
);
// If there is no exact match and no partial match, replay
// any suppressed events and clear the pending state.
if (!exact && !partial) {
this._replayKeydownEvents();
this._clearPendingState();
return;
}
// Stop propagation of the event. If there is only a partial match,
// the event will be replayed if a final exact match never occurs.
event.preventDefault();
event.stopPropagation();
// If there is an exact match but no partial match, the exact match
// can be dispatched immediately. The pending state is cleared so
// the next key press starts from the default state.
if (exact && !partial) {
this._executeKeyBinding(exact);
this._clearPendingState();
return;
}
// If there is both an exact match and a partial match, the exact
// match is stored for future dispatch in case the timer expires
// before a more specific match is triggered.
if (exact) {
this._exactKeyMatch = exact;
}
// Store the event for possible playback in the future.
this._keydownEvents.push(event);
// (Re)start the timer to dispatch the most recent exact match
// in case the partial match fails to result in an exact match.
this._startTimer();
}
/**
* Start or restart the pending timeout.
*/
private _startTimer(): void {
this._clearTimer();
this._timerID = window.setTimeout(() => {
this._onPendingTimeout();
}, Private.CHORD_TIMEOUT);
}
/**
* Clear the pending timeout.
*/
private _clearTimer(): void {
if (this._timerID !== 0) {
clearTimeout(this._timerID);
this._timerID = 0;
}
}
/**
* Replay the keydown events which were suppressed.
*/
private _replayKeydownEvents(): void {
if (this._keydownEvents.length === 0) {
return;
}
this._replaying = true;
this._keydownEvents.forEach(Private.replayKeyEvent);
this._replaying = false;
}
/**
* Execute the command for the given key binding.
*
* If the command is missing or disabled, a warning will be logged.
*/
private _executeKeyBinding(binding: CommandRegistry.IKeyBinding): void {
let { command, args } = binding;
let newArgs: ReadonlyPartialJSONObject = {
_luminoEvent: { type: 'keybinding', keys: binding.keys },
...args
};
if (!this.hasCommand(command) || !this.isEnabled(command, newArgs)) {
let word = this.hasCommand(command) ? 'enabled' : 'registered';
let keys = binding.keys.join(', ');
let msg1 = `Cannot execute key binding '${keys}':`;
let msg2 = `command '${command}' is not ${word}.`;
console.warn(`${msg1} ${msg2}`);
return;
}
this.execute(command, newArgs);
}
/**
* Clear the internal pending state.
*/
private _clearPendingState(): void {
this._clearTimer();
this._exactKeyMatch = null;
this._keystrokes.length = 0;
this._keydownEvents.length = 0;
}
/**
* Handle the partial match timeout.
*/
private _onPendingTimeout(): void {
this._timerID = 0;
if (this._exactKeyMatch) {
this._executeKeyBinding(this._exactKeyMatch);
} else {
this._replayKeydownEvents();
}
this._clearPendingState();
}
private _timerID = 0;
private _replaying = false;
private _keystrokes: string[] = [];
private _keydownEvents: KeyboardEvent[] = [];
private _keyBindings: CommandRegistry.IKeyBinding[] = [];
private _exactKeyMatch: CommandRegistry.IKeyBinding | null = null;
private _commands = new Map<string, Private.ICommand>();
private _commandChanged = new Signal<
this,
CommandRegistry.ICommandChangedArgs
>(this);
private _commandExecuted = new Signal<
this,
CommandRegistry.ICommandExecutedArgs
>(this);
private _keyBindingChanged = new Signal<
this,
CommandRegistry.IKeyBindingChangedArgs
>(this);
}
/**
* The namespace for the `CommandRegistry` class statics.
*/
export namespace CommandRegistry {
/**
* A type alias for a user-defined command function.
*/
export type CommandFunc<T> = (args: ReadonlyPartialJSONObject) => T;
/**
* A type alias for a simple immutable string dataset.
*/
export type Dataset = { readonly [key: string]: string };
/**
* Commands description.
*/
export type Description = { args: ReadonlyJSONObject | null };
/**
* An options object for creating a command.
*
* #### Notes
* A command is an abstract representation of code to be executed along
* with metadata for describing how the command should be displayed in
* a visual representation.
*
* A command is a collection of functions, *not* methods. The command
* registry will always invoke the command functions with a `thisArg`
* which is `undefined`.
*/
export interface ICommandOptions {
/**
* The function to invoke when the command is executed.
*
* #### Notes
* This should return the result of the command (if applicable) or
* a promise which yields the result. The result is resolved as a
* promise and that promise is returned to the code which executed
* the command.
*
* This may be invoked even when `isEnabled` returns `false`.
*
* If called via a keybinding the passed args will include a `_luminoEvent` that specify the event type and keys pressed for customization.
*/
execute: CommandFunc<any | Promise<any>>;
/**
* JSON Schemas describing the command.
*
* #### Notes
* For now, the command arguments are the only one that can be
* described.
*/
describedBy?:
| Partial<Description>
| CommandFunc<Partial<Description> | Promise<Partial<Description>>>;
/**
* The label for the command.
*
* #### Notes
* This can be a string literal, or a function which returns the
* label based on the provided command arguments.
*
* The label is often used as the primary text for the command.
*
* The default value is an empty string.
*/
label?: string | CommandFunc<string>;
/**
* The index of the mnemonic character in the command's label.
*
* #### Notes
* This can be an index literal, or a function which returns the
* mnemonic index based on the provided command arguments.
*
* The mnemonic character is often used by menus to provide easy
* single-key keyboard access for triggering a menu item. It is
* typically rendered as an underlined character in the label.
*
* The default value is `-1`.
*/
mnemonic?: number | CommandFunc<number>;
/**
* The icon renderer for the command.
*
* #### Notes
* This can be an IRenderer object, or a function which returns the
* renderer based on the provided command arguments.
*
* The default value is `undefined`.
*/
icon?:
| VirtualElement.IRenderer
| undefined
| CommandFunc<VirtualElement.IRenderer | undefined>;
/**
* The icon class for the command.
*
* #### Notes
* This class name will be added to the icon node for the visual
* representation of the command.
*
* Multiple class names can be separated with white space.
*
* This can be a string literal, or a function which returns the
* icon based on the provided command arguments.
*
* The default value is an empty string.
*/
iconClass?: string | CommandFunc<string>;
/**
* The icon label for the command.
*
* #### Notes
* This label will be added as text to the icon node for the visual
* representation of the command.
*
* This can be a string literal, or a function which returns the
* label based on the provided command arguments.
*
* The default value is an empty string.
*/
iconLabel?: string | CommandFunc<string>;
/**
* The caption for the command.
*
* #### Notes
* This should be a simple one line description of the command. It
* is used by some visual representations to show quick info about
* the command.
*
* This can be a string literal, or a function which returns the
* caption based on the provided command arguments.
*
* The default value is an empty string.
*/
caption?: string | CommandFunc<string>;
/**
* The usage text for the command.
*
* #### Notes
* This should be a full description of the command, which includes
* information about the structure of the arguments and the type of
* the return value. It is used by some visual representations when
* displaying complete help info about the command.
*
* This can be a string literal, or a function which returns the
* usage text based on the provided command arguments.
*
* The default value is an empty string.
*/
usage?: string | CommandFunc<string>;
/**
* The general class name for the command.
*
* #### Notes
* This class name will be added to the primary node for the visual
* representation of the command.
*
* Multiple class names can be separated with white space.
*
* This can be a string literal, or a function which returns the
* class name based on the provided command arguments.
*
* The default value is an empty string.
*/
className?: string | CommandFunc<string>;
/**
* The dataset for the command.
*
* #### Notes
* The dataset values will be added to the primary node for the
* visual representation of the command.
*
* This can be a dataset object, or a function which returns the
* dataset object based on the provided command arguments.
*
* The default value is an empty dataset.
*/
dataset?: Dataset | CommandFunc<Dataset>;
/**
* A function which indicates whether the command is enabled.
*
* #### Notes
* Visual representations may use this value to display a disabled
* command as grayed-out or in some other non-interactive fashion.
*
* The default value is `() => true`.
*
* If called via a keybinding the passed args will include a `_luminoEvent` that specify the event type and keys pressed for customization
*/
isEnabled?: CommandFunc<boolean>;
/**
* A function which indicates whether the command is toggled.
*
* #### Notes
* Visual representations may use this value to display a toggled
* command in a different form, such as a check mark icon for a
* menu item or a depressed state for a toggle button.
*
* The default value is `() => false`.
*/
isToggled?: CommandFunc<boolean>;
/**
* A function which indicates whether the command is toggleable.
*
* #### Notes
* Visual representations may use this value to display a toggled command in
* a different form, such as a check box for a menu item or a depressed
* state for a toggle button. This attribute also allows for accessible
* interfaces to notify the user that the command corresponds to some state.
*
* The default value is `true` if an `isToggled` function is given, `false`
* otherwise.
*/
isToggleable?: boolean;
/**
* A function which indicates whether the command is visible.
*
* #### Notes
* Visual representations may use this value to hide or otherwise
* not display a non-visible command.
*
* The default value is `() => true`.
*/
isVisible?: CommandFunc<boolean>;
}
/**
* An arguments object for the `commandChanged` signal.
*/
export interface ICommandChangedArgs {
/**
* The id of the associated command.
*
* This will be `undefined` when the type is `'many-changed'`.
*/
readonly id: string | undefined;
/**
* Whether the command was added, removed, or changed.
*/
readonly type: 'added' | 'removed' | 'changed' | 'many-changed';
}
/**
* An arguments object for the `commandExecuted` signal.
*/
export interface ICommandExecutedArgs {
/**
* The id of the associated command.
*/
readonly id: string;
/**
* The arguments object passed to the command.
*/
readonly args: ReadonlyPartialJSONObject;
/**
* The promise which resolves with the result of the command.
*/
readonly result: Promise<any>;
}
/**
* An options object for creating a key binding.
*/
export interface IKeyBindingOptions {
/**
* The default key sequence for the key binding.
*
* A key sequence is composed of one or more keystrokes, where each
* keystroke is a combination of modifiers and a primary key.
*
* Most key sequences will contain a single keystroke. Key sequences
* with multiple keystrokes are called "chords", and are useful for
* implementing modal input (ala Vim).
*
* Each keystroke in the sequence should be of the form:
* `[<modifier 1> [<modifier 2> [<modifier N> ]]]<primary key>`
*
* The supported modifiers are: `Accel`, `Alt`, `Cmd`, `Ctrl`, and
* `Shift`. The `Accel` modifier is translated to `Cmd` on Mac and
* `Ctrl` on all other platforms. The `Cmd` modifier is ignored on
* non-Mac platforms.
*
* Keystrokes are case sensitive.
*
* **Examples:** `['Accel C']`, `['Shift F11']`, `['D', 'D']`
*/
keys: string[];
/**
* The CSS selector for the key binding.
*
* The key binding will only be invoked when the selector matches a
* node on the propagation path of the keydown event. This allows
* the key binding to be restricted to user-defined contexts.
*
* The selector must not contain commas.
*/
selector: string;
/**
* The id of the command to execute when the binding is matched.
*/