feat: add shortcut viewer

This commit is contained in:
2024-04-19 21:51:07 +02:00
parent f1fcf78f6c
commit e575974872
9 changed files with 232 additions and 67 deletions

View File

@ -0,0 +1,43 @@
import { get, writable } from "svelte/store";
type Shortcut = {
key: string | string[],
shift?: boolean,
ctrl?: boolean,
alt?: boolean,
description?: string,
callback: (event: KeyboardEvent) => void
}
export function createKeyMap(keys: Shortcut[]) {
const store = writable(keys);
return {
handleKeyboardEvent: (event: KeyboardEvent) => {
const key = get(store).find(k => {
if (Array.isArray(k.key) ? !k.key.includes(event.key) : k.key !== event.key) return false;
if ("shift" in k && k.shift !== event.shiftKey) return false;
if ("ctrl" in k && k.ctrl !== event.ctrlKey) return false;
if ("alt" in k && k.alt !== event.altKey) return false;
return true;
});
console.log({ keys: get(store), out: key, key: event.key });
key?.callback(event);
},
addShortcut: (shortcut: Shortcut) => {
if (Array.isArray(shortcut.key)) {
for (const k of shortcut.key) {
store.update(keys => {
if (keys.find(kk => kk.key === k)) return keys;
return [...keys, { ...shortcut, key: k }];
});
}
} else {
store.update(keys => [...keys, shortcut]);
}
},
keys: store
}
}