summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-10-20 01:45:31 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-10-20 01:45:31 +0900
commitc66155ed48b5458c7aa4a4c365a8efa86e99d7e5 (patch)
treeb10ab3bab254e60dcc0cb894e2c36368daa7974c
parent10.25.0 (diff)
downloadsharkey-c66155ed48b5458c7aa4a4c365a8efa86e99d7e5.tar.gz
sharkey-c66155ed48b5458c7aa4a4c365a8efa86e99d7e5.tar.bz2
sharkey-c66155ed48b5458c7aa4a4c365a8efa86e99d7e5.zip
Improve shortcut key detection
-rw-r--r--src/client/app/common/hotkey.ts44
1 files changed, 22 insertions, 22 deletions
diff --git a/src/client/app/common/hotkey.ts b/src/client/app/common/hotkey.ts
index dc1a34338a..c7d81a6361 100644
--- a/src/client/app/common/hotkey.ts
+++ b/src/client/app/common/hotkey.ts
@@ -46,6 +46,16 @@ const getKeyMap = keymap => Object.entries(keymap).map(([patterns, callback]): a
const ignoreElemens = ['input', 'textarea'];
+function match(e: KeyboardEvent, patterns: action['patterns']): boolean {
+ const key = e.code.toLowerCase();
+ return patterns.some(pattern => pattern.which.includes(key) &&
+ pattern.ctrl == e.ctrlKey &&
+ pattern.shift == e.shiftKey &&
+ pattern.alt == e.altKey &&
+ e.metaKey == false
+ );
+}
+
export default {
install(Vue) {
Vue.directive('hotkey', {
@@ -55,37 +65,27 @@ export default {
const actions = getKeyMap(binding.value);
// flatten
- const reservedKeys = concat(concat(actions.map(a => a.patterns.map(p => p.which))));
+ const reservedKeys = concat(actions.map(a => a.patterns));
- el.dataset.reservedKeys = reservedKeys.map(key => `'${key}'`).join(' ');
+ el.dataset.reservedKeys = JSON.stringify(reservedKeys);
el._keyHandler = (e: KeyboardEvent) => {
- const key = e.code.toLowerCase();
-
- const targetReservedKeys = document.activeElement ? ((document.activeElement as any).dataset || {}).reservedKeys || '' : '';
+ const targetReservedKeys = JSON.parse(document.activeElement ? ((document.activeElement as any).dataset || {}).reservedKeys || '[]' : '[]');
if (document.activeElement && ignoreElemens.some(el => document.activeElement.matches(el))) return;
for (const action of actions) {
- if (el._hotkey_global && targetReservedKeys.includes(`'${key}'`)) break;
-
- const matched = action.patterns.some(pattern => {
- const matched = pattern.which.includes(key) &&
- pattern.ctrl == e.ctrlKey &&
- pattern.shift == e.shiftKey &&
- pattern.alt == e.altKey &&
- e.metaKey == false;
+ const matched = match(e, action.patterns);
- if (matched) {
- e.preventDefault();
- e.stopPropagation();
- action.callback(e);
- return true;
- } else {
- return false;
+ if (matched) {
+ if (el._hotkey_global) {
+ if (match(e, targetReservedKeys)) {
+ return;
+ }
}
- });
- if (matched) {
+ e.preventDefault();
+ e.stopPropagation();
+ action.callback(e);
break;
}
}