react-better-hotkeys 0.2.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.
- package/LICENSE +9 -0
- package/README.md +74 -0
- package/dist/HotkeyRegistry.d.ts +28 -0
- package/dist/HotkeyRegistry.d.ts.map +1 -0
- package/dist/HotkeyRegistry.js +132 -0
- package/dist/HotkeyTextResolver.d.ts +12 -0
- package/dist/HotkeyTextResolver.d.ts.map +1 -0
- package/dist/HotkeyTextResolver.js +45 -0
- package/dist/context/HotkeyContext.d.ts +8 -0
- package/dist/context/HotkeyContext.d.ts.map +1 -0
- package/dist/context/HotkeyContext.js +7 -0
- package/dist/context/HotkeyProvider.d.ts +4 -0
- package/dist/context/HotkeyProvider.d.ts.map +1 -0
- package/dist/context/HotkeyProvider.js +31 -0
- package/dist/definitions/Chord.d.ts +6 -0
- package/dist/definitions/Chord.d.ts.map +1 -0
- package/dist/definitions/Chord.js +32 -0
- package/dist/definitions/Hotkey.d.ts +7 -0
- package/dist/definitions/Hotkey.d.ts.map +1 -0
- package/dist/definitions/Hotkey.js +13 -0
- package/dist/definitions/KeyMap.d.ts +5 -0
- package/dist/definitions/KeyMap.d.ts.map +1 -0
- package/dist/definitions/KeyMap.js +583 -0
- package/dist/definitions/ModifierKeyCodes.d.ts +2 -0
- package/dist/definitions/ModifierKeyCodes.d.ts.map +1 -0
- package/dist/definitions/ModifierKeyCodes.js +10 -0
- package/dist/definitions/Sequence.d.ts +4 -0
- package/dist/definitions/Sequence.d.ts.map +1 -0
- package/dist/definitions/Sequence.js +15 -0
- package/dist/hooks/useHotkey.d.ts +18 -0
- package/dist/hooks/useHotkey.d.ts.map +1 -0
- package/dist/hooks/useHotkey.js +66 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/types/hotkey/HotKeyChordDef.d.ts +4 -0
- package/dist/types/hotkey/HotKeyChordDef.d.ts.map +1 -0
- package/dist/types/hotkey/HotKeyDef.d.ts +4 -0
- package/dist/types/hotkey/HotKeyDef.d.ts.map +1 -0
- package/dist/types/hotkey/HotKeyDefBase.d.ts +11 -0
- package/dist/types/hotkey/HotKeyDefBase.d.ts.map +1 -0
- package/dist/types/hotkey/HotKeyDefChordBase.d.ts +9 -0
- package/dist/types/hotkey/HotKeyDefChordBase.d.ts.map +1 -0
- package/dist/types/hotkey/HotKeyDefSequenceBase.d.ts +6 -0
- package/dist/types/hotkey/HotKeyDefSequenceBase.d.ts.map +1 -0
- package/dist/types/hotkey/HotKeySequenceDef.d.ts +4 -0
- package/dist/types/hotkey/HotKeySequenceDef.d.ts.map +1 -0
- package/dist/types/hotkey/HotkeyCallback.d.ts +2 -0
- package/dist/types/hotkey/HotkeyCallback.d.ts.map +1 -0
- package/dist/types/hotkey/HotkeyOptions.d.ts +21 -0
- package/dist/types/hotkey/HotkeyOptions.d.ts.map +1 -0
- package/dist/types/hotkey/HotkeyTextParts.d.ts +2 -0
- package/dist/types/hotkey/HotkeyTextParts.d.ts.map +1 -0
- package/dist/types/hotkey/HotkeyType.d.ts +2 -0
- package/dist/types/hotkey/HotkeyType.d.ts.map +1 -0
- package/dist/types/hotkey/UseHotkeyFn.d.ts +3 -0
- package/dist/types/hotkey/UseHotkeyFn.d.ts.map +1 -0
- package/dist/types/hotkey/chord/ChordNode.d.ts +7 -0
- package/dist/types/hotkey/chord/ChordNode.d.ts.map +1 -0
- package/dist/types/hotkey/chord/HotkeyChordBuilder.d.ts +6 -0
- package/dist/types/hotkey/chord/HotkeyChordBuilder.d.ts.map +1 -0
- package/dist/types/hotkey/chord/IncompleteChordHotkey.d.ts +6 -0
- package/dist/types/hotkey/chord/IncompleteChordHotkey.d.ts.map +1 -0
- package/dist/types/hotkey/provider/HotkeyProviderProps.d.ts +16 -0
- package/dist/types/hotkey/provider/HotkeyProviderProps.d.ts.map +1 -0
- package/dist/types/hotkey/sequence/HotkeySequenceBuilder.d.ts +4 -0
- package/dist/types/hotkey/sequence/HotkeySequenceBuilder.d.ts.map +1 -0
- package/dist/types/hotkey/sequence/SequenceNode.d.ts +7 -0
- package/dist/types/hotkey/sequence/SequenceNode.d.ts.map +1 -0
- package/dist/types/hotkey/sequence/SequenceTree.d.ts +11 -0
- package/dist/types/hotkey/sequence/SequenceTree.d.ts.map +1 -0
- package/dist/types/key/KeyDescription.d.ts +6 -0
- package/dist/types/key/KeyDescription.d.ts.map +1 -0
- package/dist/types/key/KeyValueType.d.ts +2 -0
- package/dist/types/key/KeyValueType.d.ts.map +1 -0
- package/dist/types/key/ModifierKeyCode.d.ts +3 -0
- package/dist/types/key/ModifierKeyCode.d.ts.map +1 -0
- package/dist/types/key/PrimaryKey.d.ts +4 -0
- package/dist/types/key/PrimaryKey.d.ts.map +1 -0
- package/dist/types/key/PrimaryKeyCode.d.ts +2 -0
- package/dist/types/key/PrimaryKeyCode.d.ts.map +1 -0
- package/dist/types/key/PrimaryKeyValue.d.ts +2 -0
- package/dist/types/key/PrimaryKeyValue.d.ts.map +1 -0
- package/dist/util/checkModifierPressed.d.ts +3 -0
- package/dist/util/checkModifierPressed.d.ts.map +1 -0
- package/dist/util/checkModifierPressed.js +6 -0
- package/dist/util/detectOS.d.ts +3 -0
- package/dist/util/detectOS.d.ts.map +1 -0
- package/dist/util/detectOS.js +11 -0
- package/dist/util/isCleanKeydown.d.ts +2 -0
- package/dist/util/isCleanKeydown.d.ts.map +1 -0
- package/dist/util/isCleanKeydown.js +6 -0
- package/dist/util/isEditable.d.ts +2 -0
- package/dist/util/isEditable.d.ts.map +1 -0
- package/dist/util/isEditable.js +30 -0
- package/dist/util/isModifierKeyboardEvent.d.ts +2 -0
- package/dist/util/isModifierKeyboardEvent.d.ts.map +1 -0
- package/dist/util/isModifierKeyboardEvent.js +6 -0
- package/dist/util/isPrimaryKeyCode.d.ts +4 -0
- package/dist/util/isPrimaryKeyCode.d.ts.map +1 -0
- package/dist/util/isPrimaryKeyCode.js +6 -0
- package/dist/util/wrongModifierPressed.d.ts +3 -0
- package/dist/util/wrongModifierPressed.d.ts.map +1 -0
- package/dist/util/wrongModifierPressed.js +6 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Christopher Haindl
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
React Better Hotkeyss
|
|
2
|
+
======================
|
|
3
|
+
|
|
4
|
+
A tiny, typed helper for registering global hotkeys in React. It supports both **chords** (e.g. `Shift + Alt + A`) and **key sequences** (e.g. `g` then `h`), respects platform-specific modifier keys, and cleans up registrations automatically.
|
|
5
|
+
|
|
6
|
+
Docs: https://selokhq.github.io/react-better-hotkeys/
|
|
7
|
+
|
|
8
|
+
Features
|
|
9
|
+
--------
|
|
10
|
+
- Chainable builders for chords and sequences with full TypeScript coverage.
|
|
11
|
+
- `Mod` alias mapping to `⌘` on macOS and `Ctrl` on Windows/Linux.
|
|
12
|
+
- Distinguish physical key `code` vs character `key` so shortcuts stay stable across layouts.
|
|
13
|
+
- Efficient global listener optimizing event handling
|
|
14
|
+
|
|
15
|
+
Installation
|
|
16
|
+
------------
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
npm install react-better-hotkeys
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Quick start
|
|
23
|
+
-----------
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
import { Hotkey, HotkeyProvider, useHotkey } from "react-better-hotkeys";
|
|
27
|
+
|
|
28
|
+
function Shortcuts() {
|
|
29
|
+
// Layout-stable chord: Shift + OS-aware Mod + physical "K" key
|
|
30
|
+
useHotkey(Hotkey.Chord.Shift.Mod.K, () => {
|
|
31
|
+
console.log("Shift+⌘+K / Shift+Ctrl+K pressed")
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Sequence: press "g" then "h"
|
|
35
|
+
useHotkey(Hotkey.Sequence.KeyG.KeyH.end, () => {
|
|
36
|
+
console.log("[G]o [H]ome")
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export default function App() {
|
|
43
|
+
return (
|
|
44
|
+
<HotkeyProvider>
|
|
45
|
+
<Shortcuts />
|
|
46
|
+
</HotkeyProvider>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Issues
|
|
52
|
+
|
|
53
|
+
Looking to contribute? Look for the [Good First Issue][good-first-issue] label.
|
|
54
|
+
|
|
55
|
+
### 🐛 Bugs
|
|
56
|
+
|
|
57
|
+
Please file an issue for bugs, missing documentation, or unexpected behavior.
|
|
58
|
+
|
|
59
|
+
[**See Bugs**][bugs]
|
|
60
|
+
|
|
61
|
+
### 💡 Feature Requests
|
|
62
|
+
|
|
63
|
+
Please file an issue to suggest new features. Vote on feature requests by adding
|
|
64
|
+
a 👍. This helps to prioritize what to work on.
|
|
65
|
+
|
|
66
|
+
[**See Feature Requests**][requests]
|
|
67
|
+
|
|
68
|
+
## License
|
|
69
|
+
|
|
70
|
+
[MIT](LICENSE)
|
|
71
|
+
|
|
72
|
+
[requests]: https://github.com/selokhq/react-better-hotkeys/issues?q=is%3Aissue%20state%3Aopen%20sort%3Areactions-%2B1-desc%20label%3Aenhancement
|
|
73
|
+
[bugs]: https://github.com/selokhq/react-better-hotkeys/issues?q=is%3Aissue%20is%3Aopen%20sort%3Acreated-desc%20label%3Abug
|
|
74
|
+
[good-first-issue]: https://github.com/selokhq/react-better-hotkeys/issues?q=is%3Aissue%20is%3Aopen%20sort%3Areactions-%2B1-desc%20label%3Aenhancement%20label%3A%22good%20first%20issue%22
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { HotkeyTextResolver } from './HotkeyTextResolver';
|
|
2
|
+
import { IncompleteHotkey } from './types/hotkey/chord/IncompleteChordHotkey';
|
|
3
|
+
import { HotKeyChordDef } from './types/hotkey/HotKeyChordDef';
|
|
4
|
+
import { HotKeySequenceDef } from './types/hotkey/HotKeySequenceDef';
|
|
5
|
+
import { SequenceTree } from './types/hotkey/sequence/SequenceTree';
|
|
6
|
+
export declare class HotkeyRegistry {
|
|
7
|
+
sequenceTimeout: number;
|
|
8
|
+
chordTimeout: number;
|
|
9
|
+
chords: Record<string, HotKeyChordDef>;
|
|
10
|
+
chordMap: Partial<Record<string, HotKeyChordDef[]>>;
|
|
11
|
+
sequence: Record<string, HotKeySequenceDef>;
|
|
12
|
+
sequenceTree: SequenceTree | undefined;
|
|
13
|
+
incompleteChords: Record<string, IncompleteHotkey>;
|
|
14
|
+
waitingSequenceNodes: Record<number, SequenceTree>;
|
|
15
|
+
textResolver?: HotkeyTextResolver;
|
|
16
|
+
constructor(sequenceTimeout?: number, chordTimeout?: number, textResolver?: HotkeyTextResolver);
|
|
17
|
+
addChordHotkey(hotkey: HotKeyChordDef): void;
|
|
18
|
+
addSequenceHotkey(hotkey: HotKeySequenceDef): void;
|
|
19
|
+
removeChordHotkey(id: string): void;
|
|
20
|
+
removeSequenceHotkey(id: string): void;
|
|
21
|
+
private resolveSequenceTreeNode;
|
|
22
|
+
clearIncompleteChords(): void;
|
|
23
|
+
handleModifierKeydown(event: KeyboardEvent): void;
|
|
24
|
+
handleChordHotkeyPressSuccess(chord: HotKeyChordDef, event: KeyboardEvent): void;
|
|
25
|
+
handleKeydown(event: KeyboardEvent): void;
|
|
26
|
+
handleKeyup(event: KeyboardEvent): void;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=HotkeyRegistry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HotkeyRegistry.d.ts","sourceRoot":"","sources":["../src/lib/HotkeyRegistry.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC/D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4CAA4C,CAAC;AACnF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AACpE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AAC1E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sCAAsC,CAAC;AASzE,qBAAa,cAAc;IACzB,eAAe,EAAE,MAAM,CAAC;IACxB,YAAY,EAAE,MAAM,CAAC;IAGrB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAM;IAE5C,QAAQ,EAAE,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,cAAc,EAAE,CAAC,CAAC,CAAM;IAEzD,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAM;IACjD,YAAY,EAAE,YAAY,GAAG,SAAS,CAAC;IAGvC,gBAAgB,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAM;IAExD,oBAAoB,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAM;IAExD,YAAY,CAAC,EAAE,kBAAkB,CAAC;gBAGhC,eAAe,CAAC,EAAE,MAAM,EACxB,YAAY,CAAC,EAAE,MAAM,EACrB,YAAY,CAAC,EAAE,kBAAkB;IAO5B,cAAc,CAAC,MAAM,EAAE,cAAc;IAOrC,iBAAiB,CAAC,MAAM,EAAE,iBAAiB;IAwB3C,iBAAiB,CAAC,EAAE,EAAE,MAAM;IAQ5B,oBAAoB,CAAC,EAAE,EAAE,MAAM;IAoCtC,OAAO,CAAC,uBAAuB;IA4C/B,qBAAqB;IAOrB,qBAAqB,CAAC,KAAK,EAAE,aAAa;IAW1C,6BAA6B,CAAC,KAAK,EAAE,cAAc,EAAE,KAAK,EAAE,aAAa;IAclE,aAAa,CAAC,KAAK,EAAE,aAAa;IA2ClC,WAAW,CAAC,KAAK,EAAE,aAAa;CAGxC"}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { KeyMap as h } from "./definitions/KeyMap.js";
|
|
2
|
+
import { checkModifierPressed as l } from "./util/checkModifierPressed.js";
|
|
3
|
+
import { isClearKeydown as f } from "./util/isCleanKeydown.js";
|
|
4
|
+
import { isEditingKeystrokeContext as p } from "./util/isEditable.js";
|
|
5
|
+
import { isModifierKeyboardEvent as a } from "./util/isModifierKeyboardEvent.js";
|
|
6
|
+
import { wrongModifierPressed as k } from "./util/wrongModifierPressed.js";
|
|
7
|
+
class B {
|
|
8
|
+
sequenceTimeout;
|
|
9
|
+
chordTimeout;
|
|
10
|
+
// hotkeyId -> HotKeyChordDef
|
|
11
|
+
chords = {};
|
|
12
|
+
// Event key/code -> HotKeyChordDef[]
|
|
13
|
+
chordMap = {};
|
|
14
|
+
sequence = {};
|
|
15
|
+
sequenceTree;
|
|
16
|
+
// hotkeyId -> IncompleteHotkey
|
|
17
|
+
incompleteChords = {};
|
|
18
|
+
waitingSequenceNodes = {};
|
|
19
|
+
textResolver;
|
|
20
|
+
constructor(e, s, t) {
|
|
21
|
+
this.sequenceTimeout = e ?? 400, this.chordTimeout = s ?? 100, this.textResolver = t;
|
|
22
|
+
}
|
|
23
|
+
addChordHotkey(e) {
|
|
24
|
+
this.chords[e.id] = e;
|
|
25
|
+
const s = this.chordMap[e.primaryValue];
|
|
26
|
+
s == null ? this.chordMap[e.primaryValue] = [e] : s.push(e);
|
|
27
|
+
}
|
|
28
|
+
addSequenceHotkey(e) {
|
|
29
|
+
this.sequence[e.id] = e, this.sequenceTree == null && (this.sequenceTree = { hotkey: [], keyBased: {}, codeBased: {} });
|
|
30
|
+
let s = this.sequenceTree;
|
|
31
|
+
for (let t = 0; t < e.keys.length; t++) {
|
|
32
|
+
const o = h[e.keys[t]], i = o.on === "key" ? s.keyBased : s.codeBased, c = o.value;
|
|
33
|
+
i[c] == null && (i[c] = {
|
|
34
|
+
hotkey: [],
|
|
35
|
+
keyBased: {},
|
|
36
|
+
codeBased: {}
|
|
37
|
+
}), t === e.keys.length - 1 && i[c].hotkey.push(e), s = i[c];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
removeChordHotkey(e) {
|
|
41
|
+
const s = this.chords[e], t = this.chordMap[s.primaryValue];
|
|
42
|
+
t && (this.chordMap[s.primaryValue] = t.filter((o) => o.id != e)), Reflect.deleteProperty(this.chords, e);
|
|
43
|
+
}
|
|
44
|
+
removeSequenceHotkey(e) {
|
|
45
|
+
const s = this.sequence[e], t = (o, i) => {
|
|
46
|
+
if (i.length == 0) {
|
|
47
|
+
o.hotkey = o.hotkey.filter((y) => y.id !== s.id);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const [c, ...u] = i, d = c.value, n = c.on === "key" ? o.keyBased : o.codeBased, r = n[d];
|
|
51
|
+
r != null && (t(r, u), r.hotkey.length === 0 && Object.keys(r.codeBased).length == 0 && Object.keys(r.keyBased).length == 0 && Reflect.deleteProperty(n, d));
|
|
52
|
+
};
|
|
53
|
+
this.sequenceTree != null && t(
|
|
54
|
+
this.sequenceTree,
|
|
55
|
+
s.keys.map((o) => h[o])
|
|
56
|
+
), Reflect.deleteProperty(this.chords, e);
|
|
57
|
+
}
|
|
58
|
+
resolveSequenceTreeNode(e, s) {
|
|
59
|
+
const t = e.codeBased[s.code], o = e.keyBased[s.key];
|
|
60
|
+
if (t != null)
|
|
61
|
+
if (t.hotkey.length > 0 && Object.keys(t.codeBased).length === 0 && Object.keys(t.keyBased).length === 0)
|
|
62
|
+
t.hotkey.forEach((i) => {
|
|
63
|
+
i.options.disabled || i.callback(s);
|
|
64
|
+
}), this.waitingSequenceNodes = {};
|
|
65
|
+
else {
|
|
66
|
+
const i = setTimeout(() => {
|
|
67
|
+
Reflect.deleteProperty(this.waitingSequenceNodes, i);
|
|
68
|
+
}, this.sequenceTimeout);
|
|
69
|
+
this.waitingSequenceNodes[i] = t;
|
|
70
|
+
}
|
|
71
|
+
if (o != null)
|
|
72
|
+
if (o.hotkey.length > 0 && Object.keys(o.codeBased).length === 0 && Object.keys(o.keyBased).length === 0)
|
|
73
|
+
o.hotkey.forEach((i) => {
|
|
74
|
+
i.options.disabled || i.callback(s);
|
|
75
|
+
}), this.waitingSequenceNodes = {};
|
|
76
|
+
else {
|
|
77
|
+
const i = setTimeout(() => {
|
|
78
|
+
Reflect.deleteProperty(this.waitingSequenceNodes, i);
|
|
79
|
+
}, this.sequenceTimeout);
|
|
80
|
+
this.waitingSequenceNodes[i] = o;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
clearIncompleteChords() {
|
|
84
|
+
for (const [e, s] of Object.entries(this.incompleteChords))
|
|
85
|
+
clearTimeout(s.timeoutId), Reflect.deleteProperty(this.incompleteChords, e);
|
|
86
|
+
}
|
|
87
|
+
handleModifierKeydown(e) {
|
|
88
|
+
for (const s of Object.values(this.incompleteChords))
|
|
89
|
+
l(s.hotkey, e) ? (this.handleChordHotkeyPressSuccess(s.hotkey, e), this.clearIncompleteChords()) : k(s.hotkey, e) && this.clearIncompleteChords();
|
|
90
|
+
}
|
|
91
|
+
handleChordHotkeyPressSuccess(e, s) {
|
|
92
|
+
p(s) && !e.options.enableOnContentEditable || (e.options.preventDefault && s.preventDefault(), e.callback(s), this.waitingSequenceNodes = {});
|
|
93
|
+
}
|
|
94
|
+
handleKeydown(e) {
|
|
95
|
+
if (a(e)) {
|
|
96
|
+
this.handleModifierKeydown(e);
|
|
97
|
+
return;
|
|
98
|
+
} else f(e) && this.textResolver?.setSymbol(e.code, e.key);
|
|
99
|
+
this.clearIncompleteChords();
|
|
100
|
+
const s = [
|
|
101
|
+
...this.chordMap[e.key.toLowerCase()] ?? [],
|
|
102
|
+
...this.chordMap[e.code] ?? []
|
|
103
|
+
];
|
|
104
|
+
for (const t of s)
|
|
105
|
+
if (!t.options.disabled)
|
|
106
|
+
if (l(t, e)) {
|
|
107
|
+
this.handleChordHotkeyPressSuccess(t, e);
|
|
108
|
+
return;
|
|
109
|
+
} else
|
|
110
|
+
this.incompleteChords[t.id] = {
|
|
111
|
+
hotkey: t,
|
|
112
|
+
timeoutId: setTimeout(() => {
|
|
113
|
+
Reflect.deleteProperty(this.incompleteChords, t.id);
|
|
114
|
+
}, this.chordTimeout)
|
|
115
|
+
};
|
|
116
|
+
if (this.sequenceTree)
|
|
117
|
+
if (Object.keys(this.waitingSequenceNodes).length === 0)
|
|
118
|
+
this.resolveSequenceTreeNode(this.sequenceTree, e);
|
|
119
|
+
else {
|
|
120
|
+
const t = { ...this.waitingSequenceNodes };
|
|
121
|
+
this.waitingSequenceNodes = {}, Object.values(t).forEach(
|
|
122
|
+
(o) => this.resolveSequenceTreeNode(o, e)
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
handleKeyup(e) {
|
|
127
|
+
a(e) && this.clearIncompleteChords();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
export {
|
|
131
|
+
B as HotkeyRegistry
|
|
132
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { PrimaryKeyCode } from './types/key/PrimaryKeyCode';
|
|
2
|
+
export declare class HotkeyTextResolver {
|
|
3
|
+
symbolMap: Partial<Record<string, string>>;
|
|
4
|
+
private nextSubscriberId;
|
|
5
|
+
private subscribers;
|
|
6
|
+
constructor(symbolMap?: Partial<Record<PrimaryKeyCode, string>>);
|
|
7
|
+
setSymbol(code: string, symbol: string): void;
|
|
8
|
+
resolve(code: PrimaryKeyCode): string;
|
|
9
|
+
subscribe(code: PrimaryKeyCode, callback: (newSymbol: string) => void): () => void;
|
|
10
|
+
unsubscribe(code: PrimaryKeyCode, subscriberId: number): void;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=HotkeyTextResolver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HotkeyTextResolver.d.ts","sourceRoot":"","sources":["../src/lib/HotkeyTextResolver.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAOjE,qBAAa,kBAAkB;IAC7B,SAAS,EAAE,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAM;IAEhD,OAAO,CAAC,gBAAgB,CAAK;IAE7B,OAAO,CAAC,WAAW,CAEZ;gBAEK,SAAS,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,cAAc,EAAE,MAAM,CAAC,CAAC;IAiBxD,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IActC,OAAO,CAAC,IAAI,EAAE,cAAc,GAAG,MAAM;IAKrC,SAAS,CACd,IAAI,EAAE,cAAc,EACpB,QAAQ,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,GACpC,MAAM,IAAI;IAmBN,WAAW,CAAC,IAAI,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,GAAG,IAAI;CAUrE"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { KeyMap as b } from "./definitions/KeyMap.js";
|
|
2
|
+
class o {
|
|
3
|
+
symbolMap = {};
|
|
4
|
+
nextSubscriberId = 0;
|
|
5
|
+
subscribers = {};
|
|
6
|
+
constructor(s) {
|
|
7
|
+
const e = Object.entries(b).reduce(
|
|
8
|
+
(t, r) => {
|
|
9
|
+
if (r[1].on === "key") {
|
|
10
|
+
const i = r[0].slice(3);
|
|
11
|
+
t[i] = r[1].value.match(/[a-z]/) ? r[1].value.toUpperCase() : r[1].value;
|
|
12
|
+
}
|
|
13
|
+
return t;
|
|
14
|
+
},
|
|
15
|
+
{}
|
|
16
|
+
);
|
|
17
|
+
console.log(e), this.symbolMap = { ...e, ...s };
|
|
18
|
+
}
|
|
19
|
+
setSymbol(s, e) {
|
|
20
|
+
if (this.symbolMap[s] === e) return;
|
|
21
|
+
this.symbolMap[s] = e;
|
|
22
|
+
const r = this.subscribers[s];
|
|
23
|
+
if (r)
|
|
24
|
+
for (const i of r.values())
|
|
25
|
+
i.callback(e);
|
|
26
|
+
}
|
|
27
|
+
resolve(s) {
|
|
28
|
+
return console.log("resolve", s, this.symbolMap[s] ?? b[s].value), this.symbolMap[s] ?? b[s].value;
|
|
29
|
+
}
|
|
30
|
+
subscribe(s, e) {
|
|
31
|
+
const t = this.nextSubscriberId++;
|
|
32
|
+
this.subscribers[s] || (this.subscribers[s] = /* @__PURE__ */ new Map()), this.subscribers[s].set(t, { id: t, callback: e });
|
|
33
|
+
const r = this.symbolMap[s];
|
|
34
|
+
return r !== void 0 && e(r), () => {
|
|
35
|
+
this.unsubscribe(s, t);
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
unsubscribe(s, e) {
|
|
39
|
+
const t = this.subscribers[s];
|
|
40
|
+
t && (t.delete(e), t.size === 0 && Reflect.deleteProperty(this.subscribe, s));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export {
|
|
44
|
+
o as HotkeyTextResolver
|
|
45
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { HotkeyRegistry } from '../HotkeyRegistry';
|
|
2
|
+
import { HotkeyTextResolver } from '../HotkeyTextResolver';
|
|
3
|
+
export type HotkeyContextProps = {
|
|
4
|
+
registry: HotkeyRegistry;
|
|
5
|
+
textResolver: HotkeyTextResolver;
|
|
6
|
+
};
|
|
7
|
+
export declare const HotkeyContext: import('react').Context<HotkeyContextProps | undefined>;
|
|
8
|
+
//# sourceMappingURL=HotkeyContext.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HotkeyContext.d.ts","sourceRoot":"","sources":["../../src/lib/context/HotkeyContext.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAEhE,MAAM,MAAM,kBAAkB,GAAG;IAC/B,QAAQ,EAAE,cAAc,CAAC;IACzB,YAAY,EAAE,kBAAkB,CAAC;CAClC,CAAC;AAEF,eAAO,MAAM,aAAa,yDAEzB,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { PropsWithChildren } from 'react';
|
|
2
|
+
import { HotkeyProviderProps } from '../types/hotkey/provider/HotkeyProviderProps';
|
|
3
|
+
export declare function HotkeyProvider({ children, sequenceTimeout, chordTimeout, }: PropsWithChildren<HotkeyProviderProps>): import("react/jsx-runtime").JSX.Element;
|
|
4
|
+
//# sourceMappingURL=HotkeyProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HotkeyProvider.d.ts","sourceRoot":"","sources":["../../src/lib/context/HotkeyProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAC;AAI/C,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,8CAA8C,CAAC;AAGxF,wBAAgB,cAAc,CAAC,EAC7B,QAAQ,EACR,eAAe,EACf,YAAY,GACb,EAAE,iBAAiB,CAAC,mBAAmB,CAAC,2CA6BxC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { jsx as u } from "react/jsx-runtime";
|
|
2
|
+
import { useContext as v, useState as s, useEffect as a } from "react";
|
|
3
|
+
import { HotkeyContext as d } from "./HotkeyContext.js";
|
|
4
|
+
import { HotkeyRegistry as l } from "../HotkeyRegistry.js";
|
|
5
|
+
import { HotkeyTextResolver as f } from "../HotkeyTextResolver.js";
|
|
6
|
+
function K({
|
|
7
|
+
children: y,
|
|
8
|
+
sequenceTimeout: i,
|
|
9
|
+
chordTimeout: m
|
|
10
|
+
}) {
|
|
11
|
+
const p = v(d), [o] = s(
|
|
12
|
+
p?.textResolver ?? new f()
|
|
13
|
+
), [e] = s(
|
|
14
|
+
new l(i, m, o)
|
|
15
|
+
);
|
|
16
|
+
return a(() => {
|
|
17
|
+
const n = (t) => e.handleKeydown(t), r = (t) => e.handleKeyup(t);
|
|
18
|
+
return addEventListener("keydown", n), addEventListener("keyup", r), () => {
|
|
19
|
+
removeEventListener("keydown", n), removeEventListener("keyup", r);
|
|
20
|
+
};
|
|
21
|
+
}, [e, e.handleKeydown, e.handleKeyup]), /* @__PURE__ */ u(
|
|
22
|
+
d.Provider,
|
|
23
|
+
{
|
|
24
|
+
value: { registry: e, textResolver: o },
|
|
25
|
+
children: y
|
|
26
|
+
}
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
export {
|
|
30
|
+
K as HotkeyProvider
|
|
31
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ChordNode } from '../types/hotkey/chord/ChordNode';
|
|
2
|
+
import { ModifierKeyCode } from '../types/key/ModifierKeyCode';
|
|
3
|
+
import { PrimaryKeyCode } from '../types/key/PrimaryKeyCode';
|
|
4
|
+
import { PrimaryKeyValue } from '../types/key/PrimaryKeyValue';
|
|
5
|
+
export declare const createHotkeyChordBuilder: (mods: ModifierKeyCode[]) => ChordNode<ModifierKeyCode, PrimaryKeyCode | PrimaryKeyValue>;
|
|
6
|
+
//# sourceMappingURL=Chord.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Chord.d.ts","sourceRoot":"","sources":["../../src/lib/definitions/Chord.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iCAAiC,CAAC;AACjE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AACpE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAGpE,eAAO,MAAM,wBAAwB,GACnC,MAAM,eAAe,EAAE,KACtB,SAAS,CAAC,eAAe,EAAE,cAAc,GAAG,eAAe,CAkCK,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { detectOS as i } from "../util/detectOS.js";
|
|
2
|
+
import { ALL_MODIFIER_KEY_CODES as n } from "./ModifierKeyCodes.js";
|
|
3
|
+
import { KeyMap as a } from "./KeyMap.js";
|
|
4
|
+
const l = (r) => new Proxy(
|
|
5
|
+
{},
|
|
6
|
+
{
|
|
7
|
+
get(s, e) {
|
|
8
|
+
if (typeof e == "string") {
|
|
9
|
+
if (n.indexOf(e) === -1) {
|
|
10
|
+
const o = a[e], t = {
|
|
11
|
+
Meta: !1,
|
|
12
|
+
Alt: !1,
|
|
13
|
+
Control: !1,
|
|
14
|
+
Shift: !1
|
|
15
|
+
};
|
|
16
|
+
return r.forEach((f) => {
|
|
17
|
+
f === "Mod" ? t[i() === "macOS" ? "Meta" : "Control"] = !0 : t[f] = !0;
|
|
18
|
+
}), {
|
|
19
|
+
type: "chord",
|
|
20
|
+
modifier: t,
|
|
21
|
+
resolve: o.on,
|
|
22
|
+
primaryValue: o.value
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
return l([...r, e]);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
);
|
|
30
|
+
export {
|
|
31
|
+
l as createHotkeyChordBuilder
|
|
32
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { HotkeyChordBuilder } from '../types/hotkey/chord/HotkeyChordBuilder';
|
|
2
|
+
import { HotkeySequenceBuilder } from '../types/hotkey/sequence/HotkeySequenceBuilder';
|
|
3
|
+
export declare const Hotkey: {
|
|
4
|
+
readonly Chord: HotkeyChordBuilder;
|
|
5
|
+
readonly Sequence: HotkeySequenceBuilder;
|
|
6
|
+
};
|
|
7
|
+
//# sourceMappingURL=Hotkey.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Hotkey.d.ts","sourceRoot":"","sources":["../../src/lib/definitions/Hotkey.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0CAA0C,CAAC;AACnF,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,gDAAgD,CAAC;AAI5F,eAAO,MAAM,MAAM;oBACJ,kBAAkB;uBAGf,qBAAqB;CAG7B,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { createHotkeyChordBuilder as e } from "./Chord.js";
|
|
2
|
+
import { createHotkeySequenceBuilder as r } from "./Sequence.js";
|
|
3
|
+
const u = {
|
|
4
|
+
get Chord() {
|
|
5
|
+
return e([]);
|
|
6
|
+
},
|
|
7
|
+
get Sequence() {
|
|
8
|
+
return r([]);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
export {
|
|
12
|
+
u as Hotkey
|
|
13
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { KeyDescription } from '../types/key/KeyDescription';
|
|
2
|
+
import { PrimaryKeyCode } from '../types/key/PrimaryKeyCode';
|
|
3
|
+
import { PrimaryKeyValue } from '../types/key/PrimaryKeyValue';
|
|
4
|
+
export declare const KeyMap: Record<PrimaryKeyCode, KeyDescription<"code">> & Record<PrimaryKeyValue, KeyDescription<"key">>;
|
|
5
|
+
//# sourceMappingURL=KeyMap.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"KeyMap.d.ts","sourceRoot":"","sources":["../../src/lib/definitions/KeyMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAEpE,eAAO,MAAM,MAAM,EAAE,MAAM,CAAC,cAAc,EAAE,cAAc,CAAC,MAAM,CAAC,CAAC,GACjE,MAAM,CAAC,eAAe,EAAE,cAAc,CAAC,KAAK,CAAC,CA6iB9C,CAAC"}
|