Skip to content

Commit 4ff070b

Browse files
authored
Merge pull request #146 from github/fix-sort-modifiers
Fix double-modifier bug in hotkey modifier sorting
2 parents a991102 + dd025b5 commit 4ff070b

File tree

3 files changed

+15
-50
lines changed

3 files changed

+15
-50
lines changed

package-lock.json

Lines changed: 0 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hotkey.ts

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

99+
const orderedModifiers: Partial<Record<string, number>> = {
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
108+
.split('+')
109+
.sort((a, b) => (orderedModifiers[a] ?? Infinity) - (orderedModifiers[b] ?? Infinity))
110+
.join('+')
109111
}

test/test-normalize-hotkey.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ describe('normalizeHotkey', () => {
3030
['Mod+a', 'Control+a', undefined],
3131
// Modifier sorting
3232
['Shift+Alt+Meta+Control+m', 'Control+Alt+Meta+Shift+m'],
33-
['Shift+Alt+Mod+m', 'Control+Alt+Shift+m', 'win']
33+
['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)