@peter.naydenov/shortcuts 3.5.0 → 3.5.1

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/Changelog.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
 
4
4
 
5
+ ### 3.5.1 ( 2025-10-20 )
6
+ - [x] Update in package.json. Field 'types' was added;
7
+
8
+
9
+
5
10
  ### 3.5.0 ( 2025-10-19)
6
11
  - [x] Featire: New method 'listPlugins' to get list of enabled plugins;
7
12
  - [x] Upgrade: Improvment of type definitions. New d.ts files were added;
package/README.md CHANGED
@@ -6,14 +6,15 @@
6
6
  ![npm bundle size](https://img.shields.io/bundlephobia/minzip/%40peter.naydenov%2Fshortcuts)
7
7
 
8
8
 
9
-
10
- Describe all page activities as list of shortcuts wrapped in contexts. Switch among available contexts.
11
- Library has a plugin system to extend the possible `shortcut names`/`event coverage`. Plugins role is to convert DOM events to shortcut strings, then the core part will trigger the action functions related to the shortcut. At the moment we have 3 plugins:
12
- - `key` - converts keyboard events to shortcut names;
13
- - `click` - converts mouse events to shortcut names;
14
- - `form` - watches for form elements changes;
15
9
 
16
- Planned work on the plugins for `scroll`, `drag-drop`, etc...
10
+ A shortcut management library that wrappes shortcut definitions in a context and allows to switch between contexts. Shortcuts are events triggered by keyboard, mouse, and DOM events. The library has a plugin system that makes the library extensible.
11
+
12
+ Currently existing plugins:
13
+ - `key` - Converts keyboard events to shortcuts;
14
+ - `click` - Converts mouse events to shortcuts;
15
+ - `form` - Form element changes to shortcuts;
16
+
17
+ Planned work on plugins: `scroll`, `drag-drop`, etc.
17
18
 
18
19
 
19
20
 
@@ -49,9 +50,9 @@ Load a shortcut definition by calling `load` method.
49
50
 
50
51
  ```js
51
52
  // for es6 module projects:
52
- include { shortcuts, pluginKey, pluginClick } from '@peter.naydenov/shortcuts'
53
+ import { shortcuts, pluginKey, pluginClick, pluginForm } from '@peter.naydenov/shortcuts'
53
54
  // for commonjs projects:
54
- const { shortcuts, pluginKey, pluginClick } = require('@peter.naydenov/shortcuts')
55
+ const { shortcuts, pluginKey, pluginClick, pluginForm } = require('@peter.naydenov/shortcuts')
55
56
 
56
57
 
57
58
 
@@ -484,6 +485,32 @@ Plugin options are provided as a second argument during the plugin enabling. It'
484
485
 
485
486
 
486
487
 
488
+ ## TypeScript Support
489
+
490
+ The library includes TypeScript definitions. Install the package and TypeScript will automatically detect the types.
491
+
492
+ ```typescript
493
+ import { shortcuts, pluginKey, pluginClick, pluginForm } from '@peter.naydenov/shortcuts';
494
+
495
+ const short: ShortcutsAPI = shortcuts();
496
+ short.enablePlugin(pluginKey);
497
+ short.enablePlugin(pluginClick);
498
+ short.enablePlugin(pluginForm);
499
+
500
+ // Type-safe shortcut definitions
501
+ const shortcutDefinition = {
502
+ myContext: {
503
+ 'key:ctrl+s': () => console.log('Saved'),
504
+ 'click:left-1': (args: { target: HTMLElement }) => console.log('Clicked', args.target)
505
+ }
506
+ };
507
+
508
+ short.load(shortcutDefinition);
509
+ short.changeContext('myContext');
510
+ ```
511
+
512
+ The `ShortcutsAPI` interface provides full type safety for all methods and their parameters.
513
+
487
514
  ## Links
488
515
 
489
516
  - [History of changes](https://github.com/PeterNaydenov/shortcuts/blob/main/Changelog.md)
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@peter.naydenov/shortcuts",
3
3
  "description": "Context control of shortcuts based on keyboard and mouse events",
4
- "version": "3.5.0",
4
+ "version": "3.5.1",
5
5
  "license": "MIT",
6
6
  "author": "Peter Naydenov",
7
7
  "main": "./dist/shortcuts.umd.js",
8
+ "types": "./dist/main.d.ts",
8
9
  "type": "module",
9
10
  "exports": {
10
11
  ".": {
@@ -21,7 +22,7 @@
21
22
  "dev": "vite",
22
23
  "build": "rollup -c && npm run types",
23
24
  "test": "vitest",
24
- "test:ui": "vitest --ui",
25
+ "test:ui": "vitest --ui --open",
25
26
  "cover": "vitest run --coverage",
26
27
  "types": "tsc"
27
28
  },
@@ -49,7 +50,7 @@
49
50
  "react": "^19.2.0",
50
51
  "react-dom": "^19.2.0",
51
52
  "typescript": "^5.9.3",
52
- "vite": "^7.1.10",
53
+ "vite": "^7.1.11",
53
54
  "vitest": "^3.2.4"
54
55
  },
55
56
  "keywords": [
@@ -53,13 +53,13 @@ const contextDefinition = {
53
53
  , extend : {
54
54
  'form : watch' : () => 'input'
55
55
  , 'form : define' : () => 'input'
56
- , 'form : action' : () => [
57
- {
58
- fn : (e) => console.log ( e.target )
59
- , type : 'input'
60
- , mode : 'in'
61
- }
62
- ]
56
+ , 'form : action' : () => [
57
+ {
58
+ fn : (e) => console.log ( e.target )
59
+ , type : 'input'
60
+ , timing : 'in'
61
+ }
62
+ ]
63
63
  }
64
64
  }
65
65
 
@@ -67,7 +67,7 @@ let short = shortcuts ();
67
67
 
68
68
 
69
69
 
70
- describe.skip ( 'Form plugin', () => {
70
+ describe.only ( 'Form plugin', () => {
71
71
 
72
72
  beforeEach ( async () => {
73
73
  short.load ( contextDefinition )
@@ -86,5 +86,48 @@ describe.skip ( 'Form plugin', () => {
86
86
  }) // afterEach
87
87
 
88
88
 
89
-
89
+ it ( 'Shortcut when plugin is not installed', async () => {
90
+ const ls = short.listShortcuts ( 'extend' )
91
+ expect ( ls ).to.includes ( 'form : action' )
92
+ }) // it Shortcuts when plugin is not installed
93
+
94
+
95
+ it ( 'Shortcuts when plugin is enabled', async () => {
96
+ short.enablePlugin ( pluginForm )
97
+ const ls = short.listShortcuts ( 'extend' )
98
+ expect ( ls ).to.includes ( 'FORM:WATCH' )
99
+ // Shortcut names are normalized by the plugins!
100
+ expect ( ls ).to.not.includes ( 'form : watch' )
101
+ }) // it Shortcuts when plugin is enabled
102
+
103
+
104
+
105
+ it ( 'Simpler form listener. Only "form:action" defined', async () => {
106
+ // Uses predefined 'watch' and 'define' functions
107
+ short.enablePlugin ( pluginForm )
108
+ let edit = 'none';
109
+
110
+ const contextExtension = {
111
+ 'local' : {
112
+ 'form: action' : () => [{
113
+ fn : () => edit = 'changed'
114
+ , type : 'input'
115
+ , timing : 'instant'
116
+ }]
117
+ }
118
+ };
119
+ short.load ( contextExtension )
120
+ short.changeContext ( 'local' )
121
+ let input = document.getElementById ( 'name' )
122
+ input.focus ()
123
+ await userEvent.keyboard ( 'hello' )
124
+ await wait ( 50 )
125
+ await waitFor ( () => {
126
+ expect ( edit ).to.equal ( 'changed' )
127
+ }, { timeout: 1000, interval: 12 })
128
+ }) // it Simpler form listener
129
+
130
+
131
+
132
+
90
133
  }) // describe