Files
herdrMario/herdr/herdr-composer/mods/herdr-composer/keys.js
T

46 lines
1.4 KiB
JavaScript

// Pure key intents of the composer: what a key press means, given the state of
// the composer. No gpui, no herdr - main.js acts on the answer.
/**
* @typedef {"none"|"send"|"send_now"|"hold"|"newline"|"interrupt"|"close_menu"|"close_panel"|"open_panel"|"menu_up"|"menu_down"|"menu_accept"} Intent
*/
/**
* What a key press means.
*
* @param {{key:string, shift?:boolean, ctrl?:boolean, alt?:boolean, composing?:boolean}} e
* @param {{hasKeyboard:boolean, menuOpen:boolean, panelOpen:boolean, empty:boolean, status:string|null}} ctx
* @returns {Intent}
*/
export function keyIntent(e, ctx) {
// Only the copy whose own field has the keyboard acts; an IME in the middle
// of a composition owns the key.
if (!e || !ctx || !ctx.hasKeyboard || e.composing) return "none";
const key = String(e.key ?? "").toLowerCase();
if (key === "enter") {
if (e.ctrl) return "send_now";
if (e.alt) return "hold";
if (e.shift) return "newline";
return "send";
}
if (key === "escape") {
if (ctx.menuOpen) return "close_menu";
if (ctx.panelOpen) return "close_panel";
if (ctx.empty && ctx.status === "working") return "interrupt";
return "none";
}
if (key === "r" && e.ctrl && !e.alt && !e.shift) return "open_panel";
if (ctx.menuOpen) {
if (key === "tab") return "menu_accept";
if (key === "up" || key === "arrowup") return "menu_up";
if (key === "down" || key === "arrowdown") return "menu_down";
}
return "none";
}