@neovici/cosmoz-utils 6.12.0 → 6.13.0

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.
@@ -1,2 +1,2 @@
1
- declare const useHostBounds: () => DOMRectReadOnly;
1
+ declare const useHostBounds: () => DOMRectReadOnly | undefined;
2
2
  export { useHostBounds };
@@ -0,0 +1,2 @@
1
+ import { RegisterFn } from './types';
2
+ export declare const Keybindings: import("@pionjs/pion/lib/create-context").Context<RegisterFn>;
@@ -0,0 +1,4 @@
1
+ import { createContext } from '@pionjs/pion';
2
+ import { noop } from '../function';
3
+ export const Keybindings = createContext(() => noop);
4
+ customElements.define('cosmoz-keybinding-provider', Keybindings.Provider);
@@ -0,0 +1,4 @@
1
+ export type * from './types';
2
+ export * from './context';
3
+ export * from './use-activity';
4
+ export * from './use-keybindings';
@@ -0,0 +1,3 @@
1
+ export * from './context';
2
+ export * from './use-activity';
3
+ export * from './use-keybindings';
@@ -0,0 +1,13 @@
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
+ element?: () => Element | null | undefined;
12
+ };
13
+ export type RegisterFn = (handler: ActivityHandler) => () => void;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import { ActivityHandler } from './types';
2
+ export declare const useActivity: (handler: ActivityHandler, deps: unknown[]) => void;
@@ -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,2 @@
1
+ import { KeyBinding, RegisterFn } from './types';
2
+ export declare const useKeybindings: (bindings: readonly KeyBinding[]) => RegisterFn;
@@ -0,0 +1,37 @@
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 handler = (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) => !handler.element || isInteractive(handler.element()));
22
+ if (!handler)
23
+ return;
24
+ e.preventDefault();
25
+ handler.callback();
26
+ };
27
+ document.addEventListener('keydown', handler, true);
28
+ return () => document.removeEventListener('keydown', handler, true);
29
+ }, []);
30
+ const register = useCallback((handler) => {
31
+ meta[handler.activity] = [handler, ...(meta[handler.activity] ?? [])];
32
+ return () => {
33
+ meta[handler.activity] = meta[handler.activity]?.filter((h) => h !== handler);
34
+ };
35
+ }, []);
36
+ return register;
37
+ };
@@ -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, textarea'))
23
+ return true;
24
+ if ('isContentEditable' in active && active.isContentEditable) {
25
+ return true;
26
+ }
27
+ return false;
28
+ };
package/dist/tagged.js CHANGED
@@ -1 +1 @@
1
- export const tagged = (strings, ...values) => strings.flatMap((s, i) => [s, values[i] || '']).join('');
1
+ export const tagged = (strings, ...values) => strings.flatMap((s, i) => [s, values[i] ?? '']).join('');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neovici/cosmoz-utils",
3
- "version": "6.12.0",
3
+ "version": "6.13.0",
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.0.0"
76
+ "@pionjs/pion": "^2.7.1"
75
77
  },
76
78
  "devDependencies": {
77
79
  "@commitlint/cli": "^18.0.0",