mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-21 01:41:46 +03:00
3780e80b14
closes https://github.com/tryghost/ghost/issues/12294 refs https://github.com/TryGhost/Admin/pull/1707 - cache registered shortcuts and check KeyboardEvent before dispatching event to the root
42 lines
747 B
JavaScript
42 lines
747 B
JavaScript
const cache = {};
|
|
|
|
export function includes(event) {
|
|
const keys = [];
|
|
let ctrlPressed = false;
|
|
|
|
if (event.ctrlKey) {
|
|
keys.push('ctrl');
|
|
ctrlPressed = true;
|
|
}
|
|
|
|
if (event.shiftKey) {
|
|
keys.push('shift');
|
|
}
|
|
|
|
if (event.altKey) {
|
|
keys.push('alt');
|
|
}
|
|
|
|
keys.push(event.key);
|
|
|
|
const exists = cache[keys.join('+')];
|
|
|
|
if (!exists && ctrlPressed) { // Test things like cmd+s
|
|
return cache[keys.join('+').replace('ctrl', 'cmd')];
|
|
}
|
|
|
|
return exists;
|
|
}
|
|
|
|
export function register(shortcut) {
|
|
cache[shortcut.toLowerCase()] = true;
|
|
}
|
|
|
|
export function unregister(shortcut) {
|
|
delete cache[shortcut];
|
|
}
|
|
|
|
export function getAll() {
|
|
return Object.assign({}, cache);
|
|
}
|