@rwh/keystrokes
Advanced tools
Comparing version 1.1.0 to 1.1.1
var w = Object.defineProperty; | ||
var E = (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) => (E(i, typeof e != "symbol" ? e + "" : e, t), t); | ||
class v { | ||
var E = (n, e, t) => e in n ? w(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t; | ||
var o = (n, e, t) => (E(n, typeof e != "symbol" ? e + "" : e, t), t); | ||
class C { | ||
constructor(e) { | ||
@@ -20,4 +20,4 @@ o(this, "_onPressed"); | ||
executePressed(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); | ||
var t, i; | ||
this._isPressed || (t = this._onPressed) == null || t.call(this, e), this._isPressed = !0, (i = this._onPressedWithRepeat) == null || i.call(this, e); | ||
} | ||
@@ -30,3 +30,3 @@ executeReleased(e) { | ||
const c = class c { | ||
constructor(e, t, n = {}) { | ||
constructor(e, t, i = {}) { | ||
o(this, "_normalizedKeyCombo"); | ||
@@ -39,3 +39,3 @@ o(this, "_parsedKeyCombo"); | ||
o(this, "_keyComboEventMapper"); | ||
this._normalizedKeyCombo = c.normalizeKeyCombo(e), this._parsedKeyCombo = c.parseKeyCombo(e), this._handlerState = new v(n), this._keyComboEventMapper = t, this._lastActiveKeyPresses = [], this._isPressedWithFinalKey = null, this._sequenceIndex = 0; | ||
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; | ||
} | ||
@@ -46,7 +46,7 @@ static parseKeyCombo(e) { | ||
const t = e.toLowerCase(); | ||
let n = "", s = [], r = [s], a = [r]; | ||
const l = [a]; | ||
let i = "", s = [], a = [s], r = [a]; | ||
const l = [r]; | ||
let y = !1; | ||
for (let h = 0; h < e.length; h += 1) | ||
t[h] === "\\" ? y = !0 : (t[h] === "+" || t[h] === ">" || t[h] === ",") && !y ? n = t[h] : t[h].match(/[^\s]/) && (n && (n === "," ? (s = [], r = [s], a = [r], l.push(a)) : n === ">" ? (s = [], r = [s], a.push(r)) : n === "+" && (s = [], r.push(s)), n = ""), y = !1, s.push(t[h])); | ||
t[h] === "\\" ? y = !0 : (t[h] === "+" || t[h] === ">" || t[h] === ",") && !y ? i = t[h] : t[h].match(/[^\s]/) && (i && (i === "," ? (s = [], a = [s], r = [a], l.push(r)) : i === ">" ? (s = [], a = [s], r.push(a)) : i === "+" && (s = [], a.push(s)), i = ""), y = !1, s.push(t[h])); | ||
const K = l.map((h) => h.map((k) => k.map((g) => g.join("")))); | ||
@@ -56,3 +56,3 @@ return c._parseCache[e] = K, K; | ||
static stringifyKeyCombo(e) { | ||
return e.map((t) => t.map((n) => n.map((s) => s === "+" ? "\\+" : s === ">" ? "\\>" : s === "," ? "\\," : s).join("+")).join(">")).join(","); | ||
return e.map((t) => t.map((i) => i.map((s) => s === "+" ? "\\+" : s === ">" ? "\\>" : s === "," ? "\\," : s).join("+")).join(">")).join(","); | ||
} | ||
@@ -84,11 +84,11 @@ static normalizeKeyCombo(e) { | ||
const t = this._parsedKeyCombo[this._sequenceIndex]; | ||
let n = 0; | ||
let i = 0; | ||
for (const s of t) { | ||
let r = n; | ||
for (const a of s) { | ||
let a = i; | ||
for (const r of s) { | ||
let l = !1; | ||
for (let y = n; y < e.length; y += 1) { | ||
for (let y = i; y < e.length; y += 1) { | ||
const K = e[y]; | ||
if (a === K.key) { | ||
y > r && (r = y), l = !0; | ||
if (r === K.key) { | ||
y > a && (a = y), l = !0; | ||
break; | ||
@@ -102,13 +102,13 @@ } | ||
} | ||
n = r; | ||
i = a; | ||
} | ||
for (const s of e) { | ||
let r = !1; | ||
for (const a of t) | ||
for (const l of a) | ||
let a = !1; | ||
for (const r of t) | ||
for (const l of r) | ||
if (s.key === l) { | ||
r = !0; | ||
a = !0; | ||
break; | ||
} | ||
if (!r) { | ||
if (!a) { | ||
this._lastActiveKeyPresses.length = 0, this._sequenceIndex = 0; | ||
@@ -141,7 +141,7 @@ return; | ||
*/ | ||
addEventListener: (...i) => { | ||
addEventListener: (...n) => { | ||
}, | ||
removeEventListener: (...i) => { | ||
removeEventListener: (...n) => { | ||
}, | ||
dispatchEvent: (...i) => { | ||
dispatchEvent: (...n) => { | ||
} | ||
@@ -157,8 +157,8 @@ /* | ||
let m = ""; | ||
const B = (i) => { | ||
!f || i.key !== "Meta" || (m = i.code); | ||
}, I = (i) => !(!f || i.key !== "Meta"), C = () => { | ||
const B = (n) => { | ||
!f || n.key !== "Meta" || (m = n.code); | ||
}, I = (n) => !(!f || n.key !== "Meta"), P = () => { | ||
if (!f || !m) | ||
return; | ||
const i = new KeyboardEvent("keyup", { | ||
const n = new KeyboardEvent("keyup", { | ||
key: "Meta", | ||
@@ -169,6 +169,6 @@ code: m, | ||
}); | ||
m = "", u().dispatchEvent(i); | ||
}, W = (i) => { | ||
m = "", u().dispatchEvent(n); | ||
}, W = (n) => { | ||
try { | ||
const e = () => i(); | ||
const e = () => n(); | ||
return addEventListener("focus", e), () => { | ||
@@ -179,6 +179,6 @@ removeEventListener("focus", e); | ||
} | ||
}, L = (i) => { | ||
}, L = (n) => { | ||
try { | ||
const e = () => i(); | ||
return C(), addEventListener("blur", e), () => { | ||
const e = () => n(); | ||
return P(), addEventListener("blur", e), () => { | ||
removeEventListener("blur", e); | ||
@@ -188,10 +188,10 @@ }; | ||
} | ||
}, O = (i) => { | ||
}, O = (n) => { | ||
try { | ||
const e = (t) => { | ||
const n = t.composedPath(); | ||
return B(t), i({ | ||
const i = t.composedPath(); | ||
return B(t), n({ | ||
key: t.key, | ||
originalEvent: t, | ||
composedPath: () => n | ||
composedPath: () => i | ||
}); | ||
@@ -204,11 +204,11 @@ }; | ||
} | ||
}, q = (i) => { | ||
}, q = (n) => { | ||
try { | ||
const e = (t) => { | ||
const n = t.composedPath(); | ||
if (C(), !I(t)) | ||
return i({ | ||
const i = t.composedPath(); | ||
if (P(), !I(t)) | ||
return n({ | ||
key: t.key, | ||
originalEvent: t, | ||
composedPath: () => n | ||
composedPath: () => i | ||
}); | ||
@@ -221,4 +221,4 @@ }; | ||
} | ||
}, z = typeof requestAnimationFrame == "function" ? (i) => requestAnimationFrame(i) : (i) => setTimeout(i, 0), b = () => new Promise((i) => z(i)); | ||
class P { | ||
}, z = typeof requestAnimationFrame == "function" ? (n) => requestAnimationFrame(n) : (n) => setTimeout(n, 0), b = () => new Promise((n) => z(n)); | ||
class v { | ||
constructor(e = {}) { | ||
@@ -253,14 +253,14 @@ o(this, "_isActive"); | ||
e = e.toLowerCase(); | ||
const n = new v(t); | ||
(s = this._handlerStates)[e] ?? (s[e] = []), this._handlerStates[e].push(n); | ||
const i = new C(t); | ||
(s = this._handlerStates)[e] ?? (s[e] = []), this._handlerStates[e].push(i); | ||
} | ||
unbindKey(e, t) { | ||
e = e.toLowerCase(); | ||
const n = this._handlerStates[e]; | ||
if (n) | ||
const i = this._handlerStates[e]; | ||
if (i) | ||
if (t) | ||
for (let s = 0; s < n.length; s += 1) | ||
n[s].isOwnHandler(t) && (n.splice(s, 1), s -= 1); | ||
for (let s = 0; s < i.length; s += 1) | ||
i[s].isOwnHandler(t) && (i.splice(s, 1), s -= 1); | ||
else | ||
n.length = 0; | ||
i.length = 0; | ||
} | ||
@@ -270,18 +270,18 @@ bindKeyCombo(e, t) { | ||
e = d.normalizeKeyCombo(e); | ||
const n = new d(e, this._keyComboEventMapper, t); | ||
(s = this._keyComboStates)[e] ?? (s[e] = []), this._keyComboStates[e].push(n), this._keyComboStatesArray.push(n); | ||
const i = new d(e, this._keyComboEventMapper, t); | ||
(s = this._keyComboStates)[e] ?? (s[e] = []), this._keyComboStates[e].push(i), this._keyComboStatesArray.push(i); | ||
} | ||
unbindKeyCombo(e, t) { | ||
e = d.normalizeKeyCombo(e); | ||
const n = this._keyComboStates[e]; | ||
if (n) | ||
const i = this._keyComboStates[e]; | ||
if (i) | ||
if (t) { | ||
for (let s = 0; s < n.length; s += 1) | ||
if (n[s].isOwnHandler(t)) { | ||
for (let r = 0; r < this._keyComboStatesArray.length; r += 1) | ||
this._keyComboStatesArray[r] === n[s] && (this._keyComboStatesArray.splice(r, 1), r -= 1); | ||
n.splice(s, 1), s -= 1; | ||
for (let s = 0; s < i.length; s += 1) | ||
if (i[s].isOwnHandler(t)) { | ||
for (let a = 0; a < this._keyComboStatesArray.length; a += 1) | ||
this._keyComboStatesArray[a] === i[s] && (this._keyComboStatesArray.splice(a, 1), a -= 1); | ||
i.splice(s, 1), s -= 1; | ||
} | ||
} else | ||
n.length = 0; | ||
i.length = 0; | ||
} | ||
@@ -301,11 +301,11 @@ checkKey(e) { | ||
this._isActive = !0; | ||
}), n = this._onInactiveBinder(() => { | ||
}), i = this._onInactiveBinder(() => { | ||
this._isActive = !1; | ||
}), s = this._onKeyPressedBinder((a) => { | ||
this._handleKeyPress(a); | ||
}), r = this._onKeyReleasedBinder((a) => { | ||
this._handleKeyRelease(a); | ||
}), s = this._onKeyPressedBinder((r) => { | ||
this._handleKeyPress(r); | ||
}), a = this._onKeyReleasedBinder((r) => { | ||
this._handleKeyRelease(r); | ||
}); | ||
this._unbinder = () => { | ||
t == null || t(), n == null || n(), s == null || s(), r == null || r(); | ||
t == null || t(), i == null || i(), s == null || s(), a == null || a(); | ||
}; | ||
@@ -326,15 +326,15 @@ } | ||
return; | ||
let t = e.key.toLowerCase(); | ||
const n = this._keyRemap[t]; | ||
n && (t = n); | ||
const s = this._handlerStates[t]; | ||
if (s) | ||
for (const r of s) | ||
r.executePressed(e); | ||
this._activeKeySet.has(t) || (this._activeKeySet.add(t), this._activeKeyPresses.push({ | ||
key: t, | ||
e = { ...e, key: e.key.toLowerCase() }; | ||
const t = this._keyRemap[e.key]; | ||
t && (e.key = t); | ||
const i = this._handlerStates[e.key]; | ||
if (i) | ||
for (const s of i) | ||
s.executePressed(e); | ||
this._activeKeySet.has(e.key) || (this._activeKeySet.add(e.key), this._activeKeyPresses.push({ | ||
key: e.key, | ||
event: e | ||
})), await this._updateKeyComboStates(); | ||
for (const r of this._keyComboStatesArray) | ||
r.executePressed(e); | ||
for (const s of this._keyComboStatesArray) | ||
s.executePressed(e); | ||
})().catch((t) => { | ||
@@ -346,10 +346,13 @@ console.error(t); | ||
(async () => { | ||
const t = e.key.toLowerCase(), n = this._handlerStates[t]; | ||
if (n) | ||
for (const s of n) | ||
e = { ...e, key: e.key.toLowerCase() }; | ||
const t = this._keyRemap[e.key]; | ||
t && (e.key = t); | ||
const i = this._handlerStates[e.key]; | ||
if (i) | ||
for (const s of i) | ||
s.executeReleased(e); | ||
if (this._activeKeySet.has(t)) { | ||
this._activeKeySet.delete(t); | ||
if (this._activeKeySet.has(e.key)) { | ||
this._activeKeySet.delete(e.key); | ||
for (let s = 0; s < this._activeKeyPresses.length; s += 1) | ||
if (this._activeKeyPresses[s].key === t) { | ||
if (this._activeKeyPresses[s].key === e.key) { | ||
this._activeKeyPresses.splice(s, 1), s -= 1; | ||
@@ -381,21 +384,21 @@ break; | ||
let S, p; | ||
const M = (i) => { | ||
p = i ?? new P(S); | ||
}, _ = () => (p || M(), p), H = (i) => { | ||
S = i; | ||
}, U = (...i) => _().bindKey(...i), j = (...i) => _().unbindKey(...i), T = (...i) => _().bindKeyCombo(...i), D = (...i) => _().unbindKeyCombo(...i), G = (...i) => _().checkKey(...i), N = (...i) => _().checkKeyCombo(...i), J = d.normalizeKeyCombo, Q = d.stringifyKeyCombo, V = d.parseKeyCombo, X = (i = {}) => { | ||
let e, t, n, s; | ||
return Object.assign(new P({ | ||
...i, | ||
onActive(a) { | ||
e = a; | ||
const M = (n) => { | ||
p = n ?? new v(S); | ||
}, _ = () => (p || M(), p), H = (n) => { | ||
S = 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 v({ | ||
...n, | ||
onActive(r) { | ||
e = r; | ||
}, | ||
onInactive(a) { | ||
t = a; | ||
onInactive(r) { | ||
t = r; | ||
}, | ||
onKeyPressed(a) { | ||
n = a; | ||
onKeyPressed(r) { | ||
i = r; | ||
}, | ||
onKeyReleased(a) { | ||
s = a; | ||
onKeyReleased(r) { | ||
s = r; | ||
} | ||
@@ -409,7 +412,7 @@ }), { | ||
}, | ||
press(a) { | ||
n({ composedPath: () => [], ...a }); | ||
press(r) { | ||
i({ composedPath: () => [], ...r }); | ||
}, | ||
release(a) { | ||
s({ composedPath: () => [], ...a }); | ||
release(r) { | ||
s({ composedPath: () => [], ...r }); | ||
} | ||
@@ -419,3 +422,3 @@ }); | ||
export { | ||
P as Keystrokes, | ||
v as Keystrokes, | ||
U as bindKey, | ||
@@ -422,0 +425,0 @@ T as bindKeyCombo, |
{ | ||
"name": "@rwh/keystrokes", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"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.", | ||
@@ -58,4 +58,4 @@ "type": "module", | ||
"build": "rm -rf dist/ && cp ../../readme.md readme.md && vite build", | ||
"dev": "vite" | ||
"dev": "vite build -w" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
50704
609