Skip to content

Commit 964d66f

Browse files
authored
fix: single config will conflict with other combinations (#465)
1 parent cfd47ca commit 964d66f

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ function hotkeys(key, option, method) {
369369

370370
if (typeof option === 'string') scope = option;
371371

372+
// 如果只允许单个callback,先unbind
373+
if (single) unbind(key, scope);
374+
372375
// 对于每个快捷键进行处理
373376
for (; i < keys.length; i++) {
374377
key = keys[i].split(splitKey); // 按键列表
@@ -383,8 +386,6 @@ function hotkeys(key, option, method) {
383386

384387
// 判断key是否在_handlers中,不在就赋一个空数组
385388
if (!(key in _handlers)) _handlers[key] = [];
386-
// 如果只允许单个callback,重新设置_handlers
387-
if (single) _handlers[key] = [];
388389

389390
_handlers[key].push({
390391
keyup,

test/run.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,30 @@ describe('\n Hotkeys.js Test Case222.\n', () => {
577577
hotkeys.unbind('⌃+a');
578578
});
579579

580+
test('HotKeys Key single callback Test Case', async () => {
581+
hotkeys('ctrl+s', () => {
582+
expect(false).toBeTruthy();
583+
});
584+
hotkeys('ctrl+s', { single: true }, (e) => {
585+
expect(e.keyCode).toBe(83);
586+
expect(e.ctrlKey).toBeTruthy();
587+
});
588+
hotkeys('ctrl+shift+s', { single: true }, (e) => {
589+
expect(e.shiftKey).toBeTruthy();
590+
expect(e.keyCode).toBe(83);
591+
expect(e.ctrlKey).toBeTruthy();
592+
});
593+
__triggerKeyboardEvent(document.body, 83, {
594+
ctrlKey: true,
595+
shiftKey: true,
596+
});
597+
__triggerKeyboardEvent(document.body, 83, {
598+
ctrlKey: true,
599+
});
600+
601+
expect.assertions(5);
602+
});
603+
580604
// const _modifier = { //修饰键
581605
// '⇧': 16, shift: 16,
582606
// '⌥': 18, alt: 18, option: 18,

0 commit comments

Comments
 (0)