From 4aab68fd94137ceee9beb8ebab039ebaec5330ec Mon Sep 17 00:00:00 2001 From: Sam Magura Date: Fri, 20 May 2022 14:59:10 -0400 Subject: [PATCH] fix: Make it so unbind('') does not remove all event handlers (#372) --- dist/hotkeys.common.js | 19 +++++++++++-------- dist/hotkeys.common.min.js | 4 ++-- dist/hotkeys.esm.js | 19 +++++++++++-------- dist/hotkeys.js | 19 +++++++++++-------- dist/hotkeys.min.js | 4 ++-- package-lock.json | 11 +++++------ src/index.js | 2 +- test/run.test.js | 13 +++++++++++++ 8 files changed, 56 insertions(+), 35 deletions(-) diff --git a/dist/hotkeys.common.js b/dist/hotkeys.common.js index 823d8297..f3091880 100644 --- a/dist/hotkeys.common.js +++ b/dist/hotkeys.common.js @@ -1,5 +1,5 @@ /**! - * hotkeys-js v3.9.0 + * hotkeys-js v3.9.3 * A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies. * * Copyright (c) 2022 kenny wong @@ -11,9 +11,9 @@ var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false; // 绑定事件 -function addEvent(object, event, method) { +function addEvent(object, event, method, useCapture) { if (object.addEventListener) { - object.addEventListener(event, method, false); + object.addEventListener(event, method, useCapture); } else if (object.attachEvent) { object.attachEvent("on".concat(event), function () { method(window.event); @@ -256,7 +256,7 @@ function clearModifier(event) { function unbind(keysInfo) { // unbind(), unbind all keys - if (!keysInfo) { + if (typeof keysInfo === 'undefined') { Object.keys(_handlers).forEach(function (key) { return delete _handlers[key]; }); @@ -480,7 +480,8 @@ function hotkeys(key, option, method) { var i = 0; var keyup = false; var keydown = true; - var splitKey = '+'; // 对为设定范围的判断 + var splitKey = '+'; + var capture = false; // 对为设定范围的判断 if (method === undefined && typeof option === 'function') { method = option; @@ -495,6 +496,8 @@ 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 } @@ -531,19 +534,19 @@ function hotkeys(key, option, method) { elementHasBindEvent.push(element); addEvent(element, 'keydown', function (e) { dispatch(e, element); - }); + }, capture); if (!winListendFocus) { winListendFocus = true; addEvent(window, 'focus', function () { _downKeys = []; - }); + }, capture); } addEvent(element, 'keyup', function (e) { dispatch(e, element); clearModifier(e); - }); + }, capture); } } diff --git a/dist/hotkeys.common.min.js b/dist/hotkeys.common.min.js index 249424c7..d3a8624f 100644 --- a/dist/hotkeys.common.min.js +++ b/dist/hotkeys.common.min.js @@ -1,2 +1,2 @@ -/*! hotkeys-js v3.9.0 | MIT © 2022 kenny wong http://jaywcjlove.github.io/hotkeys */ -"use strict";var isff="undefined"!=typeof navigator&&0 http://jaywcjlove.github.io/hotkeys */ +"use strict";var isff="undefined"!=typeof navigator&&0 @@ -9,9 +9,9 @@ var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false; // 绑定事件 -function addEvent(object, event, method) { +function addEvent(object, event, method, useCapture) { if (object.addEventListener) { - object.addEventListener(event, method, false); + object.addEventListener(event, method, useCapture); } else if (object.attachEvent) { object.attachEvent("on".concat(event), function () { method(window.event); @@ -254,7 +254,7 @@ function clearModifier(event) { function unbind(keysInfo) { // unbind(), unbind all keys - if (!keysInfo) { + if (typeof keysInfo === 'undefined') { Object.keys(_handlers).forEach(function (key) { return delete _handlers[key]; }); @@ -478,7 +478,8 @@ function hotkeys(key, option, method) { var i = 0; var keyup = false; var keydown = true; - var splitKey = '+'; // 对为设定范围的判断 + var splitKey = '+'; + var capture = false; // 对为设定范围的判断 if (method === undefined && typeof option === 'function') { method = option; @@ -493,6 +494,8 @@ 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 } @@ -529,19 +532,19 @@ function hotkeys(key, option, method) { elementHasBindEvent.push(element); addEvent(element, 'keydown', function (e) { dispatch(e, element); - }); + }, capture); if (!winListendFocus) { winListendFocus = true; addEvent(window, 'focus', function () { _downKeys = []; - }); + }, capture); } addEvent(element, 'keyup', function (e) { dispatch(e, element); clearModifier(e); - }); + }, capture); } } diff --git a/dist/hotkeys.js b/dist/hotkeys.js index 4aa33c53..bdcd7cac 100644 --- a/dist/hotkeys.js +++ b/dist/hotkeys.js @@ -1,5 +1,5 @@ /**! - * hotkeys-js v3.9.0 + * hotkeys-js v3.9.3 * A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies. * * Copyright (c) 2022 kenny wong @@ -15,9 +15,9 @@ var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false; // 绑定事件 - function addEvent(object, event, method) { + function addEvent(object, event, method, useCapture) { if (object.addEventListener) { - object.addEventListener(event, method, false); + object.addEventListener(event, method, useCapture); } else if (object.attachEvent) { object.attachEvent("on".concat(event), function () { method(window.event); @@ -260,7 +260,7 @@ function unbind(keysInfo) { // unbind(), unbind all keys - if (!keysInfo) { + if (typeof keysInfo === 'undefined') { Object.keys(_handlers).forEach(function (key) { return delete _handlers[key]; }); @@ -484,7 +484,8 @@ var i = 0; var keyup = false; var keydown = true; - var splitKey = '+'; // 对为设定范围的判断 + var splitKey = '+'; + var capture = false; // 对为设定范围的判断 if (method === undefined && typeof option === 'function') { method = option; @@ -499,6 +500,8 @@ 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 } @@ -535,19 +538,19 @@ elementHasBindEvent.push(element); addEvent(element, 'keydown', function (e) { dispatch(e, element); - }); + }, capture); if (!winListendFocus) { winListendFocus = true; addEvent(window, 'focus', function () { _downKeys = []; - }); + }, capture); } addEvent(element, 'keyup', function (e) { dispatch(e, element); clearModifier(e); - }); + }, capture); } } diff --git a/dist/hotkeys.min.js b/dist/hotkeys.min.js index 7eccd66a..58ea7193 100644 --- a/dist/hotkeys.min.js +++ b/dist/hotkeys.min.js @@ -1,2 +1,2 @@ -/*! hotkeys-js v3.9.0 | MIT © 2022 kenny wong http://jaywcjlove.github.io/hotkeys */ -!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&&0 http://jaywcjlove.github.io/hotkeys */ +!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&&0 delete _handlers[key]); } else if (Array.isArray(keysInfo)) { // support like : unbind([{key: 'ctrl+a', scope: 's1'}, {key: 'ctrl-a', scope: 's2', splitKey: '-'}]) diff --git a/test/run.test.js b/test/run.test.js index be17f18a..c52278ba 100644 --- a/test/run.test.js +++ b/test/run.test.js @@ -185,6 +185,19 @@ describe('\n Hotkeys.js Test Case222.\n', () => { expect(hotkeys.unbind('enter12')).toBe(undefined); }); + test('passing an empty string to unbind does not remove all handlers', () => { + let isExecuteFunction = false; + hotkeys('enter', (e) => { + isExecuteFunction = true; + expect(e.keyCode).toBe(13); + }); + + hotkeys.unbind(''); + + __triggerKeyboardEvent(document.body, 13); + expect(isExecuteFunction).toBeTruthy(); + }); + test('HotKeys Special keys Test Case', async () => { let isExecuteFunction = false; hotkeys('enter', (e) => {