From ac2265315eac140fb60fb8da5a6a96d312f2de93 Mon Sep 17 00:00:00 2001 From: mauro-balades Date: Tue, 8 Oct 2024 21:23:18 +0200 Subject: [PATCH] Refactor ZenKeyboardShortcuts to improve initialization and keyset handling --- src/ZenKeyboardShortcuts.mjs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/ZenKeyboardShortcuts.mjs b/src/ZenKeyboardShortcuts.mjs index 68cc95d..5d814a8 100644 --- a/src/ZenKeyboardShortcuts.mjs +++ b/src/ZenKeyboardShortcuts.mjs @@ -21,6 +21,7 @@ const KEYCODE_MAP = { ARROWDOWN: 'VK_DOWN', DELETE: 'VK_DELETE', BACKSPACE: 'VK_BACK', + HOME: 'VK_HOME', }; const defaultKeyboardGroups = { @@ -497,10 +498,12 @@ class KeyShortcut { for (let keycode of Object.keys(KEYCODE_MAP)) { if (keycode == shortcut.toUpperCase()) { this.#keycode = KEYCODE_MAP[keycode]; + this.#key = ''; return; } } + this.#keycode = ''; // Clear the keycode this.#key = shortcut; } } @@ -696,7 +699,7 @@ function zenGetDefaultShortcuts() { } class ZenKeyboardShortcutsVersioner { - static LATEST_KBS_VERSION = 1; + static LATEST_KBS_VERSION = 2; constructor() {} @@ -752,6 +755,17 @@ class ZenKeyboardShortcutsVersioner { // since nothing seems to work properly. data = zenGetDefaultShortcuts(); } + if (version < 2) { + // Migrate from 1 to 2 + // In this new version, we are resolving the conflicts between + // shortcuts having keycode and key at the same time. + // If there's both, we remove the keycodes. + for (let shortcut of data) { + if (shortcut.getKeyCode() && shortcut.getKeyName()) { + shortcut.setNewBinding(shortcut.getKeyName()); + } + } + } return data; } } @@ -837,12 +851,10 @@ var gZenKeyboardShortcutsManager = { if (!mainKeyset) { throw new Error('Main keyset not found'); } - - let parent = mainKeyset.parentElement; this.clearMainKeyset(mainKeyset); const keyset = this.getZenKeyset(browser); - this.clearMainKeyset(keyset); + keyset.innerHTML = ''; // We dont check this anymore since we are skiping internal keys //if (mainKeyset.children.length > 0) { @@ -857,7 +869,7 @@ var gZenKeyboardShortcutsManager = { keyset.appendChild(child); } - parent.prepend(keyset); + mainKeyset.after(keyset); console.debug('Shortcuts applied...'); } },