Make class

This commit is contained in:
Aidan Timson
2025-09-29 10:10:50 +01:00
parent f5fa606ac2
commit 6f7be230d8
2 changed files with 34 additions and 35 deletions

View File

@@ -3,7 +3,7 @@ import { canOverrideAlphanumericInput } from "../dom/can-override-input";
export type ShortcutHandler = (event: KeyboardEvent) => void; export type ShortcutHandler = (event: KeyboardEvent) => void;
export interface ShortcutManager { export interface ShortcutManagerInterface {
/** /**
* Add a group of keyboard shortcuts to the manager. * Add a group of keyboard shortcuts to the manager.
* *
@@ -23,6 +23,9 @@ export interface ShortcutManager {
} }
interface ShortcutEntry { interface ShortcutEntry {
/**
* The keys that the shortcut is registered to.
*/
keys: Set<string>; keys: Set<string>;
disposer: () => void; disposer: () => void;
} }
@@ -55,26 +58,23 @@ function registerShortcuts(
} }
/** /**
* Create a shortcut manager that can add and dispose shortcuts. * A class that can add and remove keyboard shortcuts.
*
* @returns A shortcut manager containing the add and remove methods.
*/ */
export function createShortcutManager(): ShortcutManager { export class ShortcutManager implements ShortcutManagerInterface {
const shortcutEntries: ShortcutEntry[] = []; private shortcutEntries: ShortcutEntry[] = [];
return { public add(shortcuts: Record<string, ShortcutHandler>) {
add(shortcuts: Record<string, ShortcutHandler>) {
const disposer = registerShortcuts(shortcuts); const disposer = registerShortcuts(shortcuts);
const keys = new Set(Object.keys(shortcuts)); const keys = new Set(Object.keys(shortcuts));
const entry: ShortcutEntry = { keys, disposer }; const entry: ShortcutEntry = { keys, disposer };
shortcutEntries.push(entry); this.shortcutEntries.push(entry);
}, }
remove(keys?: string[]) { public remove(keys?: string[]) {
if (keys) { if (keys) {
const entriesToRemove: ShortcutEntry[] = []; const entriesToRemove: ShortcutEntry[] = [];
for (const entry of shortcutEntries) { for (const entry of this.shortcutEntries) {
if (keys.some((key) => entry.keys.has(key))) { if (keys.some((key) => entry.keys.has(key))) {
entry.disposer(); entry.disposer();
entriesToRemove.push(entry); entriesToRemove.push(entry);
@@ -82,15 +82,14 @@ export function createShortcutManager(): ShortcutManager {
} }
entriesToRemove.forEach((entry) => { entriesToRemove.forEach((entry) => {
const index = shortcutEntries.indexOf(entry); const index = this.shortcutEntries.indexOf(entry);
if (index !== -1) { if (index !== -1) {
shortcutEntries.splice(index, 1); this.shortcutEntries.splice(index, 1);
} }
}); });
} else { } else {
shortcutEntries.forEach((entry) => entry.disposer()); this.shortcutEntries.forEach((entry) => entry.disposer());
shortcutEntries.length = 0; this.shortcutEntries.length = 0;
}
} }
},
};
} }

View File

@@ -11,7 +11,7 @@ import type { Constructor, HomeAssistant } from "../types";
import { storeState } from "../util/ha-pref-storage"; import { storeState } from "../util/ha-pref-storage";
import { showToast } from "../util/toast"; import { showToast } from "../util/toast";
import type { HassElement } from "./hass-element"; 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 { extractSearchParamsObject } from "../common/url/search-params";
import { showVoiceCommandDialog } from "../dialogs/voice-command-dialog/show-ha-voice-command-dialog"; import { showVoiceCommandDialog } from "../dialogs/voice-command-dialog/show-ha-voice-command-dialog";
import { canOverrideAlphanumericInput } from "../common/dom/can-override-input"; import { canOverrideAlphanumericInput } from "../common/dom/can-override-input";
@@ -62,7 +62,7 @@ export default <T extends Constructor<HassElement>>(superClass: T) =>
} }
private _registerShortcut() { private _registerShortcut() {
const shortcutManager = createShortcutManager(); const shortcutManager = new ShortcutManager();
shortcutManager.add({ shortcutManager.add({
// Those are for latin keyboards that have e, c, m keys // Those are for latin keyboards that have e, c, m keys
e: (ev) => this._showQuickBar(ev), e: (ev) => this._showQuickBar(ev),