From ac4450a27f19b007f9f2877236e6ae9df70200a9 Mon Sep 17 00:00:00 2001 From: Botvid Johansson Date: Thu, 24 Apr 2025 13:59:45 +0200 Subject: [PATCH] =?UTF-8?q?l=C3=A4gg=20till=20modul=20f=C3=B6r=20kortkomma?= =?UTF-8?q?ndon?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ctrl+0 öppnar "välj lista" globalt --- package-lock.json | 10 ++++++++ package.json | 1 + src/lib/hotkeys.js | 56 +++++++++++++++++++++++++++++++++++++++++ src/routes/+page.svelte | 18 ++++++++++++- 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/lib/hotkeys.js diff --git a/package-lock.json b/package-lock.json index 9eb1001..fdcc0d4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "@oslojs/encoding": "^1.1.0", "@tauri-apps/api": "^2.2.0", "dexie": "^4.0.11", + "hotkeys-js": "^3.13.9", "svelte-outside": "^0.0.3", "svelte-radix": "^2.0.1" }, @@ -4872,6 +4873,15 @@ "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": { "version": "5.3.1", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.1.tgz", diff --git a/package.json b/package.json index 0d3cd7d..10556d7 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "@oslojs/encoding": "^1.1.0", "@tauri-apps/api": "^2.2.0", "dexie": "^4.0.11", + "hotkeys-js": "^3.13.9", "svelte-outside": "^0.0.3", "svelte-radix": "^2.0.1" } diff --git a/src/lib/hotkeys.js b/src/lib/hotkeys.js new file mode 100644 index 0000000..4034d49 --- /dev/null +++ b/src/lib/hotkeys.js @@ -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); +} diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index c64ed2b..0079da8 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -8,6 +8,7 @@ import Menu from '../components/menu.svelte'; import { Button } from '$lib/components/ui/button'; import { appState, shortforms } from '$lib/stores.svelte'; + import { initHotkeys, registerAllHotkeys, registerHotkey, unregisterHotkey } from '$lib/hotkeys'; let loaded = false; $effect(() => { console.log(appState.open); @@ -33,13 +34,28 @@ import { hotkeys } from '../modules/keyboard'; var showDashboard = $state(true); 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) => { hotkeys.get(e.key)?.action(e); }; let textarea: HTMLTextAreaElement | undefined = $state(); onMount(() => { 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 debugShortforms = $state();