fix: focus textarea when n key is pressed

This commit is contained in:
Shusui MOYATANI
2023-03-08 13:11:06 +09:00
parent f732203893
commit 3228f401ae
3 changed files with 24 additions and 16 deletions

View File

@@ -2,6 +2,7 @@
// type Commands = (typeof commands)[number];
import { onMount, onCleanup, type JSX } from 'solid-js';
import throttle from 'lodash/throttle';
import { useRequestCommand, type Command } from '@/hooks/useCommandBus';
@@ -42,7 +43,7 @@ const useShortcutKeys = ({ shortcuts = defaultShortcut, onShortcut }: UseShortcu
const shortcutsMap = createShortcutsMap(shortcuts);
onMount(() => {
const handleKeydown = (ev: KeyboardEvent) => {
const handleKeydown = throttle((ev: KeyboardEvent) => {
if (ev.type !== 'keydown') return;
if (ev.target instanceof HTMLTextAreaElement || ev.target instanceof HTMLInputElement) return;
@@ -51,7 +52,7 @@ const useShortcutKeys = ({ shortcuts = defaultShortcut, onShortcut }: UseShortcu
if (shortcut == null) return;
onShortcut(shortcut);
};
}, 100);
window.addEventListener('keydown', handleKeydown);
@@ -66,7 +67,10 @@ export const useMountShortcutKeys = () => {
useShortcutKeys({
onShortcut: (shortcut) => {
request(shortcut.command);
request(shortcut.command).then(
() => console.debug(`${shortcut.key} was processed successfully`),
(err) => console.error(err),
);
},
});
};