Julius Marmingephase 5f20bbdf8 months agoHistory
59 lines · 1.8 KB · TypeScript
RawDownload
import {  KeybindingRule as KeybindingRuleSchema,  type KeybindingCommand,  type KeybindingRule,  type ResolvedKeybindingsConfig,} from "@t3tools/contracts";import { Schema } from "effect"; export const PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE = "Invalid keybinding."; function normalizeProjectScriptKeybindingInput(  keybinding: string | null | undefined,): string | null {  const trimmed = keybinding?.trim() ?? "";  return trimmed.length > 0 ? trimmed : null;} export function decodeProjectScriptKeybindingRule(input: {  keybinding: string | null | undefined;  command: KeybindingCommand;}): KeybindingRule | null {  const normalizedKey = normalizeProjectScriptKeybindingInput(input.keybinding);  if (!normalizedKey) return null;   const decoded = Schema.decodeUnknownOption(KeybindingRuleSchema)({    key: normalizedKey,    command: input.command,  });  if (decoded._tag === "None") {    throw new Error(PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE);  }  return decoded.value;} export function keybindingValueForCommand(  keybindings: ResolvedKeybindingsConfig,  command: KeybindingCommand,): string | null {  for (let index = keybindings.length - 1; index >= 0; index -= 1) {    const binding = keybindings[index];    if (!binding || binding.command !== command) continue;     const parts: string[] = [];    if (binding.shortcut.modKey) parts.push("mod");    if (binding.shortcut.ctrlKey) parts.push("ctrl");    if (binding.shortcut.metaKey) parts.push("meta");    if (binding.shortcut.altKey) parts.push("alt");    if (binding.shortcut.shiftKey) parts.push("shift");    const keyToken =      binding.shortcut.key === " "        ? "space"        : binding.shortcut.key === "escape"          ? "esc"          : binding.shortcut.key;    parts.push(keyToken);    return parts.join("+");  }  return null;}