fix: `single` config will conflict with other combinations (#465)

pull/466/head
AllenLee 1 year ago committed by GitHub
parent cfd47cab74
commit 964d66f0d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

4
package-lock.json generated

@ -1,12 +1,12 @@
{
"name": "hotkeys-js",
"version": "3.12.2",
"version": "3.13.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "hotkeys-js",
"version": "3.12.2",
"version": "3.13.1",
"license": "MIT",
"devDependencies": {
"@babel/eslint-parser": "^7.18.9",

@ -369,6 +369,9 @@ function hotkeys(key, option, method) {
if (typeof option === 'string') scope = option;
// 如果只允许单个callback先unbind
if (single) unbind(key, scope);
// 对于每个快捷键进行处理
for (; i < keys.length; i++) {
key = keys[i].split(splitKey); // 按键列表
@ -383,8 +386,6 @@ function hotkeys(key, option, method) {
// 判断key是否在_handlers中不在就赋一个空数组
if (!(key in _handlers)) _handlers[key] = [];
// 如果只允许单个callback重新设置_handlers
if (single) _handlers[key] = [];
_handlers[key].push({
keyup,

@ -577,6 +577,30 @@ describe('\n Hotkeys.js Test Case222.\n', () => {
hotkeys.unbind('⌃+a');
});
test('HotKeys Key single callback Test Case', async () => {
hotkeys('ctrl+s', () => {
expect(false).toBeTruthy();
});
hotkeys('ctrl+s', { single: true }, (e) => {
expect(e.keyCode).toBe(83);
expect(e.ctrlKey).toBeTruthy();
});
hotkeys('ctrl+shift+s', { single: true }, (e) => {
expect(e.shiftKey).toBeTruthy();
expect(e.keyCode).toBe(83);
expect(e.ctrlKey).toBeTruthy();
});
__triggerKeyboardEvent(document.body, 83, {
ctrlKey: true,
shiftKey: true,
});
__triggerKeyboardEvent(document.body, 83, {
ctrlKey: true,
});
expect.assertions(5);
});
// const _modifier = { //修饰键
// '⇧': 16, shift: 16,
// '⌥': 18, alt: 18, option: 18,

Loading…
Cancel
Save