diff --git a/apps/common/embed/lib/controller/Shortcuts.js b/apps/common/embed/lib/controller/Shortcuts.js index 8f5b02fc85..444ad4ba48 100644 --- a/apps/common/embed/lib/controller/Shortcuts.js +++ b/apps/common/embed/lib/controller/Shortcuts.js @@ -233,6 +233,10 @@ shortcuts[foundIndex].ascShortcut = copyAscShortcut; } } + + if(actionsMap[actionType]) { + actionsMap[actionType].shortcuts = shortcuts.sort(_sortComparator); + } } }; @@ -294,6 +298,40 @@ return keys; }; + var _sortComparator = function(first, second) { + const priorityModifierKeys = ['asc_IsCommand', 'asc_IsCtrl', 'asc_IsAlt', 'asc_IsShift']; + function getWeight(ascShortcut) { + // Search for the first modifier key + let keyIndex = priorityModifierKeys.length; + for (let i = 0; i < priorityModifierKeys.length; i++) { + if (ascShortcut[priorityModifierKeys[i]]()) { + keyIndex = i; + break; + } + } + + if (keyIndex === priorityModifierKeys.length) return -1; + + // Count extra modifier keys + let extras = 0; + for (let j = 0; j < priorityModifierKeys.length; j++) { + if (j !== keyIndex && ascShortcut[priorityModifierKeys[j]]()) extras++; + } + + // weight = range for main key + “cost” of extra keys + return keyIndex * 100 + extras; + } + + if (first.ascShortcut.asc_IsLocked() && !second.ascShortcut.asc_IsLocked()) return -1; + if (!first.ascShortcut.asc_IsLocked() && second.ascShortcut.asc_IsLocked()) return 1; + + let wFirst = getWeight(first.ascShortcut); + let wSecond = getWeight(second.ascShortcut); + + if (wFirst !== wSecond) return wFirst - wSecond; + + return first.ascShortcut.asc_GetKeyCode() - second.ascShortcut.asc_GetKeyCode(); + }; // Utils var pairs = function(obj) { diff --git a/apps/common/main/lib/controller/Shortcuts.js b/apps/common/main/lib/controller/Shortcuts.js index f0c9a7e694..7c7463ae4e 100644 --- a/apps/common/main/lib/controller/Shortcuts.js +++ b/apps/common/main/lib/controller/Shortcuts.js @@ -159,6 +159,7 @@ define([ ascShortcut: ascShortcut, } }); + actionItem.shortcuts = actionItem.shortcuts.sort(this._sortComparator); } this._eventsTrigger(); @@ -472,6 +473,9 @@ define([ shortcuts[foundIndex].ascShortcut = copyAscShortcut; } } + if(this.actionsMap[actionType]) { + this.actionsMap[actionType].shortcuts = shortcuts.sort(this._sortComparator); + } } }, diff --git a/apps/common/main/lib/view/ShortcutsEditDialog.js b/apps/common/main/lib/view/ShortcutsEditDialog.js index f7a75be0f9..4ebe1f5732 100644 --- a/apps/common/main/lib/view/ShortcutsEditDialog.js +++ b/apps/common/main/lib/view/ShortcutsEditDialog.js @@ -313,10 +313,10 @@ define([ const keys = []; - if (e.ctrlKey) keys.push(me._shortcutsController.keyCodeToKeyName(Common.UI.Keys.CTRL)); - if (e.shiftKey) keys.push(me._shortcutsController.keyCodeToKeyName(Common.UI.Keys.SHIFT)); - if (e.altKey) keys.push(me._shortcutsController.keyCodeToKeyName(Common.UI.Keys.ALT)); if (e.metaKey && Common.Utils.isMac) keys.push(me._shortcutsController.keyCodeToKeyName(Common.UI.Keys.META)); + if (e.ctrlKey) keys.push(me._shortcutsController.keyCodeToKeyName(Common.UI.Keys.CTRL)); + if (e.altKey) keys.push(me._shortcutsController.keyCodeToKeyName(Common.UI.Keys.ALT)); + if (e.shiftKey) keys.push(me._shortcutsController.keyCodeToKeyName(Common.UI.Keys.SHIFT)); if (![Common.UI.Keys.CTRL, Common.UI.Keys.SHIFT, Common.UI.Keys.ALT, Common.UI.Keys.META].includes(e.keyCode)) { keys.push(me._shortcutsController.keyCodeToKeyName(e.keyCode));