From 1f8284229362b16a0feaf2f70767485462f3b143 Mon Sep 17 00:00:00 2001 From: Talon Date: Fri, 30 Aug 2024 16:15:15 +0200 Subject: [PATCH] Initial hotkeys for frontend --- frontend/src/views/main.ts | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/frontend/src/views/main.ts b/frontend/src/views/main.ts index 0b22a71..000adfb 100644 --- a/frontend/src/views/main.ts +++ b/frontend/src/views/main.ts @@ -36,6 +36,8 @@ export class MainView extends View { private messageElementMap: Map = new Map(); + private hotkeyMap: Map void> = new Map(); + public onActivate(): void { if (!state.currentChannel) { if (state.defaultChannelId) { @@ -71,11 +73,14 @@ export class MainView extends View { } } }); - } + document.addEventListener("keydown", (e) => this.handleHotkey(e)); + } public onDeactivate(): void { clearInterval(this.updateInterval); + // unregister hotkey + document.removeEventListener("keydown", (e) => this.handleHotkey(e)); } public onCreate(): void { @@ -124,6 +129,7 @@ export class MainView extends View { }); this.channelInfoButton.onClick(() => this.handleChannelInfoButton()); this.imageInput.onClick(async () => this.handleImageButton()); + this.setHotkeys(); } public onDestroy(): void { @@ -564,4 +570,23 @@ export class MainView extends View { const photo = await new TakePhotoDialog().open(); this.uploadImage(photo); } + + private setHotkeys() { + this.hotkeyMap.set("s", () => this.openSettingsDialog()); + this.hotkeyMap.set("c", () => this.channelSwitcher.focus()); + this.hotkeyMap.set("x", () => this.handleChannelInfoButton()); + this.hotkeyMap.set("f", () => this.openSearchDialog()); + this.hotkeyMap.set("v", () => this.handleVoiceMessageButton()); + this.hotkeyMap.set(" ", () => this.messageInput.focus()); + } + + private handleHotkey(e: KeyboardEvent) { + if (e.altKey) { + const action = this.hotkeyMap.get(e.key); + if (action) { + e.preventDefault(); + action(); + } + } + } } \ No newline at end of file