Refactor ZenKeyboardShortcuts to improve initialization and keyset handling

This commit is contained in:
mauro-balades 2024-10-08 21:23:18 +02:00
parent e9fb3b5c75
commit ac2265315e
No known key found for this signature in database
GPG key ID: CBD57A2AEDBDA1FB

View file

@ -21,6 +21,7 @@ const KEYCODE_MAP = {
ARROWDOWN: 'VK_DOWN', ARROWDOWN: 'VK_DOWN',
DELETE: 'VK_DELETE', DELETE: 'VK_DELETE',
BACKSPACE: 'VK_BACK', BACKSPACE: 'VK_BACK',
HOME: 'VK_HOME',
}; };
const defaultKeyboardGroups = { const defaultKeyboardGroups = {
@ -497,10 +498,12 @@ class KeyShortcut {
for (let keycode of Object.keys(KEYCODE_MAP)) { for (let keycode of Object.keys(KEYCODE_MAP)) {
if (keycode == shortcut.toUpperCase()) { if (keycode == shortcut.toUpperCase()) {
this.#keycode = KEYCODE_MAP[keycode]; this.#keycode = KEYCODE_MAP[keycode];
this.#key = '';
return; return;
} }
} }
this.#keycode = ''; // Clear the keycode
this.#key = shortcut; this.#key = shortcut;
} }
} }
@ -696,7 +699,7 @@ function zenGetDefaultShortcuts() {
} }
class ZenKeyboardShortcutsVersioner { class ZenKeyboardShortcutsVersioner {
static LATEST_KBS_VERSION = 1; static LATEST_KBS_VERSION = 2;
constructor() {} constructor() {}
@ -752,6 +755,17 @@ class ZenKeyboardShortcutsVersioner {
// since nothing seems to work properly. // since nothing seems to work properly.
data = zenGetDefaultShortcuts(); 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; return data;
} }
} }
@ -837,12 +851,10 @@ var gZenKeyboardShortcutsManager = {
if (!mainKeyset) { if (!mainKeyset) {
throw new Error('Main keyset not found'); throw new Error('Main keyset not found');
} }
let parent = mainKeyset.parentElement;
this.clearMainKeyset(mainKeyset); this.clearMainKeyset(mainKeyset);
const keyset = this.getZenKeyset(browser); const keyset = this.getZenKeyset(browser);
this.clearMainKeyset(keyset); keyset.innerHTML = '';
// We dont check this anymore since we are skiping internal keys // We dont check this anymore since we are skiping internal keys
//if (mainKeyset.children.length > 0) { //if (mainKeyset.children.length > 0) {
@ -857,7 +869,7 @@ var gZenKeyboardShortcutsManager = {
keyset.appendChild(child); keyset.appendChild(child);
} }
parent.prepend(keyset); mainKeyset.after(keyset);
console.debug('Shortcuts applied...'); console.debug('Shortcuts applied...');
} }
}, },