|
| 1 | +interface QuickToolModifiers { |
| 2 | + ctrlKey?: boolean; |
| 3 | + altKey?: boolean; |
| 4 | + shiftKey?: boolean; |
| 5 | + metaKey?: boolean; |
| 6 | +} |
| 7 | + |
| 8 | +interface QuickToolCommand { |
| 9 | + name: string; |
| 10 | + key?: string | null; |
| 11 | +} |
| 12 | + |
| 13 | +type ModifierName = "Ctrl" | "Alt" | "Shift" | "Meta"; |
| 14 | + |
| 15 | +const modifierAliases: Record<string, ModifierName> = { |
| 16 | + ctrl: "Ctrl", |
| 17 | + control: "Ctrl", |
| 18 | + alt: "Alt", |
| 19 | + option: "Alt", |
| 20 | + shift: "Shift", |
| 21 | + meta: "Meta", |
| 22 | + cmd: "Meta", |
| 23 | + command: "Meta", |
| 24 | + mod: "Ctrl", |
| 25 | +}; |
| 26 | + |
| 27 | +const modifierOrder: ModifierName[] = ["Ctrl", "Alt", "Shift", "Meta"]; |
| 28 | + |
| 29 | +export function mapQuickToolShiftText(char: string | null | undefined): string { |
| 30 | + switch (char) { |
| 31 | + case "1": |
| 32 | + return "!"; |
| 33 | + case "2": |
| 34 | + return "@"; |
| 35 | + case "3": |
| 36 | + return "#"; |
| 37 | + case "4": |
| 38 | + return "$"; |
| 39 | + case "5": |
| 40 | + return "%"; |
| 41 | + case "6": |
| 42 | + return "^"; |
| 43 | + case "7": |
| 44 | + return "&"; |
| 45 | + case "8": |
| 46 | + return "*"; |
| 47 | + case "9": |
| 48 | + return "("; |
| 49 | + case "0": |
| 50 | + return ")"; |
| 51 | + case "-": |
| 52 | + return "_"; |
| 53 | + case "=": |
| 54 | + return "+"; |
| 55 | + case "[": |
| 56 | + return "{"; |
| 57 | + case "]": |
| 58 | + return "}"; |
| 59 | + case "\\": |
| 60 | + return "|"; |
| 61 | + case ";": |
| 62 | + return ":"; |
| 63 | + case "'": |
| 64 | + return '"'; |
| 65 | + case ",": |
| 66 | + return "<"; |
| 67 | + case ".": |
| 68 | + return ">"; |
| 69 | + case "/": |
| 70 | + return "?"; |
| 71 | + default: |
| 72 | + return String(char ?? "").toUpperCase(); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +export function getQuickToolCombo( |
| 77 | + key: string, |
| 78 | + modifiers: QuickToolModifiers = {}, |
| 79 | +): string | null { |
| 80 | + const normalizedKey = normalizeKey(key); |
| 81 | + if (!normalizedKey) return null; |
| 82 | + |
| 83 | + const modifierParts = Object.entries(modifiers) |
| 84 | + .filter(([, enabled]) => enabled) |
| 85 | + .map(([modifier]) => modifier.replace(/Key$/, "")); |
| 86 | + return normalizeShortcutCombo([...modifierParts, normalizedKey].join("-")); |
| 87 | +} |
| 88 | + |
| 89 | +export function findQuickToolCommand( |
| 90 | + commands: QuickToolCommand[], |
| 91 | + key: string, |
| 92 | + modifiers: QuickToolModifiers = {}, |
| 93 | +): QuickToolCommand | null { |
| 94 | + const combo = getQuickToolCombo(key, modifiers); |
| 95 | + if (!combo) return null; |
| 96 | + |
| 97 | + return ( |
| 98 | + commands.find((command) => |
| 99 | + getShortcutAlternatives(command.key).includes(combo), |
| 100 | + ) || null |
| 101 | + ); |
| 102 | +} |
| 103 | + |
| 104 | +export function getShortcutAlternatives( |
| 105 | + keyString: string | null | undefined, |
| 106 | +): string[] { |
| 107 | + if (!keyString) return []; |
| 108 | + return String(keyString) |
| 109 | + .split("|") |
| 110 | + .map(normalizeShortcutCombo) |
| 111 | + .filter((combo): combo is string => Boolean(combo)); |
| 112 | +} |
| 113 | + |
| 114 | +export function normalizeShortcutCombo(combo: string): string | null { |
| 115 | + if (!combo) return null; |
| 116 | + const parts = splitShortcutCombo(combo); |
| 117 | + const modifiers: ModifierName[] = []; |
| 118 | + let key: string | null = null; |
| 119 | + |
| 120 | + parts.forEach((part) => { |
| 121 | + const modifier = modifierAliases[part.toLowerCase()]; |
| 122 | + if (modifier) { |
| 123 | + if (!modifiers.includes(modifier)) modifiers.push(modifier); |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + key = normalizeKey(part); |
| 128 | + }); |
| 129 | + |
| 130 | + if (!key) return null; |
| 131 | + const orderedModifiers = modifierOrder.filter((modifier) => |
| 132 | + modifiers.includes(modifier), |
| 133 | + ); |
| 134 | + return [...orderedModifiers, key].join("-"); |
| 135 | +} |
| 136 | + |
| 137 | +function splitShortcutCombo(combo: string): string[] { |
| 138 | + if (combo.endsWith("-")) { |
| 139 | + return [...combo.slice(0, -1).split("-").filter(Boolean), "-"]; |
| 140 | + } |
| 141 | + return combo |
| 142 | + .split("-") |
| 143 | + .map((part) => part.trim()) |
| 144 | + .filter(Boolean); |
| 145 | +} |
| 146 | + |
| 147 | +function normalizeKey(key: string | null | undefined): string | null { |
| 148 | + const text = String(key ?? ""); |
| 149 | + if (!text) return null; |
| 150 | + if (text.length === 1 && /[a-z]/i.test(text)) return text.toUpperCase(); |
| 151 | + return text; |
| 152 | +} |
0 commit comments