Repository navigation
Expand file tree
/
Copy pathprojectScriptKeybindings.ts
More file actions
66 lines (58 loc) · 2.02 KB
/
Copy pathprojectScriptKeybindings.ts
File metadata and controls
66 lines (58 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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;
}