lägg till modul för kortkommandon

ctrl+0 öppnar "välj lista" globalt
This commit is contained in:
botvid johansson 2025-04-24 13:59:45 +02:00
parent 619c33c189
commit ac4450a27f
4 changed files with 84 additions and 1 deletions

10
package-lock.json generated
View File

@ -14,6 +14,7 @@
"@oslojs/encoding": "^1.1.0", "@oslojs/encoding": "^1.1.0",
"@tauri-apps/api": "^2.2.0", "@tauri-apps/api": "^2.2.0",
"dexie": "^4.0.11", "dexie": "^4.0.11",
"hotkeys-js": "^3.13.9",
"svelte-outside": "^0.0.3", "svelte-outside": "^0.0.3",
"svelte-radix": "^2.0.1" "svelte-radix": "^2.0.1"
}, },
@ -4872,6 +4873,15 @@
"node": ">= 0.4" "node": ">= 0.4"
} }
}, },
"node_modules/hotkeys-js": {
"version": "3.13.9",
"resolved": "https://registry.npmjs.org/hotkeys-js/-/hotkeys-js-3.13.9.tgz",
"integrity": "sha512-3TRCj9u9KUH6cKo25w4KIdBfdBfNRjfUwrljCLDC2XhmPDG0SjAZFcFZekpUZFmXzfYoGhFDcdx2gX/vUVtztQ==",
"license": "MIT",
"funding": {
"url": "https://jaywcjlove.github.io/#/sponsor"
}
},
"node_modules/ignore": { "node_modules/ignore": {
"version": "5.3.1", "version": "5.3.1",
"resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.1.tgz", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.1.tgz",

View File

@ -58,6 +58,7 @@
"@oslojs/encoding": "^1.1.0", "@oslojs/encoding": "^1.1.0",
"@tauri-apps/api": "^2.2.0", "@tauri-apps/api": "^2.2.0",
"dexie": "^4.0.11", "dexie": "^4.0.11",
"hotkeys-js": "^3.13.9",
"svelte-outside": "^0.0.3", "svelte-outside": "^0.0.3",
"svelte-radix": "^2.0.1" "svelte-radix": "^2.0.1"
} }

56
src/lib/hotkeys.js Normal file
View File

@ -0,0 +1,56 @@
import hotkeys from "hotkeys-js";
let activeBindings = new Set();
export function initHotkeys(options = { scope: "all", filterInputs: false }) {
hotkeys.setScope(options.scope);
hotkeys.filter = () => true;
if (options.filterInputs === true) {
hotkeys.filter = (e) => {
const target = e.target || e.srcElement;
const tagName = target.tagName;
return !(target.isContentEditable || (tagName === 'INPUT' && target.type !== 'checkbox' && target.type !== 'radio') || tagName == "TEXTAREA" || tagName == "SELECT");
}
}
}
export function registerHotkeys(keyMap, options = {}) {
Object.entries(keyMap).forEach(([key, handler]) => {
registerHotkey(key, handler, options)
});
}
export function registerHotkey(key, handler, options = {}) {
const scope = options.scope || 'all';
hotkeys(key, { scope, preventDefault: options.preventDefault !== false }, (event) => {
handler(event);
});
activeBindings.add(key);
}
export function unregisterHotkey(key) {
hotkeys.unbind(key);
activeBindings.delete(key);
}
export function registerAllHotkeys() {
activeBindings.forEach(key => {
hotkeys.unbind(key);
})
activeBindings.clear();
}
export function pauseHotkeys() {
hotkeys.pause();
}
export function resumeHotkeys() {
hotkeys.unpause();
}
export function setHotkeeyScope(scope) {
hotkeys.setScope(scope);
}

View File

@ -8,6 +8,7 @@
import Menu from '../components/menu.svelte'; import Menu from '../components/menu.svelte';
import { Button } from '$lib/components/ui/button'; import { Button } from '$lib/components/ui/button';
import { appState, shortforms } from '$lib/stores.svelte'; import { appState, shortforms } from '$lib/stores.svelte';
import { initHotkeys, registerAllHotkeys, registerHotkey, unregisterHotkey } from '$lib/hotkeys';
let loaded = false; let loaded = false;
$effect(() => { $effect(() => {
console.log(appState.open); console.log(appState.open);
@ -33,13 +34,28 @@
import { hotkeys } from '../modules/keyboard'; import { hotkeys } from '../modules/keyboard';
var showDashboard = $state(true); var showDashboard = $state(true);
import { Toaster } from '$lib/components/ui/sonner'; import { Toaster } from '$lib/components/ui/sonner';
import { onMount } from 'svelte'; import { onDestroy, onMount } from 'svelte';
import { preventDefault } from 'svelte/legacy';
const handleHotkeys = (e: KeyboardEvent) => { const handleHotkeys = (e: KeyboardEvent) => {
hotkeys.get(e.key)?.action(e); hotkeys.get(e.key)?.action(e);
}; };
let textarea: HTMLTextAreaElement | undefined = $state(); let textarea: HTMLTextAreaElement | undefined = $state();
onMount(() => { onMount(() => {
appState.open = 'dashboard'; appState.open = 'dashboard';
initHotkeys({ scope: 'main', filterInputs: false });
registerHotkey(
'ctrl+0',
(e) => {
console.log('ctrl+0');
e.preventDefault();
appState.open = 'selectLists';
},
{ scope: 'main', preventDefault: true }
);
});
onDestroy(() => {
registerAllHotkeys();
}); });
var debugState = $state(); var debugState = $state();
var debugShortforms = $state(); var debugShortforms = $state();