(hotkeyString: string)
| 66 | * @returns 解析结果 |
| 67 | */ |
| 68 | export function parseHotkey(hotkeyString: string): ParsedHotkey { |
| 69 | if (!hotkeyString || hotkeyString.trim() === '') { |
| 70 | return { |
| 71 | modifiers: [], |
| 72 | key: '', |
| 73 | isValid: false, |
| 74 | displayName: '', |
| 75 | errorMessage: '快捷键不能为空' |
| 76 | }; |
| 77 | } |
| 78 | |
| 79 | const parts = hotkeyString.toLowerCase().split('+').map(part => part.trim()); |
| 80 | |
| 81 | if (parts.length === 0) { |
| 82 | return { |
| 83 | modifiers: [], |
| 84 | key: '', |
| 85 | isValid: false, |
| 86 | displayName: '', |
| 87 | errorMessage: '无效的快捷键格式' |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | const modifiers: string[] = []; |
| 92 | let key = ''; |
| 93 | |
| 94 | // 检查每个部分 |
| 95 | for (let i = 0; i < parts.length; i++) { |
| 96 | const part = parts[i]; |
| 97 | |
| 98 | if (i === parts.length - 1) { |
| 99 | // 最后一个部分应该是普通按键 |
| 100 | if (REGULAR_KEYS[part as keyof typeof REGULAR_KEYS]) { |
| 101 | key = part; |
| 102 | } else { |
| 103 | return { |
| 104 | modifiers, |
| 105 | key: part, |
| 106 | isValid: false, |
| 107 | displayName: '', |
| 108 | errorMessage: `不支持的按键: ${part}` |
| 109 | }; |
| 110 | } |
| 111 | } else { |
| 112 | // 前面的部分应该是修饰键 |
| 113 | let isValidModifier = false; |
| 114 | for (const [modifierKey, aliases] of Object.entries(MODIFIER_KEYS)) { |
| 115 | if (aliases.includes(part)) { |
| 116 | if (!modifiers.includes(modifierKey)) { |
| 117 | modifiers.push(modifierKey); |
| 118 | } |
| 119 | isValidModifier = true; |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (!isValidModifier) { |
| 125 | return { |
nothing calls this directly
no test coverage detected