Skip to content

Commit 8b9bf9c

Browse files
authored
Fix bug in hotkey modifier sorting
1 parent a991102 commit 8b9bf9c

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/hotkey.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,15 @@ function localizeMod(hotkey: string, platform?: string | undefined): string {
9696
return hotkey.replace('Mod', localModifier)
9797
}
9898

99+
const orderedModifiers = {
100+
Control: 0,
101+
Alt: 1,
102+
Meta: 2,
103+
Shift: 3
104+
}
105+
99106
function sortModifiers(hotkey: string): string {
100-
const key = hotkey.split('+').pop()
101-
const modifiers: string[] = []
102-
for (const modifier of ['Control', 'Alt', 'Meta', 'Shift']) {
103-
if (hotkey.includes(modifier)) {
104-
modifiers.push(modifier)
105-
}
106-
}
107-
if (key) modifiers.push(key)
108-
return modifiers.join('+')
107+
return hotkey.split('+')
108+
.sort((a, b) => (orderedModifiers[a] ?? Infinity) - (orderedModifiers[b] ?? Infinity))
109+
.join('+')
109110
}

test/test-normalize-hotkey.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ describe('normalizeHotkey', () => {
3131
// Modifier sorting
3232
['Shift+Alt+Meta+Control+m', 'Control+Alt+Meta+Shift+m'],
3333
['Shift+Alt+Mod+m', 'Control+Alt+Shift+m', 'win']
34+
// Edge case: only modifiers
35+
['Alt', 'Alt', 'win / linux'],
36+
['Alt+Mod', 'Control+Alt', 'win / linux']
3437
]
3538

3639
for (const [input, expected, platform = 'any platform'] of tests) {

0 commit comments

Comments
 (0)