diff --git a/src/common/keyboard/shortcuts.ts b/src/common/keyboard/shortcuts.ts index 3b2e1067d1..8aa45e2f3e 100644 --- a/src/common/keyboard/shortcuts.ts +++ b/src/common/keyboard/shortcuts.ts @@ -3,7 +3,7 @@ import { canOverrideAlphanumericInput } from "../dom/can-override-input"; export type ShortcutHandler = (event: KeyboardEvent) => void; -export interface ShortcutManager { +export interface ShortcutManagerInterface { /** * Add a group of keyboard shortcuts to the manager. * @@ -23,6 +23,9 @@ export interface ShortcutManager { } interface ShortcutEntry { + /** + * The keys that the shortcut is registered to. + */ keys: Set; disposer: () => void; } @@ -55,42 +58,38 @@ function registerShortcuts( } /** - * Create a shortcut manager that can add and dispose shortcuts. - * - * @returns A shortcut manager containing the add and remove methods. + * A class that can add and remove keyboard shortcuts. */ -export function createShortcutManager(): ShortcutManager { - const shortcutEntries: ShortcutEntry[] = []; +export class ShortcutManager implements ShortcutManagerInterface { + private shortcutEntries: ShortcutEntry[] = []; - return { - add(shortcuts: Record) { - const disposer = registerShortcuts(shortcuts); - const keys = new Set(Object.keys(shortcuts)); - const entry: ShortcutEntry = { keys, disposer }; - shortcutEntries.push(entry); - }, + public add(shortcuts: Record) { + const disposer = registerShortcuts(shortcuts); + const keys = new Set(Object.keys(shortcuts)); + const entry: ShortcutEntry = { keys, disposer }; + this.shortcutEntries.push(entry); + } - remove(keys?: string[]) { - if (keys) { - const entriesToRemove: ShortcutEntry[] = []; + public remove(keys?: string[]) { + if (keys) { + const entriesToRemove: ShortcutEntry[] = []; - for (const entry of shortcutEntries) { - if (keys.some((key) => entry.keys.has(key))) { - entry.disposer(); - entriesToRemove.push(entry); - } + for (const entry of this.shortcutEntries) { + if (keys.some((key) => entry.keys.has(key))) { + entry.disposer(); + entriesToRemove.push(entry); } - - entriesToRemove.forEach((entry) => { - const index = shortcutEntries.indexOf(entry); - if (index !== -1) { - shortcutEntries.splice(index, 1); - } - }); - } else { - shortcutEntries.forEach((entry) => entry.disposer()); - shortcutEntries.length = 0; } - }, - }; + + entriesToRemove.forEach((entry) => { + const index = this.shortcutEntries.indexOf(entry); + if (index !== -1) { + this.shortcutEntries.splice(index, 1); + } + }); + } else { + this.shortcutEntries.forEach((entry) => entry.disposer()); + this.shortcutEntries.length = 0; + } + } } diff --git a/src/state/quick-bar-mixin.ts b/src/state/quick-bar-mixin.ts index bb3e920d71..258a629b99 100644 --- a/src/state/quick-bar-mixin.ts +++ b/src/state/quick-bar-mixin.ts @@ -11,7 +11,7 @@ import type { Constructor, HomeAssistant } from "../types"; import { storeState } from "../util/ha-pref-storage"; import { showToast } from "../util/toast"; import type { HassElement } from "./hass-element"; -import { createShortcutManager } from "../common/keyboard/shortcuts"; +import { ShortcutManager } from "../common/keyboard/shortcuts"; import { extractSearchParamsObject } from "../common/url/search-params"; import { showVoiceCommandDialog } from "../dialogs/voice-command-dialog/show-ha-voice-command-dialog"; import { canOverrideAlphanumericInput } from "../common/dom/can-override-input"; @@ -62,7 +62,7 @@ export default >(superClass: T) => } private _registerShortcut() { - const shortcutManager = createShortcutManager(); + const shortcutManager = new ShortcutManager(); shortcutManager.add({ // Those are for latin keyboards that have e, c, m keys e: (ev) => this._showQuickBar(ev),