Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: single config will conflict with other combinations #465

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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