shortcutter
Version:
Tiny, dependency-free library to manage keyboard shortcuts in your application.
92 lines (91 loc) • 3.94 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import { useContexts } from './contexts';
import { useController } from './controller';
import { normalizeKeyCode } from './helpers';
var DEFAULT_CONFIG = {
eventTarget: window,
defaultContext: 'default',
};
export function useShortcutter(customConfig) {
if (customConfig === void 0) { customConfig = {}; }
var CONFIG = __assign(__assign({}, DEFAULT_CONFIG), customConfig);
var eventTarget = CONFIG.eventTarget, defaultContext = CONFIG.defaultContext;
var contexts = useContexts();
var keysRecorder = useController();
contexts.add(defaultContext);
contexts.setActive(defaultContext);
eventTarget.addEventListener('keydown', onkeydown);
eventTarget.addEventListener('keyup', onkeyup);
eventTarget.addEventListener('blur', onblur);
eventTarget.addEventListener('unload', onunload);
var invokeClbck = function (keys, event, phase) {
contexts.getActive().forEach(function (context) {
var ctx = contexts.get(context);
if (ctx === null || ctx === void 0 ? void 0 : ctx.has(keys, phase)) {
ctx.get(keys, phase)(event, phase);
}
});
};
function onkeydown(event) {
var previousCombination = keysRecorder.getPressed().join('+');
keysRecorder.press(normalizeKeyCode(event.code));
var nextCombination = keysRecorder.getPressed().join('+');
if (previousCombination !== nextCombination) {
if (previousCombination.length) {
invokeClbck(previousCombination.split('+'), event, "up");
}
invokeClbck(nextCombination.split('+'), event, "down");
}
else {
invokeClbck(previousCombination.split('+'), event, "press");
}
}
function onkeyup(event) {
var previousCombination = keysRecorder.getPressed().join('+');
keysRecorder.release(normalizeKeyCode(event.code));
var nextCombination = keysRecorder.getPressed().join('+');
if (previousCombination.length) {
invokeClbck(previousCombination.split('+'), event, "up");
}
if (nextCombination.length) {
invokeClbck(nextCombination.split('+'), event, "down");
}
}
function onblur(event) {
var previousCombination = keysRecorder.getPressed().join('+');
if (previousCombination.length) {
invokeClbck(previousCombination.split('+'), event, "up");
}
keysRecorder.releaseAll();
}
function onunload() {
eventTarget.removeEventListener('keydown', onkeydown);
eventTarget.removeEventListener('keyup', onkeyup);
eventTarget.removeEventListener('blur', onblur);
eventTarget.removeEventListener('unload', onunload);
}
return {
listen: function (context, keys, callback, phases) {
var ctx = contexts.has(context) ? contexts.get(context) : contexts.add(context);
ctx === null || ctx === void 0 ? void 0 : ctx.add(keys, callback, phases);
return function () { return ctx === null || ctx === void 0 ? void 0 : ctx.remove(keys, phases); };
},
unlisten: function (context, keys, phases) {
var ctx = contexts.get(context);
ctx === null || ctx === void 0 ? void 0 : ctx.remove(keys, phases);
},
hasContext: function (name) { return contexts.has(name); },
getActiveContext: function () { return contexts.getActive(); },
setActiveContext: function (name) { return contexts.setActive(name); },
};
}