Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
67 lines
2.0 KiB
TypeScript
67 lines
2.0 KiB
TypeScript
import {
|
|
KeybindingRule as KeybindingRuleSchema,
|
|
type KeybindingCommand,
|
|
type KeybindingRule,
|
|
type ResolvedKeybindingsConfig,
|
|
} from "@t3tools/contracts";
|
|
import * as Schema from "effect/Schema";
|
|
|
|
export const PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE = "Invalid keybinding.";
|
|
|
|
const decodeKeybindingRule = Schema.decodeUnknownOption(KeybindingRuleSchema);
|
|
|
|
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 | null;
|
|
}): KeybindingRule | null {
|
|
const normalizedKey = normalizeProjectScriptKeybindingInput(input.keybinding);
|
|
if (!normalizedKey) return null;
|
|
|
|
if (input.command === null) {
|
|
throw new Error(PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE);
|
|
}
|
|
|
|
const decoded = decodeKeybindingRule({
|
|
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 | null,
|
|
): string | null {
|
|
if (command === null) return 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;
|
|
}
|