Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from github/theinterned/mod-character
Add a special `Mod` modifier that translates to either `Meta` on Mac or `Control` on other platforms
- Loading branch information
Showing
6 changed files
with
98 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Normalizes a hotkey string before comparing it to the serialized event | ||
* string produced by `eventToHotkeyString`. | ||
* - Replaces the `Mod` modifier with `Meta` on mac, `Control` on other | ||
* platforms. | ||
* - Ensures modifiers are sorted in a consistent order | ||
* @param hotkey a hotkey string | ||
* @param platform NOTE: this param is only intended to be used to mock `navigator.platform` in tests | ||
* @returns {string} normalized representation of the given hotkey string | ||
*/ | ||
export function normalizeHotkey(hotkey: string, platform?: string | undefined): string { | ||
let result: string | ||
result = localizeMod(hotkey, platform) | ||
result = sortModifiers(result) | ||
return result | ||
} | ||
|
||
const matchApplePlatform = /Mac|iPod|iPhone|iPad/i | ||
|
||
function localizeMod(hotkey: string, platform: string = navigator.platform): string { | ||
const localModifier = matchApplePlatform.test(platform) ? 'Meta' : 'Control' | ||
return hotkey.replace('Mod', localModifier) | ||
} | ||
|
||
function sortModifiers(hotkey: string): string { | ||
const key = hotkey.split('+').pop() | ||
const modifiers = [] | ||
for (const modifier of ['Control', 'Alt', 'Meta', 'Shift']) { | ||
if (hotkey.includes(modifier)) { | ||
modifiers.push(modifier) | ||
} | ||
} | ||
modifiers.push(key) | ||
return modifiers.join('+') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import {normalizeHotkey} from '../dist/index.js' | ||
|
||
describe('normalizeHotkey', () => { | ||
it('should exist', () => { | ||
assert.isDefined(normalizeHotkey) | ||
}) | ||
|
||
const tests = [ | ||
// Base case control tests | ||
['a', 'a'], | ||
['Control+a', 'Control+a'], | ||
['Meta+a', 'Meta+a'], | ||
['Control+Meta+a', 'Control+Meta+a'], | ||
// Mod should be localized based on platform | ||
['Mod+a', 'Control+a', 'win / linux'], | ||
['Mod+a', 'Meta+a', 'mac'], | ||
['Mod+a', 'Meta+a', 'iPod'], | ||
['Mod+a', 'Meta+a', 'iPhone'], | ||
['Mod+a', 'Meta+a', 'iPad'], | ||
['Mod+A', 'Control+A', 'win / linux'], | ||
['Mod+A', 'Meta+A', 'mac'], // TODO: on a mac upper-case keys are lowercased when Meta is pressed | ||
['Mod+9', 'Control+9', 'win / linux'], | ||
['Mod+9', 'Meta+9', 'mac'], | ||
['Mod+)', 'Control+)', 'win / linux'], | ||
['Mod+)', 'Meta+)', 'mac'], // TODO: on a mac upper-case keys are lowercased when Meta is pressed | ||
['Mod+Alt+a', 'Control+Alt+a', 'win / linux'], | ||
['Mod+Alt+a', 'Alt+Meta+a', 'mac'], | ||
// Modifier sorting | ||
['Shift+Alt+Meta+Control+m', 'Control+Alt+Meta+Shift+m'], | ||
['Shift+Alt+Mod+m', 'Control+Alt+Shift+m', 'win'] | ||
] | ||
|
||
for (const [input, expected, platform = 'any platform'] of tests) { | ||
it(`given "${input}", returns "${expected}" on ${platform}`, function (done) { | ||
assert.equal(normalizeHotkey(input, platform), expected) | ||
done() | ||
}) | ||
} | ||
}) |