UNPKG

23.9 kBTypeScriptView Raw
1import { ReadonlyJSONObject } from '@phosphor/coreutils';
2import { IDisposable } from '@phosphor/disposable';
3import { ISignal } from '@phosphor/signaling';
4/**
5 * An object which manages a collection of commands.
6 *
7 * #### Notes
8 * A command registry can be used to populate a variety of action-based
9 * widgets, such as command palettes, menus, and toolbars.
10 */
11export declare class CommandRegistry {
12 /**
13 * Construct a new command registry.
14 */
15 constructor();
16 /**
17 * A signal emitted when a command has changed.
18 *
19 * #### Notes
20 * This signal is useful for visual representations of commands which
21 * need to refresh when the state of a relevant command has changed.
22 */
23 readonly commandChanged: ISignal<this, CommandRegistry.ICommandChangedArgs>;
24 /**
25 * A signal emitted when a command has executed.
26 *
27 * #### Notes
28 * Care should be taken when consuming this signal. It is intended to
29 * be used largely for debugging and logging purposes. It should not
30 * be (ab)used for general purpose spying on command execution.
31 */
32 readonly commandExecuted: ISignal<this, CommandRegistry.ICommandExecutedArgs>;
33 /**
34 * A signal emitted when a key binding is changed.
35 */
36 readonly keyBindingChanged: ISignal<this, CommandRegistry.IKeyBindingChangedArgs>;
37 /**
38 * A read-only array of the key bindings in the registry.
39 */
40 readonly keyBindings: ReadonlyArray<CommandRegistry.IKeyBinding>;
41 /**
42 * List the ids of the registered commands.
43 *
44 * @returns A new array of the registered command ids.
45 */
46 listCommands(): string[];
47 /**
48 * Test whether a specific command is registered.
49 *
50 * @param id - The id of the command of interest.
51 *
52 * @returns `true` if the command is registered, `false` otherwise.
53 */
54 hasCommand(id: string): boolean;
55 /**
56 * Add a command to the registry.
57 *
58 * @param id - The unique id of the command.
59 *
60 * @param options - The options for the command.
61 *
62 * @returns A disposable which will remove the command.
63 *
64 * @throws An error if the given `id` is already registered.
65 */
66 addCommand(id: string, options: CommandRegistry.ICommandOptions): IDisposable;
67 /**
68 * Notify listeners that the state of a command has changed.
69 *
70 * @param id - The id of the command which has changed. If more than
71 * one command has changed, this argument should be omitted.
72 *
73 * @throws An error if the given `id` is not registered.
74 *
75 * #### Notes
76 * This method should be called by the command author whenever the
77 * application state changes such that the results of the command
78 * metadata functions may have changed.
79 *
80 * This will cause the `commandChanged` signal to be emitted.
81 */
82 notifyCommandChanged(id?: string): void;
83 /**
84 * Get the display label for a specific command.
85 *
86 * @param id - The id of the command of interest.
87 *
88 * @param args - The arguments for the command.
89 *
90 * @returns The display label for the command, or an empty string
91 * if the command is not registered.
92 */
93 label(id: string, args?: ReadonlyJSONObject): string;
94 /**
95 * Get the mnemonic index for a specific command.
96 *
97 * @param id - The id of the command of interest.
98 *
99 * @param args - The arguments for the command.
100 *
101 * @returns The mnemonic index for the command, or `-1` if the
102 * command is not registered.
103 */
104 mnemonic(id: string, args?: ReadonlyJSONObject): number;
105 /**
106 * @deprecated Use `iconClass()` instead.
107 */
108 icon(id: string, args?: ReadonlyJSONObject): string;
109 /**
110 * Get the icon class for a specific command.
111 *
112 * @param id - The id of the command of interest.
113 *
114 * @param args - The arguments for the command.
115 *
116 * @returns The icon class for the command, or an empty string if
117 * the command is not registered.
118 */
119 iconClass(id: string, args?: ReadonlyJSONObject): string;
120 /**
121 * Get the icon label for a specific command.
122 *
123 * @param id - The id of the command of interest.
124 *
125 * @param args - The arguments for the command.
126 *
127 * @returns The icon label for the command, or an empty string if
128 * the command is not registered.
129 */
130 iconLabel(id: string, args?: ReadonlyJSONObject): string;
131 /**
132 * Get the short form caption for a specific command.
133 *
134 * @param id - The id of the command of interest.
135 *
136 * @param args - The arguments for the command.
137 *
138 * @returns The caption for the command, or an empty string if the
139 * command is not registered.
140 */
141 caption(id: string, args?: ReadonlyJSONObject): string;
142 /**
143 * Get the usage help text for a specific command.
144 *
145 * @param id - The id of the command of interest.
146 *
147 * @param args - The arguments for the command.
148 *
149 * @returns The usage text for the command, or an empty string if
150 * the command is not registered.
151 */
152 usage(id: string, args?: ReadonlyJSONObject): string;
153 /**
154 * Get the extra class name for a specific command.
155 *
156 * @param id - The id of the command of interest.
157 *
158 * @param args - The arguments for the command.
159 *
160 * @returns The class name for the command, or an empty string if
161 * the command is not registered.
162 */
163 className(id: string, args?: ReadonlyJSONObject): string;
164 /**
165 * Get the dataset for a specific command.
166 *
167 * @param id - The id of the command of interest.
168 *
169 * @param args - The arguments for the command.
170 *
171 * @returns The dataset for the command, or an empty dataset if
172 * the command is not registered.
173 */
174 dataset(id: string, args?: ReadonlyJSONObject): CommandRegistry.Dataset;
175 /**
176 * Test whether a specific command is enabled.
177 *
178 * @param id - The id of the command of interest.
179 *
180 * @param args - The arguments for the command.
181 *
182 * @returns A boolean indicating whether the command is enabled,
183 * or `false` if the command is not registered.
184 */
185 isEnabled(id: string, args?: ReadonlyJSONObject): boolean;
186 /**
187 * Test whether a specific command is toggled.
188 *
189 * @param id - The id of the command of interest.
190 *
191 * @param args - The arguments for the command.
192 *
193 * @returns A boolean indicating whether the command is toggled,
194 * or `false` if the command is not registered.
195 */
196 isToggled(id: string, args?: ReadonlyJSONObject): boolean;
197 /**
198 * Test whether a specific command is visible.
199 *
200 * @param id - The id of the command of interest.
201 *
202 * @param args - The arguments for the command.
203 *
204 * @returns A boolean indicating whether the command is visible,
205 * or `false` if the command is not registered.
206 */
207 isVisible(id: string, args?: ReadonlyJSONObject): boolean;
208 /**
209 * Execute a specific command.
210 *
211 * @param id - The id of the command of interest.
212 *
213 * @param args - The arguments for the command.
214 *
215 * @returns A promise which resolves with the result of the command.
216 *
217 * #### Notes
218 * The promise will reject if the command throws an exception,
219 * or if the command is not registered.
220 */
221 execute(id: string, args?: ReadonlyJSONObject): Promise<any>;
222 /**
223 * Add a key binding to the registry.
224 *
225 * @param options - The options for creating the key binding.
226 *
227 * @returns A disposable which removes the added key binding.
228 *
229 * #### Notes
230 * If multiple key bindings are registered for the same sequence, the
231 * binding with the highest selector specificity is executed first. A
232 * tie is broken by using the most recently added key binding.
233 *
234 * Ambiguous key bindings are resolved with a timeout. As an example,
235 * suppose two key bindings are registered: one with the key sequence
236 * `['Ctrl D']`, and another with `['Ctrl D', 'Ctrl W']`. If the user
237 * presses `Ctrl D`, the first binding cannot be immediately executed
238 * since the user may intend to complete the chord with `Ctrl W`. For
239 * such cases, a timer is used to allow the chord to be completed. If
240 * the chord is not completed before the timeout, the first binding
241 * is executed.
242 */
243 addKeyBinding(options: CommandRegistry.IKeyBindingOptions): IDisposable;
244 /**
245 * Process a `'keydown'` event and invoke a matching key binding.
246 *
247 * @param event - The event object for a `'keydown'` event.
248 *
249 * #### Notes
250 * This should be called in response to a `'keydown'` event in order
251 * to invoke the command for the best matching key binding.
252 *
253 * The registry **does not** install its own listener for `'keydown'`
254 * events. This allows the application full control over the nodes
255 * and phase for which the registry processes `'keydown'` events.
256 */
257 processKeydownEvent(event: KeyboardEvent): void;
258 /**
259 * Start or restart the pending timeout.
260 */
261 private _startTimer;
262 /**
263 * Clear the pending timeout.
264 */
265 private _clearTimer;
266 /**
267 * Replay the keydown events which were suppressed.
268 */
269 private _replayKeydownEvents;
270 /**
271 * Execute the command for the given key binding.
272 *
273 * If the command is missing or disabled, a warning will be logged.
274 */
275 private _executeKeyBinding;
276 /**
277 * Clear the internal pending state.
278 */
279 private _clearPendingState;
280 /**
281 * Handle the partial match timeout.
282 */
283 private _onPendingTimeout;
284 private _timerID;
285 private _replaying;
286 private _keystrokes;
287 private _keydownEvents;
288 private _keyBindings;
289 private _exactKeyMatch;
290 private _commands;
291 private _commandChanged;
292 private _commandExecuted;
293 private _keyBindingChanged;
294}
295/**
296 * The namespace for the `CommandRegistry` class statics.
297 */
298export declare namespace CommandRegistry {
299 /**
300 * A type alias for a user-defined command function.
301 */
302 type CommandFunc<T> = (args: ReadonlyJSONObject) => T;
303 /**
304 * A type alias for a simple immutable string dataset.
305 */
306 type Dataset = {
307 readonly [key: string]: string;
308 };
309 /**
310 * An options object for creating a command.
311 *
312 * #### Notes
313 * A command is an abstract representation of code to be executed along
314 * with metadata for describing how the command should be displayed in
315 * a visual representation.
316 *
317 * A command is a collection of functions, *not* methods. The command
318 * registry will always invoke the command functions with a `thisArg`
319 * which is `undefined`.
320 */
321 interface ICommandOptions {
322 /**
323 * The function to invoke when the command is executed.
324 *
325 * #### Notes
326 * This should return the result of the command (if applicable) or
327 * a promise which yields the result. The result is resolved as a
328 * promise and that promise is returned to the code which executed
329 * the command.
330 *
331 * This may be invoked even when `isEnabled` returns `false`.
332 */
333 execute: CommandFunc<any | Promise<any>>;
334 /**
335 * The label for the command.
336 *
337 * #### Notes
338 * This can be a string literal, or a function which returns the
339 * label based on the provided command arguments.
340 *
341 * The label is often used as the primary text for the command.
342 *
343 * The default value is an empty string.
344 */
345 label?: string | CommandFunc<string>;
346 /**
347 * The index of the mnemonic character in the command's label.
348 *
349 * #### Notes
350 * This can be an index literal, or a function which returns the
351 * mnemonic index based on the provided command arguments.
352 *
353 * The mnemonic character is often used by menus to provide easy
354 * single-key keyboard access for triggering a menu item. It is
355 * typically rendered as an underlined character in the label.
356 *
357 * The default value is `-1`.
358 */
359 mnemonic?: number | CommandFunc<number>;
360 /**
361 * @deprecated Use `iconClass` instead.
362 */
363 icon?: string | CommandFunc<string>;
364 /**
365 * The icon class for the command.
366 *
367 * #### Notes
368 * This class name will be added to the icon node for the visual
369 * representation of the command.
370 *
371 * Multiple class names can be separated with white space.
372 *
373 * This can be a string literal, or a function which returns the
374 * icon based on the provided command arguments.
375 *
376 * The default value is an empty string.
377 */
378 iconClass?: string | CommandFunc<string>;
379 /**
380 * The icon label for the command.
381 *
382 * #### Notes
383 * This label will be added as text to the icon node for the visual
384 * representation of the command.
385 *
386 * This can be a string literal, or a function which returns the
387 * label based on the provided command arguments.
388 *
389 * The default value is an empty string.
390 */
391 iconLabel?: string | CommandFunc<string>;
392 /**
393 * The caption for the command.
394 *
395 * #### Notes
396 * This should be a simple one line description of the command. It
397 * is used by some visual representations to show quick info about
398 * the command.
399 *
400 * This can be a string literal, or a function which returns the
401 * caption based on the provided command arguments.
402 *
403 * The default value is an empty string.
404 */
405 caption?: string | CommandFunc<string>;
406 /**
407 * The usage text for the command.
408 *
409 * #### Notes
410 * This should be a full description of the command, which includes
411 * information about the structure of the arguments and the type of
412 * the return value. It is used by some visual representations when
413 * displaying complete help info about the command.
414 *
415 * This can be a string literal, or a function which returns the
416 * usage text based on the provided command arguments.
417 *
418 * The default value is an empty string.
419 */
420 usage?: string | CommandFunc<string>;
421 /**
422 * The general class name for the command.
423 *
424 * #### Notes
425 * This class name will be added to the primary node for the visual
426 * representation of the command.
427 *
428 * Multiple class names can be separated with white space.
429 *
430 * This can be a string literal, or a function which returns the
431 * class name based on the provided command arguments.
432 *
433 * The default value is an empty string.
434 */
435 className?: string | CommandFunc<string>;
436 /**
437 * The dataset for the command.
438 *
439 * #### Notes
440 * The dataset values will be added to the primary node for the
441 * visual representation of the command.
442 *
443 * This can be a dataset object, or a function which returns the
444 * dataset object based on the provided command arguments.
445 *
446 * The default value is an empty dataset.
447 */
448 dataset?: Dataset | CommandFunc<Dataset>;
449 /**
450 * A function which indicates whether the command is enabled.
451 *
452 * #### Notes
453 * Visual representations may use this value to display a disabled
454 * command as grayed-out or in some other non-interactive fashion.
455 *
456 * The default value is `() => true`.
457 */
458 isEnabled?: CommandFunc<boolean>;
459 /**
460 * A function which indicates whether the command is toggled.
461 *
462 * #### Notes
463 * Visual representations may use this value to display a toggled
464 * command in a different form, such as a check mark icon for a
465 * menu item or a depressed state for a toggle button.
466 *
467 * The default value is `() => false`.
468 */
469 isToggled?: CommandFunc<boolean>;
470 /**
471 * A function which indicates whether the command is visible.
472 *
473 * #### Notes
474 * Visual representations may use this value to hide or otherwise
475 * not display a non-visible command.
476 *
477 * The default value is `() => true`.
478 */
479 isVisible?: CommandFunc<boolean>;
480 }
481 /**
482 * An arguments object for the `commandChanged` signal.
483 */
484 interface ICommandChangedArgs {
485 /**
486 * The id of the associated command.
487 *
488 * This will be `undefined` when the type is `'many-changed'`.
489 */
490 readonly id: string | undefined;
491 /**
492 * Whether the command was added, removed, or changed.
493 */
494 readonly type: 'added' | 'removed' | 'changed' | 'many-changed';
495 }
496 /**
497 * An arguments object for the `commandExecuted` signal.
498 */
499 interface ICommandExecutedArgs {
500 /**
501 * The id of the associated command.
502 */
503 readonly id: string;
504 /**
505 * The arguments object passed to the command.
506 */
507 readonly args: ReadonlyJSONObject;
508 /**
509 * The promise which resolves with the result of the command.
510 */
511 readonly result: Promise<any>;
512 }
513 /**
514 * An options object for creating a key binding.
515 */
516 interface IKeyBindingOptions {
517 /**
518 * The default key sequence for the key binding.
519 *
520 * A key sequence is composed of one or more keystrokes, where each
521 * keystroke is a combination of modifiers and a primary key.
522 *
523 * Most key sequences will contain a single keystroke. Key sequences
524 * with multiple keystrokes are called "chords", and are useful for
525 * implementing modal input (ala Vim).
526 *
527 * Each keystroke in the sequence should be of the form:
528 * `[<modifier 1> [<modifier 2> [<modifier N> ]]]<primary key>`
529 *
530 * The supported modifiers are: `Accel`, `Alt`, `Cmd`, `Ctrl`, and
531 * `Shift`. The `Accel` modifier is translated to `Cmd` on Mac and
532 * `Ctrl` on all other platforms. The `Cmd` modifier is ignored on
533 * non-Mac platforms.
534 *
535 * Keystrokes are case sensitive.
536 *
537 * **Examples:** `['Accel C']`, `['Shift F11']`, `['D', 'D']`
538 */
539 keys: string[];
540 /**
541 * The CSS selector for the key binding.
542 *
543 * The key binding will only be invoked when the selector matches a
544 * node on the propagation path of the keydown event. This allows
545 * the key binding to be restricted to user-defined contexts.
546 *
547 * The selector must not contain commas.
548 */
549 selector: string;
550 /**
551 * The id of the command to execute when the binding is matched.
552 */
553 command: string;
554 /**
555 * The arguments for the command, if necessary.
556 *
557 * The default value is an empty object.
558 */
559 args?: ReadonlyJSONObject;
560 /**
561 * The key sequence to use when running on Windows.
562 *
563 * If provided, this will override `keys` on Windows platforms.
564 */
565 winKeys?: string[];
566 /**
567 * The key sequence to use when running on Mac.
568 *
569 * If provided, this will override `keys` on Mac platforms.
570 */
571 macKeys?: string[];
572 /**
573 * The key sequence to use when running on Linux.
574 *
575 * If provided, this will override `keys` on Linux platforms.
576 */
577 linuxKeys?: string[];
578 }
579 /**
580 * An object which represents a key binding.
581 *
582 * #### Notes
583 * A key binding is an immutable object created by a registry.
584 */
585 interface IKeyBinding {
586 /**
587 * The key sequence for the binding.
588 */
589 readonly keys: ReadonlyArray<string>;
590 /**
591 * The CSS selector for the binding.
592 */
593 readonly selector: string;
594 /**
595 * The command executed when the binding is matched.
596 */
597 readonly command: string;
598 /**
599 * The arguments for the command.
600 */
601 readonly args: ReadonlyJSONObject;
602 }
603 /**
604 * An arguments object for the `keyBindingChanged` signal.
605 */
606 interface IKeyBindingChangedArgs {
607 /**
608 * The key binding which was changed.
609 */
610 readonly binding: IKeyBinding;
611 /**
612 * Whether the key binding was added or removed.
613 */
614 readonly type: 'added' | 'removed';
615 }
616 /**
617 * An object which holds the results of parsing a keystroke.
618 */
619 interface IKeystrokeParts {
620 /**
621 * Whether `'Cmd'` appears in the keystroke.
622 */
623 cmd: boolean;
624 /**
625 * Whether `'Ctrl'` appears in the keystroke.
626 */
627 ctrl: boolean;
628 /**
629 * Whether `'Alt'` appears in the keystroke.
630 */
631 alt: boolean;
632 /**
633 * Whether `'Shift'` appears in the keystroke.
634 */
635 shift: boolean;
636 /**
637 * The primary key for the keystroke.
638 */
639 key: string;
640 }
641 /**
642 * Parse a keystroke into its constituent components.
643 *
644 * @param keystroke - The keystroke of interest.
645 *
646 * @returns The parsed components of the keystroke.
647 *
648 * #### Notes
649 * The keystroke should be of the form:
650 * `[<modifier 1> [<modifier 2> [<modifier N> ]]]<primary key>`
651 *
652 * The supported modifiers are: `Accel`, `Alt`, `Cmd`, `Ctrl`, and
653 * `Shift`. The `Accel` modifier is translated to `Cmd` on Mac and
654 * `Ctrl` on all other platforms.
655 *
656 * The parsing is tolerant and will not throw exceptions. Notably:
657 * - Duplicate modifiers are ignored.
658 * - Extra primary keys are ignored.
659 * - The order of modifiers and primary key is irrelevant.
660 * - The keystroke parts should be separated by whitespace.
661 * - The keystroke is case sensitive.
662 */
663 function parseKeystroke(keystroke: string): IKeystrokeParts;
664 /**
665 * Normalize a keystroke into a canonical representation.
666 *
667 * @param keystroke - The keystroke of interest.
668 *
669 * @returns The normalized representation of the keystroke.
670 *
671 * #### Notes
672 * This normalizes the keystroke by removing duplicate modifiers and
673 * extra primary keys, and assembling the parts in a canonical order.
674 *
675 * The `Cmd` modifier is ignored on non-Mac platforms.
676 */
677 function normalizeKeystroke(keystroke: string): string;
678 /**
679 * Format a keystroke for display on the local system.
680 */
681 function formatKeystroke(keystroke: string): string;
682 /**
683 * Create a normalized keystroke for a `'keydown'` event.
684 *
685 * @param event - The event object for a `'keydown'` event.
686 *
687 * @returns A normalized keystroke, or an empty string if the event
688 * does not represent a valid keystroke for the given layout.
689 */
690 function keystrokeForKeydownEvent(event: KeyboardEvent): string;
691}