UNPKG

@nextcloud/vue

Version:
55 lines (54 loc) 1.68 kB
var _a, _b, _c; import { onKeyStroke } from "@vueuse/core"; const disableKeyboardShortcuts = (_c = (_b = (_a = window.OCP) == null ? void 0 : _a.Accessibility) == null ? void 0 : _b.disableKeyboardShortcuts) == null ? void 0 : _c.call(_b); const isMac = /mac|ipad|iphone|darwin/i.test(navigator.userAgent); function shouldIgnoreEvent(event) { var _a2; if (event.target instanceof HTMLInputElement || event.target instanceof HTMLTextAreaElement || event.target instanceof HTMLSelectElement || ((_a2 = event.target) == null ? void 0 : _a2.isContentEditable)) { return true; } return document.getElementsByClassName("modal-mask").length !== 0; } const eventHandler = (callback, options) => (event) => { const ctrlKeyPressed = isMac ? event.metaKey : event.ctrlKey; if (ctrlKeyPressed !== Boolean(options.ctrl)) { return; } else if (!!options.alt !== event.altKey) { return; } else if (!!options.shift !== event.shiftKey) { return; } else if (shouldIgnoreEvent(event)) { return; } if (options.prevent) { event.preventDefault(); } if (options.stop) { event.stopPropagation(); } callback(event); }; function useHotKey(key, callback = () => { }, options = {}) { if (disableKeyboardShortcuts) { return () => { }; } const stopKeyDown = onKeyStroke(key, eventHandler(callback, options), { eventName: "keydown", dedupe: true, passive: !options.prevent }); const stopKeyUp = options.push ? onKeyStroke(key, eventHandler(callback, options), { eventName: "keyup", passive: !options.prevent }) : () => { }; return () => { stopKeyDown(); stopKeyUp(); }; } export { useHotKey };