-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (45 loc) · 1.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var Mousetrap = require('mousetrap');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var Mousetrap__default = /*#__PURE__*/_interopDefaultLegacy(Mousetrap);
const isFunc = (v) => typeof v === 'function';
const isArr = Array.isArray;
const listen = (arr, mousetrap) => {
const result = [];
let v2, f1;
let i = 0;
arr.forEach((v1, k) => {
if (isArr(v1)) result.push(v1), ++i;
else {
(v2 = arr[k + 1]), (f1 = isFunc(v1));
if (!result[i]) result[i] = [[], []];
result[i][!f1 ? 0 : 1].push(v1);
if (v2 && f1 && !isFunc(v2)) ++i;
}
});
result.forEach((arr) => {
if (arr.length !== 2) listen(arr, mousetrap);
else {
const types = arr[0],
fns = isArr(arr[1]) ? arr[1] : [arr[1]];
mousetrap.bind(types, (...a) => fns.forEach((fn) => fn(...a)));
}
});
};
function use(
_node,
arr
)
{
const mousetrap = new Mousetrap__default['default'](window.document);
const destroy = () => {
mousetrap.reset();
};
const update = (arr) => {
destroy(), listen(arr, mousetrap);
};
update(arr);
return { update, destroy }
}
exports.default = use;