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

Merge pull request #2843 from node-red/theme-keymap

Allow default keymap to be overridden in settings file
This commit is contained in:
Nick O'Leary 2021-02-25 16:05:51 +00:00 committed by GitHub
commit 46bc331428
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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,38 +60,32 @@ 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 init() {
// Migrate from pre-0.18
migrateOldKeymap();
var currentEditorSettings = RED.settings.get('editor') || {}; function mergeKeymaps(defaultKeymap, themeKeymap) {
var userKeymap = currentEditorSettings.keymap || {}; // defaultKeymap has format: { scope: { action: key , action: key }}
// themeKeymap has format: {action: {scope,key}, action: {scope:key}}
$.getJSON("red/keymap.json",function(data) { var mergedKeymap = {};
for (var scope in data) { for (var scope in defaultKeymap) {
if (data.hasOwnProperty(scope)) { if (defaultKeymap.hasOwnProperty(scope)) {
var keys = data[scope]; var keys = defaultKeymap[scope];
for (var key in keys) { for (var key in keys) {
if (keys.hasOwnProperty(key)) { if (keys.hasOwnProperty(key)) {
if (!userKeymap.hasOwnProperty(keys[key])) { mergedKeymap[keys[key]] = {
addHandler(scope,key,keys[key],false);
}
defaultKeyMap[keys[key]] = {
scope:scope, scope:scope,
key:key, key:key,
user:false user:false
@ -100,6 +94,45 @@ RED.keyboard = (function() {
} }
} }
} }
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() {
// Migrate from pre-0.18
migrateOldKeymap();
var userKeymap = RED.settings.get('editor.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}}
var action;
for (action in keymap) {
if (keymap.hasOwnProperty(action)) {
if (!userKeymap.hasOwnProperty(action)) {
addHandler(keymap[action].scope,keymap[action].key,action,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);
} }
} }
} }