mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-09-01 13:39:22 +03:00
parent
5e7f531c7f
commit
3a48cc8b7f
3 changed files with 60 additions and 58 deletions
|
|
@ -5,61 +5,79 @@
|
|||
const
|
||||
doc = document,
|
||||
meta = /Mac OS X/.test(navigator.userAgent) ? 'meta' : 'ctrl',
|
||||
actions = {},
|
||||
toArray = v => Array.isArray(v) ? v : [v];
|
||||
let
|
||||
_scope = 'all';
|
||||
_scopes = {},
|
||||
toArray = v => Array.isArray(v) ? v : [v],
|
||||
|
||||
doc.addEventListener('keydown', event => {
|
||||
const key = (event.key||event.code||'').toLowerCase();
|
||||
if (actions[key] && win.shortcuts.filter(event)) {
|
||||
fire = (actions, event) => {
|
||||
let modifiers = [];
|
||||
event.altKey && modifiers.push('alt');
|
||||
event.ctrlKey && modifiers.push('ctrl');
|
||||
event.metaKey && modifiers.push('meta');
|
||||
event.shiftKey && modifiers.push('shift');
|
||||
modifiers = modifiers.join('+');
|
||||
if (actions[key][modifiers]) {
|
||||
if (actions[modifiers]) {
|
||||
// for each potential shortcut
|
||||
actions[key][modifiers].forEach(cmd => {
|
||||
// see if it's in the current scope
|
||||
if (cmd.scope.includes(_scope) || cmd.scope == 'all') {
|
||||
try {
|
||||
// call the handler and stop the event if neccessary
|
||||
if (cmd.method(event) === false) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
actions[modifiers].forEach(cmd => {
|
||||
try {
|
||||
// call the handler and stop the event if neccessary
|
||||
if (!event.defaultPrevented && cmd(event) === false) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
keydown = event => {
|
||||
const key = (event.key||event.code||'').toLowerCase();
|
||||
if (scope[key] && win.shortcuts.filter(event)) {
|
||||
fire(scope[key], event);
|
||||
}
|
||||
if (!event.defaultPrevented && _scope !== 'all' && _scopes.all[key] && win.shortcuts.filter(event)) {
|
||||
fire(_scopes.all[key], event);
|
||||
}
|
||||
};
|
||||
|
||||
let
|
||||
scope = {},
|
||||
_scope = 'all';
|
||||
|
||||
doc.addEventListener('keydown', keydown);
|
||||
|
||||
win.shortcuts = {
|
||||
add: (keys, modifiers, scope, method) => {
|
||||
on: () => doc.addEventListener('keydown', keydown),
|
||||
off: () => doc.removeEventListener('keydown', keydown),
|
||||
add: (keys, modifiers, scopes, method) => {
|
||||
if (method === undefined) {
|
||||
method = scope;
|
||||
scope = 'all';
|
||||
method = scopes;
|
||||
scopes = 'all';
|
||||
}
|
||||
toArray(keys).forEach(key => {
|
||||
key = key.toLowerCase();
|
||||
if (!actions[key]) {
|
||||
actions[key] = {};
|
||||
toArray(scopes).forEach(scope => {
|
||||
if (!_scopes[scope]) {
|
||||
_scopes[scope] = {};
|
||||
}
|
||||
modifiers = toArray(modifiers)
|
||||
.map(key => 'meta' == key ? meta : key)
|
||||
.unique().sort().join('+');
|
||||
if (!actions[key][modifiers]) {
|
||||
actions[key][modifiers] = [];
|
||||
}
|
||||
actions[key][modifiers].push({scope:toArray(scope), method:method});
|
||||
toArray(keys).forEach(key => {
|
||||
key = key.toLowerCase();
|
||||
if (!_scopes[scope][key]) {
|
||||
_scopes[scope][key] = {};
|
||||
}
|
||||
modifiers = toArray(modifiers)
|
||||
.map(key => 'meta' == key ? meta : key)
|
||||
.unique().sort().join('+');
|
||||
if (!_scopes[scope][key][modifiers]) {
|
||||
_scopes[scope][key][modifiers] = [];
|
||||
}
|
||||
_scopes[scope][key][modifiers].push(method);
|
||||
});
|
||||
});
|
||||
},
|
||||
setScope: scope => _scope = scope || 'all',
|
||||
setScope: value => {
|
||||
_scope = value || 'all';
|
||||
scope = _scopes[_scope] || {};
|
||||
},
|
||||
getScope: () => _scope,
|
||||
getMetaKey: () => meta,
|
||||
// ignore keydown in any element that supports keyboard input
|
||||
|
|
@ -67,4 +85,6 @@ win.shortcuts = {
|
|||
&& (event.target.matches('input,select,textarea') || event.target.closest('[contenteditable]')))
|
||||
};
|
||||
|
||||
win.shortcuts.on();
|
||||
|
||||
})(this);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue