From ebe604e1afe07477490ae9de2317837a90e293de Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Mon, 9 Nov 2020 21:13:20 +0000 Subject: [PATCH] Ensure user keyboard shortcuts override defaults Fixes #2753 --- .../editor-client/src/js/ui/actions.js | 13 +++++++++- .../editor-client/src/js/ui/keyboard.js | 26 ++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/packages/node_modules/@node-red/editor-client/src/js/ui/actions.js b/packages/node_modules/@node-red/editor-client/src/js/ui/actions.js index 6da89a394..8ebc97cda 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/ui/actions.js +++ b/packages/node_modules/@node-red/editor-client/src/js/ui/actions.js @@ -21,7 +21,18 @@ RED.actions = (function() { var result = []; Object.keys(actions).forEach(function(action) { var shortcut = RED.keyboard.getShortcut(action); - result.push({id:action,scope:shortcut?shortcut.scope:undefined,key:shortcut?shortcut.key:undefined,user:shortcut?shortcut.user:undefined}) + var isUser = false; + if (shortcut) { + isUser = shortcut.user; + } else { + isUser = !!RED.keyboard.getUserShortcut(action); + } + result.push({ + id:action, + scope:shortcut?shortcut.scope:undefined, + key:shortcut?shortcut.key:undefined, + user:isUser + }) }) return result; } diff --git a/packages/node_modules/@node-red/editor-client/src/js/ui/keyboard.js b/packages/node_modules/@node-red/editor-client/src/js/ui/keyboard.js index a202957ae..f8ade40c3 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/ui/keyboard.js +++ b/packages/node_modules/@node-red/editor-client/src/js/ui/keyboard.js @@ -70,6 +70,11 @@ RED.keyboard = (function() { } } } + function getUserKey(action) { + var currentEditorSettings = RED.settings.get('editor') || {}; + var userKeymap = currentEditorSettings.keymap || {}; + return userKeymap[action]; + } function init() { // Migrate from pre-0.18 migrateOldKeymap(); @@ -255,6 +260,19 @@ RED.keyboard = (function() { var i=0; if (typeof key === 'string') { if (typeof cbdown === 'string') { + if (!ondown && !defaultKeyMap.hasOwnProperty(cbdown)) { + defaultKeyMap[cbdown] = { + scope:scope, + key:key, + user:false + } + } + if (!ondown) { + var userAction = getUserKey(cbdown); + if (userAction) { + return; + } + } actionToKeyMap[cbdown] = {scope:scope,key:key}; if (typeof ondown === 'boolean') { actionToKeyMap[cbdown].user = ondown; @@ -417,11 +435,9 @@ RED.keyboard = (function() { }); revertButton.on("click", function(e) { e.stopPropagation(); - RED.keyboard.revertToDefault(object.id); container.empty(); container.removeClass('keyboard-shortcut-entry-expanded'); - var shortcut = RED.keyboard.getShortcut(object.id); - var userKeymap = RED.settings.get('keymap') || {}; + // var userKeymap = RED.settings.get('keymap') || {}; var currentEditorSettings = RED.settings.get('editor') || {}; var userKeymap = currentEditorSettings.keymap || {}; @@ -429,6 +445,9 @@ RED.keyboard = (function() { currentEditorSettings.keymap = userKeymap; RED.settings.set('editor',currentEditorSettings); + RED.keyboard.revertToDefault(object.id); + + var shortcut = RED.keyboard.getShortcut(object.id); var obj = { id:object.id, scope:shortcut?shortcut.scope:undefined, @@ -589,6 +608,7 @@ RED.keyboard = (function() { getShortcut: function(actionName) { return actionToKeyMap[actionName]; }, + getUserShortcut: getUserKey, revertToDefault: revertToDefault, formatKey: formatKey, validateKey: validateKey,