diff --git a/src/client/HelpModal.ts b/src/client/HelpModal.ts index 1b840c27b..3f47b2c8d 100644 --- a/src/client/HelpModal.ts +++ b/src/client/HelpModal.ts @@ -1,6 +1,6 @@ import { LitElement, html } from "lit"; -import { customElement, query } from "lit/decorators.js"; -import { getAltKey, getModifierKey, translateText } from "../client/Utils"; +import { customElement, query, state } from "lit/decorators.js"; +import { translateText } from "../client/Utils"; import "./components/Difficulties"; import "./components/Maps"; @@ -10,6 +10,7 @@ export class HelpModal extends LitElement { open: () => void; close: () => void; }; + @state() private keybinds: Record = this.getKeybinds(); createRenderRoot() { return this; @@ -25,6 +26,15 @@ export class HelpModal extends LitElement { super.disconnectedCallback(); } + private isKeybindObject(v: unknown): v is { value: string } { + return ( + typeof v === "object" && + v !== null && + "value" in v && + typeof (v as any).value === "string" + ); + } + private handleKeyDown = (e: KeyboardEvent) => { if (e.code === "Escape") { e.preventDefault(); @@ -32,7 +42,80 @@ export class HelpModal extends LitElement { } }; + private getKeybinds(): Record { + let saved: Record = {}; + try { + const parsed = JSON.parse( + localStorage.getItem("settings.keybinds") ?? "{}", + ); + saved = Object.fromEntries( + Object.entries(parsed) + .map(([k, v]) => { + if (this.isKeybindObject(v)) return [k, v.value]; + if (typeof v === "string") return [k, v]; + return [k, undefined]; + }) + .filter(([, v]) => typeof v === "string" && v !== "Null"), + ) as Record; + } catch (e) { + console.warn("Invalid keybinds JSON:", e); + } + + const isMac = /Mac/.test(navigator.userAgent); + return { + toggleView: "Space", + centerCamera: "KeyC", + moveUp: "KeyW", + moveDown: "KeyS", + moveLeft: "KeyA", + moveRight: "KeyD", + zoomOut: "KeyQ", + zoomIn: "KeyE", + attackRatioDown: "KeyT", + attackRatioUp: "KeyY", + shiftKey: "ShiftLeft", + modifierKey: isMac ? "MetaLeft" : "ControlLeft", + altKey: "AltLeft", + resetGfx: "KeyR", + ...saved, + }; + } + + private getKeyLabel(code: string): string { + if (!code) return ""; + + const specialLabels: Record = { + ShiftLeft: "⇧ Shift", + ShiftRight: "⇧ Shift", + ControlLeft: "Ctrl", + ControlRight: "Ctrl", + AltLeft: "Alt", + AltRight: "Alt", + MetaLeft: "⌘", + MetaRight: "⌘", + Space: "Space", + ArrowUp: "↑", + ArrowDown: "↓", + ArrowLeft: "←", + ArrowRight: "→", + }; + + if (specialLabels[code]) return specialLabels[code]; + if (code.startsWith("Key") && code.length === 4) return code.slice(3); + if (code.startsWith("Digit")) return code.slice(5); + if (code.startsWith("Numpad")) return `Num ${code.slice(6)}`; + + return code; + } + + private renderKey(code: string) { + const label = this.getKeyLabel(code); + return html`${label}`; + } + render() { + const keybinds = this.keybinds; + return html` - Space + ${this.renderKey(keybinds.toggleView)} ${translateText("help_modal.action_alt_view")}
- ⇧ Shift + ${this.renderKey(keybinds.shiftKey)} +
@@ -71,7 +154,7 @@ export class HelpModal extends LitElement {
- ${getModifierKey()} + ${this.renderKey(keybinds.modifierKey)} +
@@ -84,7 +167,7 @@ export class HelpModal extends LitElement {
- ${getAltKey()} + ${this.renderKey(keybinds.altKey)} +
@@ -95,28 +178,36 @@ export class HelpModal extends LitElement { ${translateText("help_modal.action_emote")} - C + ${this.renderKey(keybinds.centerCamera)} ${translateText("help_modal.action_center")} - Q / E + + ${this.renderKey(keybinds.zoomOut)} / + ${this.renderKey(keybinds.zoomIn)} + ${translateText("help_modal.action_zoom")} - W A - S D + ${this.renderKey(keybinds.moveUp)} + ${this.renderKey(keybinds.moveLeft)} + ${this.renderKey(keybinds.moveDown)} + ${this.renderKey(keybinds.moveRight)} ${translateText("help_modal.action_move_camera")} - 1 / 2 + + ${this.renderKey(keybinds.attackRatioDown)} / + ${this.renderKey(keybinds.attackRatioUp)} + ${translateText("help_modal.action_ratio_change")}
- ⇧ Shift + ${this.renderKey(keybinds.shiftKey)} +
@@ -133,8 +224,8 @@ export class HelpModal extends LitElement { - ${getAltKey()} + - R + ${this.renderKey(keybinds.altKey)} + + ${this.renderKey(keybinds.resetGfx)} ${translateText("help_modal.action_reset_gfx")} @@ -600,6 +691,7 @@ export class HelpModal extends LitElement { } public open() { + this.keybinds = this.getKeybinds(); this.modalEl?.open(); }