You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hotkeys/dist/hotkeys.esm.js

466 lines
12 KiB
JavaScript

7 years ago
/*!
* hotkeys-js v3.6.6
7 years ago
* A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.
*
6 years ago
* Copyright (c) 2019 kenny wong <wowohoo@qq.com>
* http://jaywcjlove.github.io/hotkeys
7 years ago
*
* Licensed under the MIT license.
*/
6 years ago
var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false; // 绑定事件
7 years ago
function addEvent(object, event, method) {
if (object.addEventListener) {
object.addEventListener(event, method, false);
} else if (object.attachEvent) {
6 years ago
object.attachEvent("on".concat(event), function () {
7 years ago
method(window.event);
});
7 years ago
}
6 years ago
} // 修饰键转换成对应的键码
7 years ago
function getMods(modifier, key) {
7 years ago
var mods = key.slice(0, key.length - 1);
6 years ago
7 years ago
for (var i = 0; i < mods.length; i++) {
mods[i] = modifier[mods[i].toLowerCase()];
6 years ago
}
return mods;
} // 处理传的key字符串转换成数组
7 years ago
function getKeys(key) {
if (!key) key = '';
key = key.replace(/\s/g, ''); // 匹配任何空白字符,包括空格、制表符、换页符等等
6 years ago
7 years ago
var keys = key.split(','); // 同时设置多个快捷键,以','分割
7 years ago
6 years ago
var index = keys.lastIndexOf(''); // 快捷键可能包含',',需特殊处理
7 years ago
for (; index >= 0;) {
keys[index - 1] += ',';
keys.splice(index, 1);
index = keys.lastIndexOf('');
}
return keys;
6 years ago
} // 比较修饰键的数组
7 years ago
function compareArray(a1, a2) {
7 years ago
var arr1 = a1.length >= a2.length ? a1 : a2;
var arr2 = a1.length >= a2.length ? a2 : a1;
var isIndex = true;
7 years ago
7 years ago
for (var i = 0; i < arr1.length; i++) {
7 years ago
if (arr2.indexOf(arr1[i]) === -1) isIndex = false;
7 years ago
}
6 years ago
7 years ago
return isIndex;
7 years ago
}
6 years ago
var _keyMap = {
// 特殊键
7 years ago
backspace: 8,
tab: 9,
clear: 12,
enter: 13,
6 years ago
"return": 13,
7 years ago
esc: 27,
escape: 27,
space: 32,
left: 37,
up: 38,
right: 39,
down: 40,
del: 46,
6 years ago
"delete": 46,
7 years ago
ins: 45,
insert: 45,
home: 36,
end: 35,
pageup: 33,
pagedown: 34,
capslock: 20,
'⇪': 20,
',': 188,
'.': 190,
'/': 191,
'`': 192,
'-': isff ? 173 : 189,
'=': isff ? 61 : 187,
';': isff ? 59 : 186,
'\'': 222,
'[': 219,
']': 221,
7 years ago
'\\': 220
7 years ago
};
6 years ago
var _modifier = {
// 修饰键
7 years ago
'⇧': 16,
shift: 16,
'⌥': 18,
alt: 18,
option: 18,
'⌃': 17,
ctrl: 17,
control: 17,
'⌘': isff ? 224 : 91,
cmd: isff ? 224 : 91,
7 years ago
command: isff ? 224 : 91
7 years ago
};
7 years ago
var _downKeys = []; // 记录摁下的绑定键
6 years ago
7 years ago
var modifierMap = {
7 years ago
16: 'shiftKey',
18: 'altKey',
7 years ago
17: 'ctrlKey'
7 years ago
};
6 years ago
var _mods = {
16: false,
18: false,
17: false
};
var _handlers = {}; // F1~F12 特殊键
7 years ago
7 years ago
for (var k = 1; k < 20; k++) {
6 years ago
_keyMap["f".concat(k)] = 111 + k;
} // 兼容Firefox处理
7 years ago
modifierMap[isff ? 224 : 91] = 'metaKey';
_mods[isff ? 224 : 91] = false;
7 years ago
var _scope = 'all'; // 默认热键范围
7 years ago
6 years ago
var elementHasBindEvent = []; // 已绑定事件的节点记录
7 years ago
// 返回键码
6 years ago
7 years ago
var code = function code(x) {
return _keyMap[x.toLowerCase()] || _modifier[x.toLowerCase()] || x.toUpperCase().charCodeAt(0);
6 years ago
}; // 设置获取当前范围(默认为'所有'
7 years ago
7 years ago
function setScope(scope) {
_scope = scope || 'all';
6 years ago
} // 获取当前范围
7 years ago
function getScope() {
return _scope || 'all';
6 years ago
} // 获取摁下绑定键的键值
7 years ago
function getPressedKeyCodes() {
return _downKeys.slice(0);
6 years ago
} // 表单控件控件判断 返回 Boolean
// hotkey is effective only when filter return true
6 years ago
7 years ago
function filter(event) {
var target = event.target || event.srcElement;
var tagName = target.tagName;
var flag = true; // ignore: isContentEditable === 'true', <input> and <textarea> when readOnly state is false, <select>
if (target.isContentEditable || tagName === 'TEXTAREA' || (tagName === 'INPUT' || tagName === 'TEXTAREA') && !target.readOnly) {
flag = false;
}
return flag;
6 years ago
} // 判断摁下的键是否为某个键返回true或者false
7 years ago
function isPressed(keyCode) {
7 years ago
if (typeof keyCode === 'string') {
7 years ago
keyCode = code(keyCode); // 转换成键码
}
6 years ago
7 years ago
return _downKeys.indexOf(keyCode) !== -1;
6 years ago
} // 循环删除handlers中的所有 scope(范围)
7 years ago
function deleteScope(scope, newScope) {
6 years ago
var handlers;
var i; // 没有指定scope获取scope
7 years ago
if (!scope) scope = getScope();
7 years ago
for (var key in _handlers) {
7 years ago
if (Object.prototype.hasOwnProperty.call(_handlers, key)) {
handlers = _handlers[key];
6 years ago
7 years ago
for (i = 0; i < handlers.length;) {
7 years ago
if (handlers[i].scope === scope) handlers.splice(i, 1);else i++;
7 years ago
}
7 years ago
}
6 years ago
} // 如果scope被删除将scope重置为all
7 years ago
if (getScope() === scope) setScope(newScope || 'all');
6 years ago
} // 清除修饰键
7 years ago
function clearModifier(event) {
7 years ago
var key = event.keyCode || event.which || event.charCode;
7 years ago
6 years ago
var i = _downKeys.indexOf(key); // 从列表中清除按压过的键
6 years ago
if (i >= 0) {
_downKeys.splice(i, 1);
6 years ago
} // 特殊处理 cmmand 键,在 cmmand 组合快捷键 keyup 只执行一次的问题
6 years ago
if (event.key && event.key.toLowerCase() === 'meta') {
_downKeys.splice(0, _downKeys.length);
6 years ago
} // 修饰键 shiftKey altKey ctrlKey (command||metaKey) 清除
7 years ago
if (key === 93 || key === 224) key = 91;
6 years ago
7 years ago
if (key in _mods) {
6 years ago
_mods[key] = false; // 将修饰键重置为false
7 years ago
7 years ago
for (var k in _modifier) {
if (_modifier[k] === key) hotkeys[k] = false;
}
7 years ago
}
6 years ago
} // 解除绑定某个范围的快捷键
7 years ago
function unbind(key, scope, method) {
7 years ago
var multipleKeys = getKeys(key);
6 years ago
var keys;
7 years ago
var mods = [];
6 years ago
var obj; // 通过函数判断,是否解除绑定
// https://github.com/jaywcjlove/hotkeys/issues/44
6 years ago
if (typeof scope === 'function') {
method = scope;
scope = 'all';
}
7 years ago
7 years ago
for (var i = 0; i < multipleKeys.length; i++) {
7 years ago
// 将组合快捷键拆分为数组
6 years ago
keys = multipleKeys[i].split('+'); // 记录每个组合键中的修饰键的键码 返回数组
7 years ago
6 years ago
if (keys.length > 1) mods = getMods(_modifier, keys); // 获取除修饰键外的键值key
7 years ago
key = keys[keys.length - 1];
6 years ago
key = key === '*' ? '*' : code(key); // 判断是否传入范围,没有就获取范围
7 years ago
6 years ago
if (!scope) scope = getScope(); // 如何key不在 _handlers 中返回不做处理
7 years ago
6 years ago
if (!_handlers[key]) return; // 清空 handlers 中数据,
7 years ago
// 让触发快捷键键之后没有事件执行到达解除快捷键绑定的目的
6 years ago
7 years ago
for (var r = 0; r < _handlers[key].length; r++) {
6 years ago
obj = _handlers[key][r]; // 通过函数判断,是否解除绑定,函数相等直接返回
var isMatchingMethod = method ? obj.method === method : true; // 判断是否在范围内并且键值相同
6 years ago
if (isMatchingMethod && obj.scope === scope && compareArray(obj.mods, mods)) {
7 years ago
_handlers[key][r] = {};
}
}
}
6 years ago
} // 对监听对应快捷键的回调函数进行处理
7 years ago
function eventHandler(event, handler, scope) {
6 years ago
var modifiersMatch; // 看它是否在当前范围
7 years ago
if (handler.scope === scope || handler.scope === 'all') {
7 years ago
// 检查是否匹配修饰符如果有返回true
7 years ago
modifiersMatch = handler.mods.length > 0;
7 years ago
for (var y in _mods) {
7 years ago
if (Object.prototype.hasOwnProperty.call(_mods, y)) {
7 years ago
if (!_mods[y] && handler.mods.indexOf(+y) > -1 || _mods[y] && handler.mods.indexOf(+y) === -1) modifiersMatch = false;
7 years ago
}
6 years ago
} // 调用处理程序,如果是修饰键不做处理
7 years ago
7 years ago
if (handler.mods.length === 0 && !_mods[16] && !_mods[18] && !_mods[17] && !_mods[91] || modifiersMatch || handler.shortcut === '*') {
7 years ago
if (handler.method(event, handler) === false) {
7 years ago
if (event.preventDefault) event.preventDefault();else event.returnValue = false;
7 years ago
if (event.stopPropagation) event.stopPropagation();
if (event.cancelBubble) event.cancelBubble = true;
}
}
}
6 years ago
} // 处理keydown事件
7 years ago
function dispatch(event) {
7 years ago
var asterisk = _handlers['*'];
var key = event.keyCode || event.which || event.charCode; // Collect bound keys
// If an Input Method Editor is processing key input and the event is keydown, return 229.
// https://stackoverflow.com/questions/25043934/is-it-ok-to-ignore-keydown-events-with-keycode-229
// http://lists.w3.org/Archives/Public/www-dom/2010JulSep/att-0182/keyCode-spec.html
7 years ago
if (_downKeys.indexOf(key) === -1 && key !== 229) _downKeys.push(key); // Gecko(Firefox)的command键值224在Webkit(Chrome)中保持一致
7 years ago
// Webkit左右command键值不一样
6 years ago
7 years ago
if (key === 93 || key === 224) key = 91;
if (key in _mods) {
6 years ago
_mods[key] = true; // 将特殊字符的key注册到 hotkeys 上
7 years ago
7 years ago
for (var k in _modifier) {
7 years ago
if (_modifier[k] === key) hotkeys[k] = true;
}
if (!asterisk) return;
6 years ago
} // 将modifierMap里面的修饰键绑定到event中
7 years ago
7 years ago
for (var e in _mods) {
7 years ago
if (Object.prototype.hasOwnProperty.call(_mods, e)) {
_mods[e] = event[modifierMap[e]];
}
6 years ago
} // 表单控件过滤 默认表单控件不触发快捷键
6 years ago
if (!hotkeys.filter.call(this, event)) return; // 获取范围 默认为all
7 years ago
6 years ago
var scope = getScope(); // 对任何快捷键都需要做的处理
7 years ago
if (asterisk) {
7 years ago
for (var i = 0; i < asterisk.length; i++) {
6 years ago
if (asterisk[i].scope === scope && (event.type === 'keydown' && !asterisk[i].keyup || event.type === 'keyup' && asterisk[i].keyup)) {
6 years ago
eventHandler(event, asterisk[i], scope);
}
7 years ago
}
6 years ago
} // key 不在_handlers中返回
7 years ago
if (!(key in _handlers)) return;
7 years ago
for (var _i = 0; _i < _handlers[key].length; _i++) {
6 years ago
if (event.type === 'keydown' && _handlers[key][_i].keydown || event.type === 'keyup' && _handlers[key][_i].keyup) {
6 years ago
if (_handlers[key][_i].key) {
var keyShortcut = _handlers[key][_i].key.split('+');
6 years ago
6 years ago
var _downKeysCurrent = []; // 记录当前按键键值
6 years ago
6 years ago
for (var a = 0; a < keyShortcut.length; a++) {
_downKeysCurrent.push(code(keyShortcut[a]));
}
6 years ago
6 years ago
_downKeysCurrent = _downKeysCurrent.sort();
6 years ago
6 years ago
if (_downKeysCurrent.join('') === _downKeys.sort().join('')) {
6 years ago
// 找到处理内容
eventHandler(event, _handlers[key][_i], scope);
}
}
6 years ago
}
7 years ago
}
6 years ago
} // 判断 element 是否已经绑定事件
function isElementBind(element) {
return elementHasBindEvent.indexOf(element) > -1;
7 years ago
}
7 years ago
function hotkeys(key, option, method) {
7 years ago
var keys = getKeys(key); // 需要处理的快捷键列表
6 years ago
7 years ago
var mods = [];
7 years ago
var scope = 'all'; // scope默认为all所有范围都有效
6 years ago
7 years ago
var element = document; // 快捷键事件绑定节点
6 years ago
7 years ago
var i = 0;
6 years ago
var keyup = false;
6 years ago
var keydown = true; // 对为设定范围的判断
7 years ago
7 years ago
if (method === undefined && typeof option === 'function') {
method = option;
7 years ago
}
if (Object.prototype.toString.call(option) === '[object Object]') {
7 years ago
if (option.scope) scope = option.scope; // eslint-disable-line
6 years ago
6 years ago
if (option.element) element = option.element; // eslint-disable-line
6 years ago
6 years ago
if (option.keyup) keyup = option.keyup; // eslint-disable-line
6 years ago
6 years ago
if (option.keydown) keydown = option.keydown; // eslint-disable-line
7 years ago
}
6 years ago
if (typeof option === 'string') scope = option; // 对于每个快捷键进行处理
7 years ago
7 years ago
for (; i < keys.length; i++) {
key = keys[i].split('+'); // 按键列表
6 years ago
mods = []; // 如果是组合快捷键取得组合快捷键
if (key.length > 1) mods = getMods(_modifier, key); // 将非修饰键转化为键码
7 years ago
key = key[key.length - 1];
key = key === '*' ? '*' : code(key); // *表示匹配所有快捷键
// 判断key是否在_handlers中不在就赋一个空数组
6 years ago
7 years ago
if (!(key in _handlers)) _handlers[key] = [];
6 years ago
7 years ago
_handlers[key].push({
6 years ago
keyup: keyup,
keydown: keydown,
7 years ago
scope: scope,
mods: mods,
7 years ago
shortcut: keys[i],
7 years ago
method: method,
key: keys[i]
7 years ago
});
6 years ago
} // 在全局document上设置快捷键
6 years ago
if (typeof element !== 'undefined' && !isElementBind(element)) {
elementHasBindEvent.push(element);
addEvent(element, 'keydown', function (e) {
dispatch(e);
});
6 years ago
addEvent(element, 'keyup', function (e) {
6 years ago
dispatch(e);
6 years ago
clearModifier(e);
});
7 years ago
}
7 years ago
}
7 years ago
var _api = {
setScope: setScope,
getScope: getScope,
deleteScope: deleteScope,
getPressedKeyCodes: getPressedKeyCodes,
isPressed: isPressed,
filter: filter,
unbind: unbind
7 years ago
};
6 years ago
7 years ago
for (var a in _api) {
7 years ago
if (Object.prototype.hasOwnProperty.call(_api, a)) {
hotkeys[a] = _api[a];
}
}
if (typeof window !== 'undefined') {
7 years ago
var _hotkeys = window.hotkeys;
6 years ago
7 years ago
hotkeys.noConflict = function (deep) {
7 years ago
if (deep && window.hotkeys === hotkeys) {
window.hotkeys = _hotkeys;
}
6 years ago
7 years ago
return hotkeys;
};
6 years ago
7 years ago
window.hotkeys = hotkeys;
}
export default hotkeys;