2017-06-02 16:03:47 -08:00
|
|
|
const {readFileSync} = require('fs');
|
2017-09-19 15:36:06 -08:00
|
|
|
const normalize = require('../utils/keymaps/normalize');
|
2017-06-02 16:03:47 -08:00
|
|
|
const {defaultPlatformKeyPath} = require('./paths');
|
|
|
|
|
|
|
|
|
|
const commands = {};
|
|
|
|
|
const keys = {};
|
|
|
|
|
|
|
|
|
|
const _setKeysForCommands = function (keymap) {
|
|
|
|
|
for (const command in keymap) {
|
|
|
|
|
if (command) {
|
2017-09-19 15:36:06 -08:00
|
|
|
commands[command] = normalize(keymap[command]);
|
2017-06-02 16:03:47 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const _setCommandsForKeys = function (commands) {
|
|
|
|
|
for (const command in commands) {
|
|
|
|
|
if (command) {
|
|
|
|
|
keys[commands[command]] = command;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-09-25 05:21:59 -08:00
|
|
|
const _import = function (customKeys) {
|
2017-06-02 16:03:47 -08:00
|
|
|
try {
|
|
|
|
|
const mapping = JSON.parse(readFileSync(defaultPlatformKeyPath()));
|
|
|
|
|
_setKeysForCommands(mapping);
|
2017-09-25 05:21:59 -08:00
|
|
|
_setKeysForCommands(customKeys);
|
2017-06-02 16:03:47 -08:00
|
|
|
_setCommandsForKeys(commands);
|
|
|
|
|
|
|
|
|
|
return {commands, keys};
|
|
|
|
|
} catch (err) {}
|
|
|
|
|
};
|
|
|
|
|
|
2017-09-25 05:21:59 -08:00
|
|
|
const _extend = function (customKeys) {
|
|
|
|
|
if (customKeys) {
|
|
|
|
|
for (const command in customKeys) {
|
2017-06-02 16:03:47 -08:00
|
|
|
if (command) {
|
2017-09-25 05:21:59 -08:00
|
|
|
commands[command] = normalize(customKeys[command]);
|
|
|
|
|
keys[normalize(customKeys[command])] = command;
|
2017-06-02 16:03:47 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return {commands, keys};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
import: _import,
|
|
|
|
|
extend: _extend
|
|
|
|
|
};
|