1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Allow default keymap to be overridden in settings file

This commit is contained in:
Nick O'Leary 2021-01-28 16:42:16 +00:00
parent 3a0074d96e
commit 61690ecf4a
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 73 additions and 36 deletions

View File

@ -200,6 +200,10 @@ module.exports = {
themeSettings.projects = theme.projects; themeSettings.projects = theme.projects;
} }
if (theme.hasOwnProperty("keymap")) {
themeSettings.keymap = theme.keymap;
}
if (theme.theme) { if (theme.theme) {
themeSettings.theme = theme.theme; themeSettings.theme = theme.theme;
} }

View File

@ -43,10 +43,10 @@ RED.keyboard = (function() {
"?":191 // <- QWERTY specific "?":191 // <- QWERTY specific
} }
var metaKeyCodes = { var metaKeyCodes = {
16:true, 16: true,
17:true, 17: true,
18: true, 18: true,
91:true, 91: true,
93: true 93: true
} }
var actionToKeyMap = {} var actionToKeyMap = {}
@ -60,46 +60,79 @@ RED.keyboard = (function() {
} }
function migrateOldKeymap() { function migrateOldKeymap() {
// pre-0.18
if ('localStorage' in window && window['localStorage'] !== null) { if ('localStorage' in window && window['localStorage'] !== null) {
var oldKeyMap = localStorage.getItem("keymap"); var oldKeyMap = localStorage.getItem("keymap");
if (oldKeyMap !== null) { if (oldKeyMap !== null) {
localStorage.removeItem("keymap"); localStorage.removeItem("keymap");
var currentEditorSettings = RED.settings.get('editor') || {}; RED.settings.set('editor.keymap',JSON.parse(oldKeyMap));
currentEditorSettings.keymap = JSON.parse(oldKeyMap);
RED.settings.set('editor',currentEditorSettings);
} }
} }
} }
function getUserKey(action) { function getUserKey(action) {
var currentEditorSettings = RED.settings.get('editor') || {}; return RED.settings.get('editor.keymap',{})[action]
var userKeymap = currentEditorSettings.keymap || {};
return userKeymap[action];
} }
function mergeKeymaps(defaultKeymap, themeKeymap) {
// defaultKeymap has format: { scope: { action: key , action: key }}
// themeKeymap has format: {action: {scope,key}, action: {scope:key}}
var mergedKeymap = {};
for (var scope in defaultKeymap) {
if (defaultKeymap.hasOwnProperty(scope)) {
var keys = defaultKeymap[scope];
for (var key in keys) {
if (keys.hasOwnProperty(key)) {
mergedKeymap[keys[key]] = {
scope:scope,
key:key,
user:false
};
}
}
}
}
for (var action in themeKeymap) {
if (themeKeymap.hasOwnProperty(action)) {
if (!themeKeymap[action].key) {
// No key for this action - default is no keybinding
delete mergedKeymap[action]
} else {
mergedKeymap[action] = {
scope: themeKeymap[action].scope || "*",
key: themeKeymap[action].key,
user: false
}
if (mergedKeymap[action].scope === "workspace") {
mergedKeymap[action].scope = "red-ui-workspace";
}
}
}
}
return mergedKeymap;
}
function init() { function init() {
// Migrate from pre-0.18 // Migrate from pre-0.18
migrateOldKeymap(); migrateOldKeymap();
var currentEditorSettings = RED.settings.get('editor') || {}; var userKeymap = RED.settings.get('editor.keymap', {});
var userKeymap = currentEditorSettings.keymap || {}; $.getJSON("red/keymap.json",function(defaultKeymap) {
var keymap = mergeKeymaps(defaultKeymap, RED.settings.theme('keymap',{}));
// keymap has the format: {action: {scope,key}, action: {scope:key}}
$.getJSON("red/keymap.json",function(data) { var action;
for (var scope in data) { for (action in keymap) {
if (data.hasOwnProperty(scope)) { if (keymap.hasOwnProperty(action)) {
var keys = data[scope]; if (!userKeymap.hasOwnProperty(action)) {
for (var key in keys) { addHandler(keymap[action].scope,keymap[action].key,action,false);
if (keys.hasOwnProperty(key)) {
if (!userKeymap.hasOwnProperty(keys[key])) {
addHandler(scope,key,keys[key],false);
}
defaultKeyMap[keys[key]] = {
scope:scope,
key:key,
user:false
};
}
} }
defaultKeyMap[action] = keymap[action];
} }
} }
for (var action in userKeymap) { for (var action in userKeymap) {
if (userKeymap.hasOwnProperty(action) && userKeymap[action]) { if (userKeymap.hasOwnProperty(action) && userKeymap[action]) {
var obj = userKeymap[action]; var obj = userKeymap[action];
@ -437,13 +470,10 @@ RED.keyboard = (function() {
e.stopPropagation(); e.stopPropagation();
container.empty(); container.empty();
container.removeClass('keyboard-shortcut-entry-expanded'); container.removeClass('keyboard-shortcut-entry-expanded');
// var userKeymap = RED.settings.get('keymap') || {};
var currentEditorSettings = RED.settings.get('editor') || {}; var userKeymap = RED.settings.get('editor.keymap', {});
var userKeymap = currentEditorSettings.keymap || {};
userKeymap[object.id] = null; userKeymap[object.id] = null;
currentEditorSettings.keymap = userKeymap; RED.settings.set('editor.keymap',userKeymap);
RED.settings.set('editor',currentEditorSettings);
RED.keyboard.revertToDefault(object.id); RED.keyboard.revertToDefault(object.id);
@ -496,11 +526,14 @@ RED.keyboard = (function() {
RED.keyboard.add(object.scope,object.key,object.id,true); RED.keyboard.add(object.scope,object.key,object.id,true);
} }
var currentEditorSettings = RED.settings.get('editor') || {};
var userKeymap = currentEditorSettings.keymap || {}; var userKeymap = RED.settings.get('editor.keymap', {});
userKeymap[object.id] = RED.keyboard.getShortcut(object.id); var shortcut = RED.keyboard.getShortcut(object.id);
currentEditorSettings.keymap = userKeymap; userKeymap[object.id] = {
RED.settings.set('editor',currentEditorSettings); scope:shortcut.scope,
key:shortcut.key
}
RED.settings.set('editor.keymap',userKeymap);
} }
} }
} }