lägg till modul för kortkommandon
ctrl+0 öppnar "välj lista" globalt
This commit is contained in:
parent
619c33c189
commit
ac4450a27f
10
package-lock.json
generated
10
package-lock.json
generated
@ -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",
|
||||
|
@ -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"
|
||||
}
|
||||
|
56
src/lib/hotkeys.js
Normal file
56
src/lib/hotkeys.js
Normal 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);
|
||||
}
|
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user