All files / lib keyboard.svelte.js

11.11% Statements 1/9
0% Branches 0/4
0% Functions 0/3
11.11% Lines 1/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45          2x                                                                              
import { onDestroy, onMount } from 'svelte';
 
import { uiState } from './state.svelte.js';
import { entries, keys } from './utils.js';
 
const GROUPS = {
	general: 'Général',
	observations: 'Observations',
	navigation: 'Navigation',
	cropping: 'Recadrage',
	debugmode: 'Debug mode',
	classification: 'Classification'
};
 
/**
 * Keyboard shortcuts to define while the component is mounted. These keyboards will be unset when the component is unmounted.
 *
 * To be called during a component initialization phase (so, not inside a $effect / $derived etc, but in the <script> tag).
 *
 * This function will _not_ override any keybind that already has an existing key shortcut defined.
 * @param {keyof typeof GROUPS} group used to group keybinds together in help dialogs, applied to all keybinds defined here, unless they override it themselves
 * @param {import("./state.svelte").Keymap<keyof typeof GROUPS>} shortcuts
 * WARNING: If you rename this function, update `heuristic` in `wuchale.config.js`
 */
export function defineKeyboardShortcuts(group, shortcuts) {
	onMount(() => {
		for (const [key, definition] of entries(shortcuts)) {
			if (key in uiState.keybinds) {
				console.warn(`Keybind ${key} already defined, not overriding.`);
				continue;
			}
 
			uiState.keybinds[key] = {
				group: GROUPS[definition.debug ? 'debugmode' : group],
				...definition
			};
		}
	});
	onDestroy(() => {
		for (const key of keys(shortcuts)) {
			delete uiState.keybinds[key];
		}
	});
}