From 8e65408b1c2dec09ab9c354e0e738dc59d6fb27e Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Mon, 1 Mar 2021 20:50:08 +0000 Subject: [PATCH] Prevent duplicate keyboard shortcut from being assigned --- .../editor-client/src/js/ui/keyboard.js | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) 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 d5ba80273..cca1ed90e 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 @@ -20,6 +20,9 @@ RED.keyboard = (function() { var handlersActive = true; var handlers = {}; + + var knownShortcuts; + var partialState; var keyMap = { @@ -453,14 +456,21 @@ RED.keyboard = (function() { container.addClass('keyboard-shortcut-entry-expanded'); var keyInput = $('').attr('placeholder',RED._('keyboard.unassigned')).val(object.key||"").appendTo(key); - keyInput.on("keyup",function(e) { - if (e.keyCode === 13) { + keyInput.on("change paste keyup",function(e) { + if (e.keyCode === 13 && !$(this).hasClass("input-error")) { return endEditShortcut(); } + if (e.keyCode === 27) { + return endEditShortcut(true); + } var currentVal = $(this).val(); currentVal = currentVal.trim(); var valid = (currentVal === "" || RED.keyboard.validateKey(currentVal)); + if (valid && currentVal !== "") { + valid = !knownShortcuts.has(scopeSelect.val()+":"+currentVal.toLowerCase()); + } $(this).toggleClass("input-error",!valid); + okButton.attr("disabled",!valid); }) var scopeSelect = $('').appendTo(scope); @@ -469,6 +479,9 @@ RED.keyboard = (function() { object.scope = "red-ui-workspace"; } scopeSelect.val(object.scope||'*'); + scopeSelect.on("change", function() { + keyInput.trigger("change"); + }) var div = $('
').appendTo(scope); var okButton = $('').appendTo(div); @@ -521,6 +534,7 @@ RED.keyboard = (function() { keyDiv.empty(); scopeDiv.empty(); if (object.key) { + knownShortcuts.delete(object.scope+":"+object.key); RED.keyboard.remove(object.key,true); } container.find(".keyboard-shortcut-entry-text i").css("opacity",1); @@ -535,10 +549,10 @@ RED.keyboard = (function() { $("").text(scope).appendTo(scopeDiv); object.key = key; object.scope = scope; + knownShortcuts.add(object.scope+":"+object.key); RED.keyboard.add(object.scope,object.key,object.id,true); } - var userKeymap = RED.settings.get('editor.keymap', {}); var shortcut = RED.keyboard.getShortcut(object.id); userKeymap[object.id] = { @@ -633,7 +647,11 @@ RED.keyboard = (function() { var Bid = B.id.replace(/^.*:/,"").replace(/[ -]/g,"").toLowerCase(); return Aid.localeCompare(Bid); }); + knownShortcuts = new Set(); shortcuts.forEach(function(s) { + if (s.key) { + knownShortcuts.add(s.scope+":"+s.key); + } shortcutList.editableList('addItem',s); }); return pane;