Skip to content

Commit

Permalink
Merge branch 'master' into canary
Browse files Browse the repository at this point in the history
  • Loading branch information
chabou committed Sep 19, 2017
2 parents 43242cf + 4c71c99 commit d3cd9ae
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 5 deletions.
7 changes: 4 additions & 3 deletions app/config/keymaps.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {readFileSync} = require('fs');
const normalize = require('../utils/keymaps/normalize');
const {defaultPlatformKeyPath} = require('./paths');

const commands = {};
Expand All @@ -7,7 +8,7 @@ const keys = {};
const _setKeysForCommands = function(keymap) {
for (const command in keymap) {
if (command) {
commands[command] = keymap[command].toLowerCase();
commands[command] = normalize(keymap[command]);
}
}
};
Expand Down Expand Up @@ -38,8 +39,8 @@ const _extend = function(customsKeys) {
if (customsKeys) {
for (const command in customsKeys) {
if (command) {
commands[command] = customsKeys[command];
keys[customsKeys[command]] = command;
commands[command] = normalize(customsKeys[command]);
keys[normalize(customsKeys[command])] = command;
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions app/utils/keymaps/find-command-by-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const normalize = require('./normalize');

module.exports = (keys, commands) => {
return commands[normalize(keys)];
};
6 changes: 6 additions & 0 deletions app/utils/keymaps/get-command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const {getKeymaps} = require('../../config');
const findCommandByKeys = require('./find-command-by-keys');

module.exports = keys => {
return findCommandByKeys(keys, getKeymaps().keys);
};
17 changes: 17 additions & 0 deletions app/utils/keymaps/normalize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This function receives a keymap in any key order and returns
// the same keymap alphatetically sorted by the clients locale.
// eg.: cmd+alt+o -> alt+cmd+o
// We do this in order to normalize what the user defined to what we
// internally parse. By doing this, you can set your keymaps in any given order
// eg.: alt+cmd+o, cmd+alt+o, o+alt+cmd, etc. #2195
module.exports = keybinding => {
function sortAlphabetically(a, b) {
return a.localeCompare(b);
}

return keybinding
.toLowerCase()
.split('+')
.sort(sortAlphabetically)
.join('+');
};
4 changes: 2 additions & 2 deletions lib/utils/keymaps.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {remote} from 'electron';

const {getKeymaps} = remote.require('./config');
const getCommand = remote.require('./utils/keymaps/get-command');

export default function returnKey(e) {
let keys = [];
Expand Down Expand Up @@ -30,5 +30,5 @@ export default function returnKey(e) {
}

keys = keys.join('+');
return getKeymaps().keys[keys.toLowerCase()];
return getCommand(keys);
}
34 changes: 34 additions & 0 deletions test/unit/keymaps-find-command-by-keys.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import test from 'ava';
import findCommandByKeys from '../../app/utils/keymaps/find-command-by-keys';

const expectedCommand = 'test-command';
const expectedLocalizedCommand = 'test-localized-command';

const commands = {
'alt+p+shift': expectedCommand,
'ç+cmd+ctrl': expectedLocalizedCommand
};

test(`returns a command`, t => {
t.is(findCommandByKeys('alt+shift+p', commands), expectedCommand);

t.is(findCommandByKeys('shift+p+alt', commands), expectedCommand);

t.is(findCommandByKeys('p+alt+shift', commands), expectedCommand);

t.is(findCommandByKeys('alt+shift+P', commands), expectedCommand);

t.is(findCommandByKeys('Shift+P+Alt', commands), expectedCommand);
});

test(`returns a localized command`, t => {
t.is(findCommandByKeys('cmd+ctrl+ç', commands), expectedLocalizedCommand);

t.is(findCommandByKeys('ç+cmd+ctrl', commands), expectedLocalizedCommand);

t.is(findCommandByKeys('ctrl+ç+cmd', commands), expectedLocalizedCommand);

t.is(findCommandByKeys('ctrl+Ç+cmd', commands), expectedLocalizedCommand);

t.is(findCommandByKeys('Cmd+Ctrl+Ç', commands), expectedLocalizedCommand);
});
16 changes: 16 additions & 0 deletions test/unit/keymaps-normalize.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import test from 'ava';
import normalize from '../../app/utils/keymaps/normalize';

test(`is normalizing keymaps correctly`, t => {
const notNormalized = 'cmd+alt+o';
const normalized = 'alt+cmd+o';

t.is(normalize(notNormalized), normalized);
});

test(`is normalizing localized keymaps correctly`, t => {
const notNormalized = 'cmd+alt+ç';
const normalized = 'alt+ç+cmd';

t.is(normalize(notNormalized), normalized);
});

0 comments on commit d3cd9ae

Please sign in to comment.