@nemigo/helpers 0.8.0 → 0.8.2
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/html.d.ts +9 -0
- package/dist/html.js +11 -0
- package/dist/index.d.ts +6 -1
- package/dist/index.js +16 -0
- package/package.json +2 -2
package/dist/html.d.ts
CHANGED
|
@@ -29,6 +29,15 @@ export declare const enterKeyHook: <T extends KeyboardEvent = KeyboardEvent>(cal
|
|
|
29
29
|
usePreventStop?: "enter" | unknown | ((e: T, isEnter: boolean) => unknown);
|
|
30
30
|
condition?: ((e: T) => unknown) | unknown;
|
|
31
31
|
}) => (e: T) => void;
|
|
32
|
+
export type TargetEvent<E extends Event, N = HTMLDivElement> = E & {
|
|
33
|
+
currentTarget: EventTarget & N;
|
|
34
|
+
};
|
|
35
|
+
export type TargetKeyboardEvent<N = HTMLDivElement> = TargetEvent<KeyboardEvent, N>;
|
|
36
|
+
export declare const isKeyboardEvent: (e: any) => e is KeyboardEvent;
|
|
37
|
+
export declare const ctxMenuHook: (call: (position: {
|
|
38
|
+
left: number;
|
|
39
|
+
top: number;
|
|
40
|
+
}) => void) => (e: MouseEvent | TargetKeyboardEvent<HTMLDivElement>) => void;
|
|
32
41
|
/**
|
|
33
42
|
* Создаёт обработчик событий для указанного элемента или окна
|
|
34
43
|
*
|
package/dist/html.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { useConditionGuard } from "./index.js";
|
|
2
|
+
import { clamp, toRound } from "./phymath/index.js";
|
|
2
3
|
/**
|
|
3
4
|
* Проверка по глобальному `window`
|
|
4
5
|
*/
|
|
@@ -41,6 +42,16 @@ export const enterKeyHook = (call, { usePreventStop = "enter", condition = true,
|
|
|
41
42
|
if (isEnter && useConditionGuard(condition))
|
|
42
43
|
call?.(e);
|
|
43
44
|
};
|
|
45
|
+
export const isKeyboardEvent = (e) => !!e.code;
|
|
46
|
+
export const ctxMenuHook = (call) => preventStopHook((e) => {
|
|
47
|
+
if (isKeyboardEvent(e)) {
|
|
48
|
+
const { x, y, width } = e.currentTarget.getBoundingClientRect();
|
|
49
|
+
call({ left: toRound(x + clamp(5, width / 10, 25), 3), top: toRound(y) });
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
call({ left: toRound(e.clientX), top: toRound(e.clientY) });
|
|
53
|
+
}
|
|
54
|
+
});
|
|
44
55
|
//...
|
|
45
56
|
/**
|
|
46
57
|
* Создаёт обработчик событий для указанного элемента или окна
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CanBeArray, CanBeReadonly } from "./types.js";
|
|
1
|
+
import type { CanBeArray, CanBePromise, CanBeReadonly } from "./types.js";
|
|
2
2
|
/**
|
|
3
3
|
* Добавляет ведущие нули к числу для достижения указанной длины
|
|
4
4
|
*
|
|
@@ -52,3 +52,8 @@ export declare const useConditionGuard: (condition: unknown, ...args: any[]) =>
|
|
|
52
52
|
* Сортирует ключи объекта в алфавитном порядке и возвращает новый объект с отсортированными ключами
|
|
53
53
|
*/
|
|
54
54
|
export declare function sortObjectByKeys<T>(obj: T): T;
|
|
55
|
+
export type BoundaryFallback<E> = E | ((err: unknown) => CanBePromise<E>);
|
|
56
|
+
/**
|
|
57
|
+
* Приведение коллбэка к промису с отловом ошибок и fallback-ом
|
|
58
|
+
*/
|
|
59
|
+
export declare const boundary: <R = undefined, E = R>(call: () => CanBePromise<R>, fallback?: BoundaryFallback<E>) => Promise<R | E>;
|
package/dist/index.js
CHANGED
|
@@ -67,3 +67,19 @@ export function sortObjectByKeys(obj) {
|
|
|
67
67
|
result[key] = obj[key];
|
|
68
68
|
return result;
|
|
69
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Приведение коллбэка к промису с отловом ошибок и fallback-ом
|
|
72
|
+
*/
|
|
73
|
+
export const boundary = async (call, fallback) => {
|
|
74
|
+
try {
|
|
75
|
+
return await call();
|
|
76
|
+
}
|
|
77
|
+
catch (e) {
|
|
78
|
+
console.error("boundary", e);
|
|
79
|
+
if (typeof fallback === "function") {
|
|
80
|
+
// @ts-expect-error <тут не типизировать>
|
|
81
|
+
return fallback(e);
|
|
82
|
+
}
|
|
83
|
+
return fallback;
|
|
84
|
+
}
|
|
85
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nemigo/helpers",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Vlad Logvin",
|
|
@@ -137,7 +137,7 @@
|
|
|
137
137
|
}
|
|
138
138
|
},
|
|
139
139
|
"peerDependencies": {
|
|
140
|
-
"zod": "
|
|
140
|
+
"zod": ">=4.0.0"
|
|
141
141
|
},
|
|
142
142
|
"peerDependenciesMeta": {
|
|
143
143
|
"zod": {
|