mirror of
https://github.com/home-assistant/frontend.git
synced 2025-11-10 11:30:01 +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 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,42 +58,38 @@ 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 };
|
this.shortcutEntries.push(entry);
|
||||||
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);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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),
|
||||||
|
|||||||
Reference in New Issue
Block a user