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 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<string>;
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<string, ShortcutHandler>) {
const disposer = registerShortcuts(shortcuts);
const keys = new Set(Object.keys(shortcuts));
const entry: ShortcutEntry = { keys, disposer };
shortcutEntries.push(entry);
},
public add(shortcuts: Record<string, ShortcutHandler>) {
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;
}
}
}

View File

@@ -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 <T extends Constructor<HassElement>>(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),