fix: Fix problem with element scope (#352)

pull/362/head
zhaokun1219 3 years ago committed by GitHub
parent 9d3cf81071
commit 973e607a26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -150,7 +150,10 @@ const eachUnbind = ({
};
// 对监听对应快捷键的回调函数进行处理
function eventHandler(event, handler, scope) {
function eventHandler(event, handler, scope, element) {
if (handler.element !== element) {
return;
}
let modifiersMatch;
// 看它是否在当前范围
@ -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;
@ -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);
}
}
}
@ -300,7 +303,7 @@ function dispatch(event) {
}
if (_downKeysCurrent.sort().join('') === _downKeys.sort().join('')) {
// 找到处理内容
eventHandler(event, record, scope);
eventHandler(event, record, scope, element);
}
}
}
@ -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;
@ -376,7 +380,7 @@ function hotkeys(key, option, method) {
});
}
addEvent(element, 'keyup', (e) => {
dispatch(e);
dispatch(e, element);
clearModifier(e);
});
}

@ -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 () => {
@ -422,7 +422,7 @@ 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);
@ -430,7 +430,7 @@ describe('\n Hotkeys.js Test Case222.\n', () => {
shiftKey: true,
});
expect(callbackA.mock.calls.length).toBe(2);
expect(callbackA.mock.calls.length).toBe(1);
hotkeys('shift+a', callbackB);
@ -438,7 +438,7 @@ describe('\n Hotkeys.js Test Case222.\n', () => {
shiftKey: true,
});
expect(callbackB.mock.calls.length).toBe(2);
expect(callbackB.mock.calls.length).toBe(1);
hotkeys.unbind('shift+a', callbackB);
@ -446,7 +446,7 @@ describe('\n Hotkeys.js Test Case222.\n', () => {
shiftKey: true,
});
expect(callbackB.mock.calls.length).toBe(2);
expect(callbackB.mock.calls.length).toBe(1);
});
test('HotKeys Key combination Test Case', async () => {

Loading…
Cancel
Save