Skip to content

Commit

Permalink
fix: Fix problem with element scope (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaokun1219 authored Apr 11, 2022
1 parent 9d3cf81 commit 973e607
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
16 changes: 10 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ const eachUnbind = ({
};

// 对监听对应快捷键的回调函数进行处理
function eventHandler(event, handler, scope) {
function eventHandler(event, handler, scope, element) {
if (handler.element !== element) {
return;
}
let modifiersMatch;

// 看它是否在当前范围
Expand Down Expand Up @@ -190,7 +193,7 @@ function eventHandler(event, handler, scope) {
}

// 处理keydown事件
function dispatch(event) {
function dispatch(event, element) {
const asterisk = _handlers['*'];
let key = event.keyCode || event.which || event.charCode;

Expand Down Expand Up @@ -278,7 +281,7 @@ function dispatch(event) {
&& ((event.type === 'keydown' && asterisk[i].keydown)
|| (event.type === 'keyup' && asterisk[i].keyup))
) {
eventHandler(event, asterisk[i], scope);
eventHandler(event, asterisk[i], scope, element);
}
}
}
Expand All @@ -300,7 +303,7 @@ function dispatch(event) {
}
if (_downKeysCurrent.sort().join('') === _downKeys.sort().join('')) {
// 找到处理内容
eventHandler(event, record, scope);
eventHandler(event, record, scope, element);
}
}
}
Expand Down Expand Up @@ -361,13 +364,14 @@ function hotkeys(key, option, method) {
method,
key: keys[i],
splitKey,
element,
});
}
// 在全局document上设置快捷键
if (typeof element !== 'undefined' && !isElementBind(element) && window) {
elementHasBindEvent.push(element);
addEvent(element, 'keydown', (e) => {
dispatch(e);
dispatch(e, element);
});
if (!winListendFocus) {
winListendFocus = true;
Expand All @@ -376,7 +380,7 @@ function hotkeys(key, option, method) {
});
}
addEvent(element, 'keyup', (e) => {
dispatch(e);
dispatch(e, element);
clearModifier(e);
});
}
Expand Down
10 changes: 5 additions & 5 deletions test/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function __triggerKeyboardFocus(el, keyCode, opt) {
beforeAll(async () => {
browser = await puppeteer.launch({ args: ['--no-sandbox'] });
page = await browser.newPage();
});
}, 1000 * 120);

describe('\n Hotkeys.js Test Case222.\n', () => {
test('HTML loader', async () => {
Expand Down Expand Up @@ -422,31 +422,31 @@ describe('\n Hotkeys.js Test Case222.\n', () => {
* 解决三键组合,实现键值比对,
* 并不是对象比对,此测试用例无法模拟
*/
expect(callbackA.mock.calls.length).toBe(2);
expect(callbackA.mock.calls.length).toBe(1);

hotkeys.unbind('shift+a', callbackA);

__triggerKeyboardEvent(document.body, 65, {
shiftKey: true,
});

expect(callbackA.mock.calls.length).toBe(2);
expect(callbackA.mock.calls.length).toBe(1);

hotkeys('shift+a', callbackB);

__triggerKeyboardEvent(document.body, 65, {
shiftKey: true,
});

expect(callbackB.mock.calls.length).toBe(2);
expect(callbackB.mock.calls.length).toBe(1);

hotkeys.unbind('shift+a', callbackB);

__triggerKeyboardEvent(document.body, 65, {
shiftKey: true,
});

expect(callbackB.mock.calls.length).toBe(2);
expect(callbackB.mock.calls.length).toBe(1);
});

test('HotKeys Key combination Test Case', async () => {
Expand Down

0 comments on commit 973e607

Please sign in to comment.