@neovici/cosmoz-utils 6.12.1 → 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.
- package/dist/hooks/use-host-bounds.d.ts +1 -1
- package/dist/keybindings/context.d.ts +2 -0
- package/dist/keybindings/context.js +4 -0
- package/dist/keybindings/index.d.ts +4 -0
- package/dist/keybindings/index.js +3 -0
- package/dist/keybindings/types.d.ts +14 -0
- package/dist/keybindings/types.js +1 -0
- package/dist/keybindings/use-activity.d.ts +2 -0
- package/dist/keybindings/use-activity.js +8 -0
- package/dist/keybindings/use-keybindings.d.ts +2 -0
- package/dist/keybindings/use-keybindings.js +43 -0
- package/dist/keybindings/utils.d.ts +9 -0
- package/dist/keybindings/utils.js +28 -0
- package/package.json +5 -3
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const useHostBounds: () => DOMRectReadOnly;
|
|
1
|
+
declare const useHostBounds: () => DOMRectReadOnly | undefined;
|
|
2
2
|
export { useHostBounds };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type Activity = symbol;
|
|
2
|
+
export type Matcher = Pick<KeyboardEvent, 'key'> & Partial<Pick<KeyboardEvent, 'ctrlKey' | 'metaKey' | 'altKey' | 'shiftKey'>>;
|
|
3
|
+
export type Info = {
|
|
4
|
+
title: string;
|
|
5
|
+
description: string;
|
|
6
|
+
};
|
|
7
|
+
export type KeyBinding = readonly [Matcher, Activity[], Info];
|
|
8
|
+
export type ActivityHandler = {
|
|
9
|
+
activity: Activity;
|
|
10
|
+
callback: () => void;
|
|
11
|
+
check?: () => boolean;
|
|
12
|
+
element?: () => Element | null | undefined;
|
|
13
|
+
};
|
|
14
|
+
export type RegisterFn = (handler: ActivityHandler) => () => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { useContext, useEffect } from '@pionjs/pion';
|
|
2
|
+
import { Keybindings } from './context';
|
|
3
|
+
import { useMeta } from '../hooks/use-meta';
|
|
4
|
+
export const useActivity = (handler, deps) => {
|
|
5
|
+
const register = useContext(Keybindings);
|
|
6
|
+
const meta = useMeta(handler);
|
|
7
|
+
useEffect(() => register(meta), deps);
|
|
8
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { useCallback, useEffect } from '@pionjs/pion';
|
|
2
|
+
import { useMeta } from '../hooks/use-meta';
|
|
3
|
+
import { focusIsInEditableArea, isInteractive, matches } from './utils';
|
|
4
|
+
export const useKeybindings = (bindings) => {
|
|
5
|
+
const meta = useMeta({ bindings });
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
const keyboardEventHandler = (e) => {
|
|
8
|
+
if (e.defaultPrevented) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
const binding = meta.bindings.find(matches(e));
|
|
12
|
+
if (!binding)
|
|
13
|
+
return;
|
|
14
|
+
if (focusIsInEditableArea())
|
|
15
|
+
return;
|
|
16
|
+
const [, activities] = binding;
|
|
17
|
+
const handlers = activities.flatMap((activity) => meta[activity]);
|
|
18
|
+
if (handlers.length === 0)
|
|
19
|
+
return;
|
|
20
|
+
// find first actionable 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
|
+
});
|
|
28
|
+
if (!handler)
|
|
29
|
+
return;
|
|
30
|
+
e.preventDefault();
|
|
31
|
+
handler.callback();
|
|
32
|
+
};
|
|
33
|
+
document.addEventListener('keydown', keyboardEventHandler, true);
|
|
34
|
+
return () => document.removeEventListener('keydown', keyboardEventHandler, true);
|
|
35
|
+
}, []);
|
|
36
|
+
const register = useCallback((handler) => {
|
|
37
|
+
meta[handler.activity] = [handler, ...(meta[handler.activity] ?? [])];
|
|
38
|
+
return () => {
|
|
39
|
+
meta[handler.activity] = meta[handler.activity]?.filter((h) => h !== handler);
|
|
40
|
+
};
|
|
41
|
+
}, []);
|
|
42
|
+
return register;
|
|
43
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { KeyBinding, Matcher } from './types';
|
|
2
|
+
declare global {
|
|
3
|
+
interface ObjectConstructor {
|
|
4
|
+
entries(o: Matcher): [keyof Matcher, Matcher[keyof Matcher]][];
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export declare const matches: (e: KeyboardEvent) => ([matcher]: KeyBinding) => boolean;
|
|
8
|
+
export declare const isInteractive: (el: Element | null | undefined) => boolean;
|
|
9
|
+
export declare const focusIsInEditableArea: () => boolean;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export const matches = (e) => ([matcher]) => Object.entries(matcher).every(([key, value]) => e[key] === value);
|
|
2
|
+
export const isInteractive = (el) => {
|
|
3
|
+
if (el == null)
|
|
4
|
+
return false;
|
|
5
|
+
const bounds = el.getBoundingClientRect(), root = el.getRootNode(), topEl = root.elementFromPoint(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
|
|
6
|
+
return el === topEl;
|
|
7
|
+
};
|
|
8
|
+
const getActiveElement = (root = document) => {
|
|
9
|
+
const activeEl = root.activeElement;
|
|
10
|
+
if (!activeEl) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
if (activeEl.shadowRoot) {
|
|
14
|
+
return getActiveElement(activeEl.shadowRoot);
|
|
15
|
+
}
|
|
16
|
+
return activeEl;
|
|
17
|
+
};
|
|
18
|
+
export const focusIsInEditableArea = () => {
|
|
19
|
+
const active = getActiveElement(document);
|
|
20
|
+
if (!active)
|
|
21
|
+
return false;
|
|
22
|
+
if (active.matches('input:not([type="checkbox"]), textarea'))
|
|
23
|
+
return true;
|
|
24
|
+
if ('isContentEditable' in active && active.isContentEditable) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@neovici/cosmoz-utils",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.13.1",
|
|
4
4
|
"description": "Date, money and template management functions commonly needed in Cosmoz views.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"polymer",
|
|
@@ -68,10 +68,12 @@
|
|
|
68
68
|
"./elements/*": "./dist/elements/*.js",
|
|
69
69
|
"./directives/*": "./dist/directives/*.js",
|
|
70
70
|
"./hooks/*": "./dist/hooks/*.js",
|
|
71
|
-
"./memoize": "./dist/memoize.js"
|
|
71
|
+
"./memoize": "./dist/memoize.js",
|
|
72
|
+
"./keybindings": "./dist/keybindings/index.js",
|
|
73
|
+
"./keybindings/*": "./dist/keybindings/*.js"
|
|
72
74
|
},
|
|
73
75
|
"dependencies": {
|
|
74
|
-
"@pionjs/pion": "^2.
|
|
76
|
+
"@pionjs/pion": "^2.7.1"
|
|
75
77
|
},
|
|
76
78
|
"devDependencies": {
|
|
77
79
|
"@commitlint/cli": "^18.0.0",
|