@rwh/keystrokes
Advanced tools
Comparing version 1.1.2 to 1.1.3
@@ -1,11 +0,11 @@ | ||
var R = Object.defineProperty; | ||
var w = (n, e, t) => e in n ? R(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t; | ||
var a = (n, e, t) => (w(n, typeof e != "symbol" ? e + "" : e, t), t); | ||
var w = Object.defineProperty; | ||
var R = (i, e, t) => e in i ? w(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t; | ||
var o = (i, e, t) => (R(i, typeof e != "symbol" ? e + "" : e, t), t); | ||
class C { | ||
constructor(e) { | ||
a(this, "_onPressed"); | ||
a(this, "_onPressedWithRepeat"); | ||
a(this, "_onReleased"); | ||
a(this, "_isPressed"); | ||
a(this, "_identity"); | ||
o(this, "_onPressed"); | ||
o(this, "_onPressedWithRepeat"); | ||
o(this, "_onReleased"); | ||
o(this, "_isPressed"); | ||
o(this, "_identity"); | ||
this._isPressed = !1, this._identity = e, typeof e == "function" ? this._onPressedWithRepeat = e : (this._onPressed = e.onPressed, this._onPressedWithRepeat = e.onPressedWithRepeat, this._onReleased = e.onReleased); | ||
@@ -20,4 +20,4 @@ } | ||
executePressed(e) { | ||
var t, i; | ||
this._isPressed || (t = this._onPressed) == null || t.call(this, e), this._isPressed = !0, (i = this._onPressedWithRepeat) == null || i.call(this, e); | ||
var t, n; | ||
this._isPressed || (t = this._onPressed) == null || t.call(this, e), this._isPressed = !0, (n = this._onPressedWithRepeat) == null || n.call(this, e); | ||
} | ||
@@ -30,11 +30,11 @@ executeReleased(e) { | ||
const c = class c { | ||
constructor(e, t, i = {}) { | ||
a(this, "_normalizedKeyCombo"); | ||
a(this, "_parsedKeyCombo"); | ||
a(this, "_handlerState"); | ||
a(this, "_lastActiveKeyPresses"); | ||
a(this, "_isPressedWithFinalKey"); | ||
a(this, "_sequenceIndex"); | ||
a(this, "_keyComboEventMapper"); | ||
this._normalizedKeyCombo = c.normalizeKeyCombo(e), this._parsedKeyCombo = c.parseKeyCombo(e), this._handlerState = new C(i), this._keyComboEventMapper = t, this._lastActiveKeyPresses = [], this._isPressedWithFinalKey = null, this._sequenceIndex = 0; | ||
constructor(e, t, n = {}) { | ||
o(this, "_normalizedKeyCombo"); | ||
o(this, "_parsedKeyCombo"); | ||
o(this, "_handlerState"); | ||
o(this, "_lastActiveKeyPresses"); | ||
o(this, "_isPressedWithFinalKey"); | ||
o(this, "_sequenceIndex"); | ||
o(this, "_keyComboEventMapper"); | ||
this._normalizedKeyCombo = c.normalizeKeyCombo(e), this._parsedKeyCombo = c.parseKeyCombo(e), this._handlerState = new C(n), this._keyComboEventMapper = t, this._lastActiveKeyPresses = [], this._isPressedWithFinalKey = null, this._sequenceIndex = 0; | ||
} | ||
@@ -45,12 +45,12 @@ static parseKeyCombo(e) { | ||
const t = e.toLowerCase(); | ||
let i = "", s = [], o = [s], r = [o]; | ||
let n = "", s = [], a = [s], r = [a]; | ||
const l = [r]; | ||
let y = !1; | ||
let d = !1; | ||
for (let h = 0; h < e.length; h += 1) | ||
t[h] === "\\" ? y = !0 : (t[h] === "+" || t[h] === ">" || t[h] === ",") && !y ? i = t[h] : t[h].match(/[^\s]/) && (i && (i === "," ? (s = [], o = [s], r = [o], l.push(r)) : i === ">" ? (s = [], o = [s], r.push(o)) : i === "+" && (s = [], o.push(s)), i = ""), y = !1, s.push(t[h])); | ||
const m = l.map((h) => h.map((S) => S.map((g) => g.join("")))); | ||
return c._parseCache[e] = m, m; | ||
t[h] === "\\" ? d = !0 : (t[h] === "+" || t[h] === ">" || t[h] === ",") && !d ? n = t[h] : t[h].match(/[^\s]/) && (n && (n === "," ? (s = [], a = [s], r = [a], l.push(r)) : n === ">" ? (s = [], a = [s], r.push(a)) : n === "+" && (s = [], a.push(s)), n = ""), d = !1, s.push(t[h])); | ||
const K = l.map((h) => h.map((g) => g.map((E) => E.join("")))); | ||
return c._parseCache[e] = K, K; | ||
} | ||
static stringifyKeyCombo(e) { | ||
return e.map((t) => t.map((i) => i.map((s) => s === "+" ? "\\+" : s === ">" ? "\\>" : s === "," ? "\\," : s).join("+")).join(">")).join(","); | ||
return e.map((t) => t.map((n) => n.map((s) => s === "+" ? "\\+" : s === ">" ? "\\>" : s === "," ? "\\," : s).join("+")).join(">")).join(","); | ||
} | ||
@@ -82,11 +82,11 @@ static normalizeKeyCombo(e) { | ||
const t = this._parsedKeyCombo[this._sequenceIndex]; | ||
let i = 0; | ||
let n = 0; | ||
for (const s of t) { | ||
let o = i; | ||
let a = n; | ||
for (const r of s) { | ||
let l = !1; | ||
for (let y = i; y < e.length; y += 1) { | ||
const m = e[y]; | ||
if (r === m.key) { | ||
y > o && (o = y), l = !0; | ||
for (let d = n; d < e.length; d += 1) { | ||
const K = e[d]; | ||
if (r === K.key) { | ||
d > a && (a = d), l = !0; | ||
break; | ||
@@ -100,13 +100,13 @@ } | ||
} | ||
i = o; | ||
n = a; | ||
} | ||
for (const s of e) { | ||
let o = !1; | ||
let a = !1; | ||
for (const r of t) | ||
for (const l of r) | ||
if (s.key === l) { | ||
o = !0; | ||
a = !0; | ||
break; | ||
} | ||
if (!o) { | ||
if (!a) { | ||
this._lastActiveKeyPresses.length = 0, this._sequenceIndex = 0; | ||
@@ -131,5 +131,5 @@ return; | ||
}; | ||
a(c, "_parseCache", {}), a(c, "_normalizationCache", {}); | ||
let d = c; | ||
const E = { | ||
o(c, "_parseCache", {}), o(c, "_normalizationCache", {}); | ||
let y = c; | ||
const A = { | ||
/* | ||
@@ -140,7 +140,7 @@ eslint-disable | ||
*/ | ||
addEventListener: (...n) => { | ||
addEventListener: (...i) => { | ||
}, | ||
removeEventListener: (...n) => { | ||
removeEventListener: (...i) => { | ||
}, | ||
dispatchEvent: (...n) => { | ||
dispatchEvent: (...i) => { | ||
} | ||
@@ -152,21 +152,36 @@ /* | ||
*/ | ||
}, A = { | ||
}, x = { | ||
userAgent: "" | ||
}, u = () => typeof document < "u" ? document : E, x = () => typeof navigator < "u" ? navigator : A, f = x().userAgent.toLocaleLowerCase().includes("mac"); | ||
let K = ""; | ||
const B = (n) => { | ||
!f || n.key !== "Meta" || (K = n.code); | ||
}, I = (n) => !(!f || n.key !== "Meta"), P = () => { | ||
if (!f || !K) | ||
}, _ = () => typeof document < "u" ? document : A, B = () => typeof navigator < "u" ? navigator : x, b = () => B().userAgent.toLocaleLowerCase().includes("mac"); | ||
let m = ""; | ||
const I = (i) => { | ||
!b() || i.key !== "Meta" || (m = i.code); | ||
}, W = (i) => !(!b() || i.key !== "Meta"), k = () => { | ||
if (!b() || !m) | ||
return; | ||
const n = new KeyboardEvent("keyup", { | ||
const i = new KeyboardEvent("keyup", { | ||
key: "Meta", | ||
code: K, | ||
code: m, | ||
bubbles: !0, | ||
cancelable: !0 | ||
}); | ||
K = "", u().dispatchEvent(n); | ||
}, W = (n) => { | ||
m = "", _().dispatchEvent(i); | ||
}, p = /* @__PURE__ */ new Map(), L = (i) => { | ||
p.set(i.key, i); | ||
}, M = (i) => { | ||
p.delete(i.key); | ||
}, O = () => { | ||
for (const i of p.values()) { | ||
const e = new KeyboardEvent("keyup", { | ||
key: i.key, | ||
code: i.code, | ||
bubbles: !0, | ||
cancelable: !0 | ||
}); | ||
_().dispatchEvent(e); | ||
} | ||
p.clear(); | ||
}, q = (i) => { | ||
try { | ||
const e = () => n(); | ||
const e = () => i(); | ||
return addEventListener("focus", e), () => { | ||
@@ -177,60 +192,56 @@ removeEventListener("focus", e); | ||
} | ||
}, L = (n) => { | ||
}, z = (i) => { | ||
try { | ||
const e = () => n(); | ||
return P(), addEventListener("blur", e), () => { | ||
removeEventListener("blur", e); | ||
const e = () => { | ||
k(), O(), i(); | ||
}; | ||
return addEventListener("blur", e), () => removeEventListener("blur", e); | ||
} catch { | ||
} | ||
}, O = (n) => { | ||
}, F = (i) => { | ||
try { | ||
const e = (t) => { | ||
const i = t.composedPath(); | ||
return B(t), n({ | ||
const n = t.composedPath(); | ||
return L(t), I(t), i({ | ||
key: t.key, | ||
originalEvent: t, | ||
composedPath: () => i | ||
composedPath: () => n | ||
}); | ||
}; | ||
return u().addEventListener("keydown", e), () => { | ||
u().removeEventListener("keydown", e); | ||
}; | ||
return _().addEventListener("keydown", e), () => _().removeEventListener("keydown", e); | ||
} catch { | ||
} | ||
}, q = (n) => { | ||
}, H = (i) => { | ||
try { | ||
const e = (t) => { | ||
const i = t.composedPath(); | ||
if (P(), !I(t)) | ||
return n({ | ||
const n = t.composedPath(); | ||
if (M(t), k(), !W(t)) | ||
return i({ | ||
key: t.key, | ||
originalEvent: t, | ||
composedPath: () => i | ||
composedPath: () => n | ||
}); | ||
}; | ||
return u().addEventListener("keyup", e), () => { | ||
u().removeEventListener("keyup", e); | ||
}; | ||
return _().addEventListener("keyup", e), () => _().removeEventListener("keyup", e); | ||
} catch { | ||
} | ||
}, z = typeof requestAnimationFrame == "function" ? (n) => requestAnimationFrame(n) : (n) => setTimeout(n, 0), b = () => new Promise((n) => z(n)); | ||
class k { | ||
}, U = typeof requestAnimationFrame == "function" ? (i) => requestAnimationFrame(i) : (i) => setTimeout(i, 0), v = () => new Promise((i) => U(i)); | ||
class P { | ||
constructor(e = {}) { | ||
a(this, "_isActive"); | ||
a(this, "_isUpdatingKeyComboState"); | ||
a(this, "_unbinder"); | ||
a(this, "_onActiveBinder"); | ||
a(this, "_onInactiveBinder"); | ||
a(this, "_onKeyPressedBinder"); | ||
a(this, "_onKeyReleasedBinder"); | ||
a(this, "_keyComboEventMapper"); | ||
a(this, "_selfReleasingKeys"); | ||
a(this, "_keyRemap"); | ||
a(this, "_handlerStates"); | ||
a(this, "_keyComboStates"); | ||
a(this, "_keyComboStatesArray"); | ||
a(this, "_activeKeyPresses"); | ||
a(this, "_activeKeySet"); | ||
a(this, "_watchedKeyComboStates"); | ||
o(this, "_isActive"); | ||
o(this, "_isUpdatingKeyComboState"); | ||
o(this, "_unbinder"); | ||
o(this, "_onActiveBinder"); | ||
o(this, "_onInactiveBinder"); | ||
o(this, "_onKeyPressedBinder"); | ||
o(this, "_onKeyReleasedBinder"); | ||
o(this, "_keyComboEventMapper"); | ||
o(this, "_selfReleasingKeys"); | ||
o(this, "_keyRemap"); | ||
o(this, "_handlerStates"); | ||
o(this, "_keyComboStates"); | ||
o(this, "_keyComboStatesArray"); | ||
o(this, "_activeKeyPresses"); | ||
o(this, "_activeKeySet"); | ||
o(this, "_watchedKeyComboStates"); | ||
this._isActive = !0, this._isUpdatingKeyComboState = !1, this._onActiveBinder = () => { | ||
@@ -248,34 +259,34 @@ }, this._onInactiveBinder = () => { | ||
e = e.toLowerCase(); | ||
const i = new C(t); | ||
(s = this._handlerStates)[e] ?? (s[e] = []), this._handlerStates[e].push(i); | ||
const n = new C(t); | ||
(s = this._handlerStates)[e] ?? (s[e] = []), this._handlerStates[e].push(n); | ||
} | ||
unbindKey(e, t) { | ||
e = e.toLowerCase(); | ||
const i = this._handlerStates[e]; | ||
if (i) | ||
const n = this._handlerStates[e]; | ||
if (n) | ||
if (t) | ||
for (let s = 0; s < i.length; s += 1) | ||
i[s].isOwnHandler(t) && (i.splice(s, 1), s -= 1); | ||
for (let s = 0; s < n.length; s += 1) | ||
n[s].isOwnHandler(t) && (n.splice(s, 1), s -= 1); | ||
else | ||
i.length = 0; | ||
n.length = 0; | ||
} | ||
bindKeyCombo(e, t) { | ||
var s; | ||
e = d.normalizeKeyCombo(e); | ||
const i = new d(e, this._keyComboEventMapper, t); | ||
(s = this._keyComboStates)[e] ?? (s[e] = []), this._keyComboStates[e].push(i), this._keyComboStatesArray.push(i); | ||
e = y.normalizeKeyCombo(e); | ||
const n = new y(e, this._keyComboEventMapper, t); | ||
(s = this._keyComboStates)[e] ?? (s[e] = []), this._keyComboStates[e].push(n), this._keyComboStatesArray.push(n); | ||
} | ||
unbindKeyCombo(e, t) { | ||
e = d.normalizeKeyCombo(e); | ||
const i = this._keyComboStates[e]; | ||
if (i) | ||
e = y.normalizeKeyCombo(e); | ||
const n = this._keyComboStates[e]; | ||
if (n) | ||
if (t) { | ||
for (let s = 0; s < i.length; s += 1) | ||
if (i[s].isOwnHandler(t)) { | ||
for (let o = 0; o < this._keyComboStatesArray.length; o += 1) | ||
this._keyComboStatesArray[o] === i[s] && (this._keyComboStatesArray.splice(o, 1), o -= 1); | ||
i.splice(s, 1), s -= 1; | ||
for (let s = 0; s < n.length; s += 1) | ||
if (n[s].isOwnHandler(t)) { | ||
for (let a = 0; a < this._keyComboStatesArray.length; a += 1) | ||
this._keyComboStatesArray[a] === n[s] && (this._keyComboStatesArray.splice(a, 1), a -= 1); | ||
n.splice(s, 1), s -= 1; | ||
} | ||
} else | ||
i.length = 0; | ||
n.length = 0; | ||
} | ||
@@ -292,14 +303,14 @@ checkKey(e) { | ||
bindEnvironment(e = {}) { | ||
this.unbindEnvironment(), this._onActiveBinder = e.onActive ?? W, this._onInactiveBinder = e.onInactive ?? L, this._onKeyPressedBinder = e.onKeyPressed ?? O, this._onKeyReleasedBinder = e.onKeyReleased ?? q, this._keyComboEventMapper = e.mapKeyComboEvent ?? (() => ({})), this._selfReleasingKeys = e.selfReleasingKeys ?? [], this._keyRemap = e.keyRemap ?? {}; | ||
this.unbindEnvironment(), this._onActiveBinder = e.onActive ?? q, this._onInactiveBinder = e.onInactive ?? z, this._onKeyPressedBinder = e.onKeyPressed ?? F, this._onKeyReleasedBinder = e.onKeyReleased ?? H, this._keyComboEventMapper = e.mapKeyComboEvent ?? (() => ({})), this._selfReleasingKeys = e.selfReleasingKeys ?? [], this._keyRemap = e.keyRemap ?? {}; | ||
const t = this._onActiveBinder(() => { | ||
this._isActive = !0; | ||
}), i = this._onInactiveBinder(() => { | ||
}), n = this._onInactiveBinder(() => { | ||
this._isActive = !1; | ||
}), s = this._onKeyPressedBinder((r) => { | ||
this._handleKeyPress(r); | ||
}), o = this._onKeyReleasedBinder((r) => { | ||
}), a = this._onKeyReleasedBinder((r) => { | ||
this._handleKeyRelease(r); | ||
}); | ||
this._unbinder = () => { | ||
t == null || t(), i == null || i(), s == null || s(), o == null || o(); | ||
t == null || t(), n == null || n(), s == null || s(), a == null || a(); | ||
}; | ||
@@ -312,3 +323,3 @@ } | ||
_ensureCachedKeyComboState(e) { | ||
e = d.normalizeKeyCombo(e), this._watchedKeyComboStates[e] || (this._watchedKeyComboStates[e] = new d(e, this._keyComboEventMapper)); | ||
e = y.normalizeKeyCombo(e), this._watchedKeyComboStates[e] || (this._watchedKeyComboStates[e] = new y(e, this._keyComboEventMapper)); | ||
const t = this._watchedKeyComboStates[e]; | ||
@@ -324,5 +335,5 @@ return t.updateState(this._activeKeyPresses), t; | ||
t && (e.key = t); | ||
const i = this._handlerStates[e.key]; | ||
if (i) | ||
for (const s of i) | ||
const n = this._handlerStates[e.key]; | ||
if (n) | ||
for (const s of n) | ||
s.executePressed(e); | ||
@@ -344,5 +355,5 @@ this._activeKeySet.has(e.key) || (this._activeKeySet.add(e.key), this._activeKeyPresses.push({ | ||
t && (e.key = t); | ||
const i = this._handlerStates[e.key]; | ||
if (i) | ||
for (const s of i) | ||
const n = this._handlerStates[e.key]; | ||
if (n) | ||
for (const s of n) | ||
s.executeReleased(e); | ||
@@ -366,4 +377,4 @@ if (this._activeKeySet.has(e.key)) { | ||
if (this._isUpdatingKeyComboState) | ||
return await b(); | ||
this._isUpdatingKeyComboState = !0, await b(); | ||
return await v(); | ||
this._isUpdatingKeyComboState = !0, await v(); | ||
for (const e of this._keyComboStatesArray) | ||
@@ -379,11 +390,11 @@ e.updateState(this._activeKeyPresses); | ||
} | ||
let v, p; | ||
const M = (n) => { | ||
p = n ?? new k(v); | ||
}, _ = () => (p || M(), p), H = (n) => { | ||
v = n; | ||
}, U = (...n) => _().bindKey(...n), j = (...n) => _().unbindKey(...n), T = (...n) => _().bindKeyCombo(...n), D = (...n) => _().unbindKeyCombo(...n), G = (...n) => _().checkKey(...n), N = (...n) => _().checkKeyCombo(...n), J = d.normalizeKeyCombo, Q = d.stringifyKeyCombo, V = d.parseKeyCombo, X = (n = {}) => { | ||
let e, t, i, s; | ||
return Object.assign(new k({ | ||
...n, | ||
let S, f; | ||
const j = (i) => { | ||
f = i ?? new P(S); | ||
}, u = () => (f || j(), f), D = (i) => { | ||
S = i; | ||
}, G = (...i) => u().bindKey(...i), N = (...i) => u().unbindKey(...i), J = (...i) => u().bindKeyCombo(...i), Q = (...i) => u().unbindKeyCombo(...i), V = (...i) => u().checkKey(...i), X = (...i) => u().checkKeyCombo(...i), Y = y.normalizeKeyCombo, Z = y.stringifyKeyCombo, $ = y.parseKeyCombo, ee = (i = {}) => { | ||
let e, t, n, s; | ||
return Object.assign(new P({ | ||
...i, | ||
onActive(r) { | ||
@@ -396,3 +407,3 @@ e = r; | ||
onKeyPressed(r) { | ||
i = r; | ||
n = r; | ||
}, | ||
@@ -410,3 +421,3 @@ onKeyReleased(r) { | ||
press(r) { | ||
i({ composedPath: () => [], ...r }); | ||
n({ composedPath: () => [], ...r }); | ||
}, | ||
@@ -419,16 +430,16 @@ release(r) { | ||
export { | ||
k as Keystrokes, | ||
U as bindKey, | ||
T as bindKeyCombo, | ||
G as checkKey, | ||
N as checkKeyCombo, | ||
X as createTestKeystrokes, | ||
_ as getGlobalKeystrokes, | ||
J as normalizeKeyCombo, | ||
V as parseKeyCombo, | ||
M as setGlobalKeystrokes, | ||
H as setGlobalKeystrokesOptions, | ||
Q as stringifyKeyCombo, | ||
j as unbindKey, | ||
D as unbindKeyCombo | ||
P as Keystrokes, | ||
G as bindKey, | ||
J as bindKeyCombo, | ||
V as checkKey, | ||
X as checkKeyCombo, | ||
ee as createTestKeystrokes, | ||
u as getGlobalKeystrokes, | ||
Y as normalizeKeyCombo, | ||
$ as parseKeyCombo, | ||
j as setGlobalKeystrokes, | ||
D as setGlobalKeystrokesOptions, | ||
Z as stringifyKeyCombo, | ||
N as unbindKey, | ||
Q as unbindKeyCombo | ||
}; |
{ | ||
"name": "@rwh/keystrokes", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "Keystrokes is an easy to use library for binding functions to keys and key combos. It can be used with any TypeScript or JavaScript project, even in non-browser environments.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
51263
621