/** * Service for binding keyboard shortcuts to execute commands * * A keybinding is a sequence or key combination on a computer keyboard which * invokes commands. This service supports key bindings as a way of responding * to individual keys typed by a user. The key combination can be pressing a single * key or a sequence of keys one after the other. * * The service works together with the `CommandBus` to execute the commands and * a command needs to already be registered on the command bus in order to be * bound as a keyboard shortcut. * * The following example registers the command `MyCommand` to be handled by * `MyHandler` and creates a keybinding for it. The `bind` method takes the key * combination and command object as input. * * Later, if the keybinding no longer is needed it can be removed with the * `unbind` method. * * @example * // The command bus needs to have a handler registered for the command * // before it can be bound to a key sequence * const handler = new MyHandler(); * commandBus.register(MyCommand, handler); * * // Bind 'ctrl+s' to trigger the command * const command = new MyCommand(); * keybindingRegistry.bind('ctrl+s', command); * * // Remove the binding when we no longer need it to trigger the command * keybindingRegistry.unbind('ctrl+s') * @note This service is work in progress * @private */ export interface KeybindingRegistry { /** * Bind a command to a specific key combination to add it to the registry * * @param {string} keys the string representation of keys * @param {Object} command the command to trigger when keys are pressed * @throws Will throw an error if the command is invalid or not registered */ bind: (keys: string, command: object) => void; /** * Unbind a keybinding to remove it from the registry * * @param {string} keys the string representation of the key combination * @param {Object} command the command connected to the key combination */ unbind: (keys: string, command: object) => void; /** * Get a list of the key bindings that are currently in the registry * * @returns {Keybinding[]} the list of command and its specific key combination */ getKeybindings: () => Keybinding[]; /** * Checks if a key combination or a specific keybinding is already bound * * @param {string} keys the string representation of a key combination * @param {Object} command the command connected to the key combination * @returns {boolean} true if key combination already exists, else false */ isBound: (keys: string, command?: object) => boolean; } /** * A key combination and its corresponding command that will be triggered when * the key combination is pressed */ export interface Keybinding { /** * The string representation of key combination */ keys: string; /** * The command to trigger when the key combination is pressed */ command: object; }