feat: make shortcuts easier to add across the app

This commit is contained in:
Zoe
2026-02-25 19:47:33 -06:00
parent 50cb195486
commit d22d08e99f
4 changed files with 57 additions and 21 deletions
+32 -13
View File
@@ -1,23 +1,41 @@
export const useKeyboardShortcuts = () => {
const { toggle: toggleSidebar } = useSidebar();
const { toggle: openSettings, open: isSettingsOpen } = useSettings();
const shortcutsMap = new Map<string, () => void>();
const normalize = (combo: string[]) =>
combo.map(k => k.toLowerCase()).sort().join('+');
const addShortcut = (combo: string[], callback: () => void) => {
const normalizedKey = normalize(combo);
shortcutsMap.set(normalizedKey, callback);
return () => {
shortcutsMap.delete(normalizedKey);
};
};
const handleKeyDown = (event: KeyboardEvent) => {
// Ctrl+[ to collapse sidebar
if (event.ctrlKey && event.key === '[') {
event.preventDefault();
toggleSidebar();
const isInput = ['INPUT', 'TEXTAREA'].includes((event.target as HTMLElement).tagName);
if (isInput && !event.ctrlKey && !event.metaKey && event.key !== 'Escape') {
return;
}
if (event.ctrlKey && event.key === ',') {
event.preventDefault();
if (isSettingsOpen.value) {
return;
}
const keys: string[] = [];
if (event.ctrlKey) keys.push('ctrl');
if (event.metaKey) keys.push('meta');
if (event.shiftKey) keys.push('shift');
if (event.altKey) keys.push('alt');
openSettings();
return;
const mainKey = event.key.toLowerCase();
if (!['control', 'shift', 'meta', 'alt'].includes(mainKey)) {
keys.push(mainKey);
}
const pressedCombo = normalize(keys);
if (shortcutsMap.has(pressedCombo)) {
event.preventDefault();
event.stopPropagation();
shortcutsMap.get(pressedCombo)!();
}
};
@@ -30,6 +48,7 @@ export const useKeyboardShortcuts = () => {
});
return {
addShortcut,
handleKeyDown,
};
};