@neovici/cosmoz-utils 6.13.0 → 6.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -8,6 +8,7 @@ export type KeyBinding = readonly [Matcher, Activity[], Info];
|
|
|
8
8
|
export type ActivityHandler = {
|
|
9
9
|
activity: Activity;
|
|
10
10
|
callback: () => void;
|
|
11
|
+
check?: () => boolean;
|
|
11
12
|
element?: () => Element | null | undefined;
|
|
12
13
|
};
|
|
13
14
|
export type RegisterFn = (handler: ActivityHandler) => () => void;
|
|
@@ -4,7 +4,7 @@ import { focusIsInEditableArea, isInteractive, matches } from './utils';
|
|
|
4
4
|
export const useKeybindings = (bindings) => {
|
|
5
5
|
const meta = useMeta({ bindings });
|
|
6
6
|
useEffect(() => {
|
|
7
|
-
const
|
|
7
|
+
const keyboardEventHandler = (e) => {
|
|
8
8
|
if (e.defaultPrevented) {
|
|
9
9
|
return;
|
|
10
10
|
}
|
|
@@ -18,14 +18,20 @@ export const useKeybindings = (bindings) => {
|
|
|
18
18
|
if (handlers.length === 0)
|
|
19
19
|
return;
|
|
20
20
|
// find first actionable handler
|
|
21
|
-
const handler = handlers.find((handler) =>
|
|
21
|
+
const handler = handlers.find((handler) => {
|
|
22
|
+
if ((handler.check && !handler.check()) ||
|
|
23
|
+
(handler.element && !isInteractive(handler.element()))) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
return handler;
|
|
27
|
+
});
|
|
22
28
|
if (!handler)
|
|
23
29
|
return;
|
|
24
30
|
e.preventDefault();
|
|
25
31
|
handler.callback();
|
|
26
32
|
};
|
|
27
|
-
document.addEventListener('keydown',
|
|
28
|
-
return () => document.removeEventListener('keydown',
|
|
33
|
+
document.addEventListener('keydown', keyboardEventHandler, true);
|
|
34
|
+
return () => document.removeEventListener('keydown', keyboardEventHandler, true);
|
|
29
35
|
}, []);
|
|
30
36
|
const register = useCallback((handler) => {
|
|
31
37
|
meta[handler.activity] = [handler, ...(meta[handler.activity] ?? [])];
|
|
@@ -19,7 +19,7 @@ export const focusIsInEditableArea = () => {
|
|
|
19
19
|
const active = getActiveElement(document);
|
|
20
20
|
if (!active)
|
|
21
21
|
return false;
|
|
22
|
-
if (active.matches('input, textarea'))
|
|
22
|
+
if (active.matches('input:not([type="checkbox"]), textarea'))
|
|
23
23
|
return true;
|
|
24
24
|
if ('isContentEditable' in active && active.isContentEditable) {
|
|
25
25
|
return true;
|