From 7bf34e9dfad1b11a75815e74624d7181d41ff1d3 Mon Sep 17 00:00:00 2001 From: AllenLee <648384410li@gmail.com> Date: Fri, 8 Dec 2023 20:52:11 +0800 Subject: [PATCH] feat: add single option. (#463) * feat: single callback * type: Option.single --- dist/hotkeys.common.js | 3 ++- dist/hotkeys.common.min.js | 2 +- dist/hotkeys.esm.js | 3 ++- dist/hotkeys.js | 3 ++- dist/hotkeys.min.js | 2 +- index.d.ts | 29 +++++++++++++++-------------- package-lock.json | 7 +++++-- src/index.js | 5 +++++ 8 files changed, 33 insertions(+), 21 deletions(-) diff --git a/dist/hotkeys.common.js b/dist/hotkeys.common.js index 51415021..64f0a34a 100644 --- a/dist/hotkeys.common.js +++ b/dist/hotkeys.common.js @@ -1,5 +1,5 @@ /**! - * hotkeys-js v3.12.0 + * hotkeys-js v3.12.2 * A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies. * * Copyright (c) 2023 kenny wong @@ -501,6 +501,7 @@ function hotkeys(key, option, method) { if (option.keydown !== undefined) keydown = option.keydown; // eslint-disable-line if (option.capture !== undefined) capture = option.capture; // eslint-disable-line if (typeof option.splitKey === 'string') splitKey = option.splitKey; // eslint-disable-line + if (option.singleton === true) ; // eslint-disable-line } if (typeof option === 'string') scope = option; diff --git a/dist/hotkeys.common.min.js b/dist/hotkeys.common.min.js index 5ff7002f..7b878e7f 100644 --- a/dist/hotkeys.common.min.js +++ b/dist/hotkeys.common.min.js @@ -1,2 +1,2 @@ -/*! hotkeys-js v3.12.0 | MIT © 2023 kenny wong https://jaywcjlove.github.io/hotkeys-js */ +/*! hotkeys-js v3.12.2 | MIT © 2023 kenny wong https://jaywcjlove.github.io/hotkeys-js */ "use strict";var isff="undefined"!=typeof navigator&&0 @@ -499,6 +499,7 @@ function hotkeys(key, option, method) { if (option.keydown !== undefined) keydown = option.keydown; // eslint-disable-line if (option.capture !== undefined) capture = option.capture; // eslint-disable-line if (typeof option.splitKey === 'string') splitKey = option.splitKey; // eslint-disable-line + if (option.singleton === true) ; // eslint-disable-line } if (typeof option === 'string') scope = option; diff --git a/dist/hotkeys.js b/dist/hotkeys.js index 23c12b31..7c333a2a 100644 --- a/dist/hotkeys.js +++ b/dist/hotkeys.js @@ -1,5 +1,5 @@ /**! - * hotkeys-js v3.12.0 + * hotkeys-js v3.12.2 * A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies. * * Copyright (c) 2023 kenny wong @@ -505,6 +505,7 @@ if (option.keydown !== undefined) keydown = option.keydown; // eslint-disable-line if (option.capture !== undefined) capture = option.capture; // eslint-disable-line if (typeof option.splitKey === 'string') splitKey = option.splitKey; // eslint-disable-line + if (option.singleton === true) ; // eslint-disable-line } if (typeof option === 'string') scope = option; diff --git a/dist/hotkeys.min.js b/dist/hotkeys.min.js index 47b5ac7f..68379a49 100644 --- a/dist/hotkeys.min.js +++ b/dist/hotkeys.min.js @@ -1,2 +1,2 @@ -/*! hotkeys-js v3.12.0 | MIT © 2023 kenny wong https://jaywcjlove.github.io/hotkeys-js */ +/*! hotkeys-js v3.12.2 | MIT © 2023 kenny wong https://jaywcjlove.github.io/hotkeys-js */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).hotkeys=t()}(this,function(){"use strict";var e="undefined"!=typeof navigator&&0Uncaught TypeError: hotkeys is not a function(anonymous function) * // @ VM2170:2InjectedScript._evaluateOn @@ -96,7 +97,7 @@ export interface Hotkeys { /** * trigger shortcut key event - * + * * ```js * hotkeys.trigger('ctrl+o'); * hotkeys.trigger('ctrl+o', 'scope2'); @@ -115,7 +116,7 @@ export interface Hotkeys { isPressed(keyCode: string): boolean; /** * Returns an array of key codes currently pressed. - * + * * ```js * hotkeys('command+ctrl+shift+a,f', function() { * console.log(hotkeys.getPressedKeyCodes()); //=> [17, 65] or [70] @@ -125,7 +126,7 @@ export interface Hotkeys { getPressedKeyCodes(): number[]; /** * Returns an array of key codes currently pressed. - * + * * ```js * hotkeys('command+ctrl+shift+a,f', function() { * console.log(hotkeys.getPressedKeyString()); //=> ['⌘', '⌃', '⇧', 'A', 'F'] @@ -135,7 +136,7 @@ export interface Hotkeys { getPressedKeyString(): string[]; /** * Get a list of all registration codes. - * + * * ```js * hotkeys('command+ctrl+shift+a,f', function() { * console.log(hotkeys.getAllKeyCodes()); @@ -145,7 +146,7 @@ export interface Hotkeys { * // ] * }) * ``` - * + * */ getAllKeyCodes(): Omit; @@ -153,7 +154,7 @@ export interface Hotkeys { * By default hotkeys are not enabled for `INPUT` `SELECT` `TEXTAREA` elements. * `Hotkeys.filter` to return to the `true` shortcut keys set to play a role, * `false` shortcut keys set up failure. - * + * * ```js * hotkeys.filter = function(event){ * return true; @@ -165,7 +166,7 @@ export interface Hotkeys { * var tagName = target.tagName; * return !(target.isContentEditable || tagName == 'INPUT' || tagName == 'SELECT' || tagName == 'TEXTAREA'); * } - * + * * hotkeys.filter = function(event){ * var tagName = (event.target || event.srcElement).tagName; * hotkeys.setScope(/^(INPUT|TEXTAREA|SELECT)$/.test(tagName) ? 'input' : 'other'); diff --git a/package-lock.json b/package-lock.json index f3a78b4d..0b22b7f1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "hotkeys-js", - "version": "3.11.2", + "version": "3.12.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "hotkeys-js", - "version": "3.11.2", + "version": "3.12.2", "license": "MIT", "devDependencies": { "@babel/eslint-parser": "^7.18.9", @@ -39,6 +39,9 @@ "rimraf": "^5.0.1", "rollup": "^3.26.2", "uglify-js": "^3.17.4" + }, + "funding": { + "url": "https://jaywcjlove.github.io/#/sponsor" } }, "node_modules/@aashutoshrathi/word-wrap": { diff --git a/src/index.js b/src/index.js index 922c82f3..4055f864 100644 --- a/src/index.js +++ b/src/index.js @@ -350,6 +350,7 @@ function hotkeys(key, option, method) { let keydown = true; let splitKey = '+'; let capture = false; + let single = false; // 单个callback // 对为设定范围的判断 if (method === undefined && typeof option === 'function') { @@ -363,6 +364,7 @@ function hotkeys(key, option, method) { if (option.keydown !== undefined) keydown = option.keydown; // eslint-disable-line if (option.capture !== undefined) capture = option.capture; // eslint-disable-line if (typeof option.splitKey === 'string') splitKey = option.splitKey; // eslint-disable-line + if (option.single === true) single = true; // eslint-disable-line } if (typeof option === 'string') scope = option; @@ -381,6 +383,9 @@ function hotkeys(key, option, method) { // 判断key是否在_handlers中,不在就赋一个空数组 if (!(key in _handlers)) _handlers[key] = []; + // 如果只允许单个callback,重新设置_handlers + if (single) _handlers[key] = []; + _handlers[key].push({ keyup, keydown,