chore(architecture): enforce layer boundaries

This commit is contained in:
syntaxbullet
2026-07-03 09:34:21 +02:00
parent a8dbd26474
commit d7406e803a
14 changed files with 354 additions and 17 deletions

48
input/keyboard.ts Normal file
View File

@@ -0,0 +1,48 @@
import type { Dispatch } from "@commands/dispatcher";
export type Keybind = string;
export type KeybindEvent = {
key: string;
code: string;
altKey: boolean;
ctrlKey: boolean;
metaKey: boolean;
shiftKey: boolean;
};
export type GlobalKeybindConsumer = (event: KeybindEvent) => boolean;
export type CommandKeybind = {
commandId: string;
payload?: unknown;
};
export type KeybindMap = ReadonlyMap<Keybind, CommandKeybind>;
export function keybindFromEvent(event: KeybindEvent): Keybind {
const parts = [
event.metaKey ? "Meta" : undefined,
event.ctrlKey ? "Ctrl" : undefined,
event.altKey ? "Alt" : undefined,
event.shiftKey ? "Shift" : undefined,
event.key,
].filter(Boolean);
return parts.join("+");
}
export function handleKeybind(options: {
event: KeybindEvent;
globalConsumer: GlobalKeybindConsumer;
keybindMap: KeybindMap;
dispatch: Dispatch;
}): boolean {
if (options.globalConsumer(options.event)) return true;
const command = options.keybindMap.get(keybindFromEvent(options.event));
if (!command) return false;
options.dispatch(command.commandId, command.payload);
return true;
}