2020-10-17 13:12:00 +02:00
|
|
|
import { Directive } from 'vue';
|
|
|
|
import keyCode from '../scripts/keycode';
|
2020-01-29 20:37:25 +01:00
|
|
|
import { concat } from '../../prelude/array';
|
2018-09-17 22:35:06 +02:00
|
|
|
|
2018-09-18 05:34:41 +02:00
|
|
|
type pattern = {
|
|
|
|
which: string[];
|
|
|
|
ctrl?: boolean;
|
|
|
|
shift?: boolean;
|
|
|
|
alt?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
type action = {
|
|
|
|
patterns: pattern[];
|
|
|
|
|
|
|
|
callback: Function;
|
2020-02-26 09:22:43 +01:00
|
|
|
|
|
|
|
allowRepeat: boolean;
|
2018-09-18 05:34:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const getKeyMap = keymap => Object.entries(keymap).map(([patterns, callback]): action => {
|
|
|
|
const result = {
|
|
|
|
patterns: [],
|
2020-02-26 09:22:43 +01:00
|
|
|
callback: callback,
|
|
|
|
allowRepeat: true
|
2018-09-18 05:34:41 +02:00
|
|
|
} as action;
|
|
|
|
|
2020-02-26 09:22:43 +01:00
|
|
|
if (patterns.match(/^\(.*\)$/) !== null) {
|
|
|
|
result.allowRepeat = false;
|
|
|
|
patterns = patterns.slice(1, -1);
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:34:41 +02:00
|
|
|
result.patterns = patterns.split('|').map(part => {
|
|
|
|
const pattern = {
|
2018-09-18 08:02:15 +02:00
|
|
|
which: [],
|
|
|
|
ctrl: false,
|
|
|
|
alt: false,
|
|
|
|
shift: false
|
2018-09-18 05:34:41 +02:00
|
|
|
} as pattern;
|
|
|
|
|
2018-12-11 12:36:55 +01:00
|
|
|
const keys = part.trim().split('+').map(x => x.trim().toLowerCase());
|
|
|
|
for (const key of keys) {
|
2018-09-18 05:34:41 +02:00
|
|
|
switch (key) {
|
|
|
|
case 'ctrl': pattern.ctrl = true; break;
|
|
|
|
case 'alt': pattern.alt = true; break;
|
|
|
|
case 'shift': pattern.shift = true; break;
|
|
|
|
default: pattern.which = keyCode(key).map(k => k.toLowerCase());
|
2018-09-18 02:11:52 +02:00
|
|
|
}
|
2018-12-11 12:36:55 +01:00
|
|
|
}
|
2018-09-17 22:35:06 +02:00
|
|
|
|
2018-09-18 05:34:41 +02:00
|
|
|
return pattern;
|
|
|
|
});
|
2018-09-17 22:35:06 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
|
|
|
|
const ignoreElemens = ['input', 'textarea'];
|
|
|
|
|
2018-10-19 18:45:31 +02:00
|
|
|
function match(e: KeyboardEvent, patterns: action['patterns']): boolean {
|
|
|
|
const key = e.code.toLowerCase();
|
|
|
|
return patterns.some(pattern => pattern.which.includes(key) &&
|
2020-04-04 01:46:54 +02:00
|
|
|
pattern.ctrl === e.ctrlKey &&
|
|
|
|
pattern.shift === e.shiftKey &&
|
|
|
|
pattern.alt === e.altKey &&
|
2019-05-05 02:27:55 +02:00
|
|
|
!e.metaKey
|
2018-10-19 18:45:31 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-17 22:35:06 +02:00
|
|
|
export default {
|
2020-10-17 13:12:00 +02:00
|
|
|
mounted(el, binding) {
|
|
|
|
el._hotkey_global = binding.modifiers.global === true;
|
|
|
|
|
|
|
|
const actions = getKeyMap(binding.value);
|
|
|
|
|
|
|
|
// flatten
|
|
|
|
const reservedKeys = concat(actions.map(a => a.patterns));
|
|
|
|
|
|
|
|
el._misskey_reservedKeys = reservedKeys;
|
|
|
|
|
|
|
|
el._keyHandler = (e: KeyboardEvent) => {
|
|
|
|
const targetReservedKeys = document.activeElement ? ((document.activeElement as any)._misskey_reservedKeys || []) : [];
|
|
|
|
if (document.activeElement && ignoreElemens.some(el => document.activeElement.matches(el))) return;
|
|
|
|
if (document.activeElement && document.activeElement.attributes['contenteditable']) return;
|
|
|
|
|
|
|
|
for (const action of actions) {
|
|
|
|
const matched = match(e, action.patterns);
|
2018-09-17 22:35:06 +02:00
|
|
|
|
2020-10-17 13:12:00 +02:00
|
|
|
if (matched) {
|
|
|
|
if (!action.allowRepeat && e.repeat) return;
|
|
|
|
if (el._hotkey_global && match(e, targetReservedKeys)) return;
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
action.callback(e);
|
|
|
|
break;
|
2018-09-17 22:35:06 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-17 13:12:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (el._hotkey_global) {
|
|
|
|
document.addEventListener('keydown', el._keyHandler);
|
|
|
|
} else {
|
|
|
|
el.addEventListener('keydown', el._keyHandler);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
unmounted(el) {
|
|
|
|
if (el._hotkey_global) {
|
|
|
|
document.removeEventListener('keydown', el._keyHandler);
|
|
|
|
} else {
|
|
|
|
el.removeEventListener('keydown', el._keyHandler);
|
|
|
|
}
|
2018-09-17 22:35:06 +02:00
|
|
|
}
|
2020-10-17 13:12:00 +02:00
|
|
|
} as Directive;
|