Compare commits

...

1 Commits

Author SHA1 Message Date
aks07
8dc4c0ce76 fix(hotkeys): ignore keydown events whose target is not an element 2026-10-15 23:59:50 +05:30
2 changed files with 19 additions and 2 deletions

View File

@@ -73,4 +73,21 @@ describe('KeyboardHotkeysProvider', () => {
expect(handleShortcut).not.toHaveBeenCalled();
});
it('ignores keydown events dispatched on document itself', () => {
const handleShortcut = jest.fn();
render(
<KeyboardHotkeysProvider>
<TestComponentWithRegister handleShortcut={handleShortcut} />
</KeyboardHotkeysProvider>,
);
expect(() =>
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'a' })),
).not.toThrow();
document.dispatchEvent(new KeyboardEvent('keyup', { key: 'a' }));
expect(handleShortcut).not.toHaveBeenCalled();
});
});

View File

@@ -79,11 +79,11 @@ export function KeyboardHotkeysProvider({
const wasExtended = useRef(false);
const handleKeyDown = (event: KeyboardEvent): void => {
if (event.repeat) {
const { target } = event;
if (event.repeat || !(target instanceof Element)) {
return;
}
const target = event.target as HTMLElement;
const isCodeMirrorEditor = target.closest('.cm-editor') !== null;
const isMonacoEditor = target.closest('.monaco-editor') !== null;
if (