chore: setup linting
This commit is contained in:
@@ -1,38 +1,39 @@
|
||||
import { derived, get, writable } from "svelte/store";
|
||||
import { derived, get, writable } from 'svelte/store';
|
||||
|
||||
type Shortcut = {
|
||||
key: string | string[],
|
||||
shift?: boolean,
|
||||
ctrl?: boolean,
|
||||
alt?: boolean,
|
||||
preventDefault?: boolean,
|
||||
description?: string,
|
||||
callback: (event: KeyboardEvent) => void
|
||||
export type ShortCut = {
|
||||
key: string | string[];
|
||||
shift?: boolean;
|
||||
ctrl?: boolean;
|
||||
alt?: boolean;
|
||||
preventDefault?: boolean;
|
||||
description?: string;
|
||||
callback: (event: KeyboardEvent) => void;
|
||||
};
|
||||
|
||||
function getShortcutId(shortcut: ShortCut) {
|
||||
return `${shortcut.key}${shortcut.shift ? '+shift' : ''}${shortcut.ctrl ? '+ctrl' : ''}${
|
||||
shortcut.alt ? '+alt' : ''
|
||||
}`;
|
||||
}
|
||||
|
||||
function getShortcutId(shortcut: Shortcut) {
|
||||
return `${shortcut.key}${shortcut.shift ? "+shift" : ""}${shortcut.ctrl ? "+ctrl" : ""}${shortcut.alt ? "+alt" : ""}`;
|
||||
}
|
||||
|
||||
export function createKeyMap(keys: Shortcut[]) {
|
||||
|
||||
export function createKeyMap(keys: ShortCut[]) {
|
||||
const store = writable(new Map(keys.map(k => [getShortcutId(k), k])));
|
||||
|
||||
return {
|
||||
handleKeyboardEvent: (event: KeyboardEvent) => {
|
||||
const activeElement = document.activeElement as HTMLElement;
|
||||
if (activeElement?.tagName === "INPUT" || activeElement?.tagName === "TEXTAREA") return;
|
||||
if (activeElement?.tagName === 'INPUT' || activeElement?.tagName === 'TEXTAREA') return;
|
||||
const key = [...get(store).values()].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;
|
||||
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;
|
||||
});
|
||||
if (key && key.preventDefault) event.preventDefault();
|
||||
key?.callback(event);
|
||||
},
|
||||
addShortcut: (shortcut: Shortcut) => {
|
||||
addShortcut: (shortcut: ShortCut) => {
|
||||
if (Array.isArray(shortcut.key)) {
|
||||
for (const k of shortcut.key) {
|
||||
store.update(shortcuts => {
|
||||
@@ -52,6 +53,5 @@ export function createKeyMap(keys: Shortcut[]) {
|
||||
}
|
||||
},
|
||||
keys: derived(store, $store => Array.from($store.values()))
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user