@peter.naydenov/shortcuts 1.0.0 → 1.1.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/Changelog.md +6 -0
- package/README.md +14 -1
- package/package.json +10 -6
- package/src/changeContext.js +0 -4
- package/src/listen.js +2 -1
- package/src/main.js +2 -0
- package/test/01-general.cy.js +17 -0
package/Changelog.md
CHANGED
package/README.md
CHANGED
|
@@ -3,7 +3,15 @@
|
|
|
3
3
|

|
|
4
4
|

|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
Define a context based keyboard-shortcuts and describe a mouse clicks. Switch among contexts.
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## What's new?
|
|
14
|
+
Version 1.1.0 is coming with new method `emit` that make possible to trigger context functions also programmatically. In `shortcuts` you can mix keyboard, mouse and programmatical events that is prity everything that can happen in a web page.
|
|
7
15
|
|
|
8
16
|
|
|
9
17
|
|
|
@@ -11,6 +19,10 @@ Build a keyboard shortcuts maps and describe a mouse clicks. Control them on con
|
|
|
11
19
|
The shortcuts definition includes a context name and a set of rules(object). The rules are a set of key-value pairs. The key is a shortcut name and the value is a function or array of functions, to be executed when the shortcut is triggered (action function).
|
|
12
20
|
|
|
13
21
|
```js
|
|
22
|
+
// { context: { shortcutName: actionFunction } }
|
|
23
|
+
// or
|
|
24
|
+
// { context: { shortcutName: [ actionFunction1, actionFunction2 ] }}
|
|
25
|
+
```
|
|
14
26
|
// Shortcut definition object:
|
|
15
27
|
{
|
|
16
28
|
contextName : {
|
|
@@ -229,6 +241,7 @@ Description of the methods of shortcut instance:
|
|
|
229
241
|
load : 'Load and extend a shortcut definition.'
|
|
230
242
|
, unload : 'Remove a shortcut context with all its shortcuts.'
|
|
231
243
|
, changeContext : 'Switch to existing shortcut context.'
|
|
244
|
+
, emit : 'Trigger a shortcut or custom event programmatically.'
|
|
232
245
|
, pause : 'Stop listening for shortcuts.'
|
|
233
246
|
, resume : 'Resume listening for shortcuts.'
|
|
234
247
|
, listContexts : 'Return list of available contexts.'
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peter.naydenov/shortcuts",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Context control of shortcuts based on keyboard and mouse events",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"shortcut",
|
|
7
7
|
"key",
|
|
@@ -16,13 +16,17 @@
|
|
|
16
16
|
"build": "vite build",
|
|
17
17
|
"test": "cypress open --component --browser chrome test"
|
|
18
18
|
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/PeterNaydenov/shortcuts"
|
|
22
|
+
},
|
|
19
23
|
"dependencies": {
|
|
20
|
-
"@peter.naydenov/notice": "^2.1
|
|
24
|
+
"@peter.naydenov/notice": "^2.2.1"
|
|
21
25
|
},
|
|
22
26
|
"devDependencies": {
|
|
23
|
-
"@vitejs/plugin-react": "^4.0
|
|
24
|
-
"chai": "^4.3.
|
|
25
|
-
"cypress": "^
|
|
27
|
+
"@vitejs/plugin-react": "^4.1.0",
|
|
28
|
+
"chai": "^4.3.10",
|
|
29
|
+
"cypress": "^13.3.0",
|
|
26
30
|
"mocha": "^10.2.0",
|
|
27
31
|
"react": "^18.2.0",
|
|
28
32
|
"react-dom": "^18.2.0",
|
package/src/changeContext.js
CHANGED
package/src/listen.js
CHANGED
|
@@ -93,6 +93,7 @@ function listen ( dependencies, options, currentContext ) { // Listen for inpu
|
|
|
93
93
|
|
|
94
94
|
function listenMouse () {
|
|
95
95
|
window.addEventListener ( 'contextmenu', event => { // Listen for right mouse clicks
|
|
96
|
+
event.preventDefault ()
|
|
96
97
|
clearTimeout ( mouseTimer )
|
|
97
98
|
if ( mouseIgnore ) {
|
|
98
99
|
clearTimeout ( mouseIgnore )
|
|
@@ -104,7 +105,6 @@ function listen ( dependencies, options, currentContext ) { // Listen for inpu
|
|
|
104
105
|
mouseIgnore = setTimeout ( () => mouseIgnore=null, mouseWait )
|
|
105
106
|
return
|
|
106
107
|
}
|
|
107
|
-
event.preventDefault ()
|
|
108
108
|
mouseTarget = findTarget (event.target, clickTarget )
|
|
109
109
|
mouseDomEvent = event
|
|
110
110
|
count++
|
|
@@ -112,6 +112,7 @@ function listen ( dependencies, options, currentContext ) { // Listen for inpu
|
|
|
112
112
|
})
|
|
113
113
|
|
|
114
114
|
document.addEventListener ( 'click', event => { // Listen for left and middle mouse clicks
|
|
115
|
+
event.preventDefault ()
|
|
115
116
|
clearTimeout ( mouseTimer )
|
|
116
117
|
if ( mouseIgnore ) {
|
|
117
118
|
clearTimeout ( mouseIgnore )
|
package/src/main.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
* History notes:
|
|
10
10
|
* - Development was started on June 21st, 2023
|
|
11
11
|
* - First version was published on August 14th, 2023
|
|
12
|
+
* - Method 'emit' was added on September 30st, 2023
|
|
12
13
|
*/
|
|
13
14
|
|
|
14
15
|
|
|
@@ -67,6 +68,7 @@ function main ( options = {} ) {
|
|
|
67
68
|
, changeContext : changeContext ( shortcuts, listenOptions, ev, currentContext )
|
|
68
69
|
, pause : () => ev.stop ()
|
|
69
70
|
, resume : () => ev.start ()
|
|
71
|
+
, emit : (x,...args) => ev.emit ( readShortcut(x), ...args )
|
|
70
72
|
, listContexts : () => Object.keys ( shortcuts )
|
|
71
73
|
, getContext
|
|
72
74
|
, getNote
|
package/test/01-general.cy.js
CHANGED
|
@@ -26,6 +26,8 @@ beforeEach ( () => {
|
|
|
26
26
|
a = false, b = false
|
|
27
27
|
}) // beforeEach
|
|
28
28
|
|
|
29
|
+
|
|
30
|
+
|
|
29
31
|
it ( 'Simple shortcut', done => {
|
|
30
32
|
let res = false;
|
|
31
33
|
short.changeContext ( 'general' )
|
|
@@ -108,4 +110,19 @@ it ( 'Double mouse click', done => {
|
|
|
108
110
|
})
|
|
109
111
|
}) // it double mouse click
|
|
110
112
|
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
it ( 'Emit custom event', () => {
|
|
116
|
+
let result = null;
|
|
117
|
+
const myAllContext = {
|
|
118
|
+
myAll: {
|
|
119
|
+
'mouse-click-leff-1' : () => console.log ( 'nothing' )
|
|
120
|
+
, 'yo' : r => result = r
|
|
121
|
+
}}
|
|
122
|
+
short.load ( myAllContext )
|
|
123
|
+
short.changeContext ( 'myAll' )
|
|
124
|
+
short.emit ( 'yo', 'hello' )
|
|
125
|
+
expect ( result ).to.be.equal ( 'hello' )
|
|
126
|
+
}) // it emit custom event
|
|
127
|
+
|
|
111
128
|
}) // describe
|