Skip to content

Commit

Permalink
fix: single config will conflict with other combinations (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
cangSDARM committed Dec 12, 2023
1 parent cfd47ca commit 964d66f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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); // 按键列表
Expand All @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions test/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 964d66f

Please sign in to comment.