mirror of
https://github.com/home-assistant/frontend.git
synced 2025-11-09 10:59:50 +00:00
Make class
This commit is contained in:
@@ -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,26 +58,23 @@ 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>) {
|
||||
public add(shortcuts: Record<string, ShortcutHandler>) {
|
||||
const disposer = registerShortcuts(shortcuts);
|
||||
const keys = new Set(Object.keys(shortcuts));
|
||||
const entry: ShortcutEntry = { keys, disposer };
|
||||
shortcutEntries.push(entry);
|
||||
},
|
||||
this.shortcutEntries.push(entry);
|
||||
}
|
||||
|
||||
remove(keys?: string[]) {
|
||||
public remove(keys?: string[]) {
|
||||
if (keys) {
|
||||
const entriesToRemove: ShortcutEntry[] = [];
|
||||
|
||||
for (const entry of shortcutEntries) {
|
||||
for (const entry of this.shortcutEntries) {
|
||||
if (keys.some((key) => entry.keys.has(key))) {
|
||||
entry.disposer();
|
||||
entriesToRemove.push(entry);
|
||||
@@ -82,15 +82,14 @@ export function createShortcutManager(): ShortcutManager {
|
||||
}
|
||||
|
||||
entriesToRemove.forEach((entry) => {
|
||||
const index = shortcutEntries.indexOf(entry);
|
||||
const index = this.shortcutEntries.indexOf(entry);
|
||||
if (index !== -1) {
|
||||
shortcutEntries.splice(index, 1);
|
||||
this.shortcutEntries.splice(index, 1);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
shortcutEntries.forEach((entry) => entry.disposer());
|
||||
shortcutEntries.length = 0;
|
||||
this.shortcutEntries.forEach((entry) => entry.disposer());
|
||||
this.shortcutEntries.length = 0;
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user