UNPKG

@blueprintjs/core

Version:
130 lines 5.87 kB
"use strict"; /* * Copyright 2021 Palantir Technologies, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.useHotkeys = void 0; var tslib_1 = require("tslib"); var React = tslib_1.__importStar(require("react")); var hotkeyParser_1 = require("../../components/hotkeys/hotkeyParser"); var context_1 = require("../../context"); /** * React hook to register global and local hotkeys for a component. * * @param keys list of hotkeys to configure * @param options hook options */ function useHotkeys(keys, options) { if (options === void 0) { options = {}; } var _a = options.showDialogKeyCombo, showDialogKeyCombo = _a === void 0 ? "?" : _a; var localKeys = React.useMemo(function () { return keys .filter(function (k) { return !k.global; }) .map(function (k) { return ({ combo: hotkeyParser_1.parseKeyCombo(k.combo), config: k, }); }); }, [keys]); var globalKeys = React.useMemo(function () { return keys .filter(function (k) { return k.global; }) .map(function (k) { return ({ combo: hotkeyParser_1.parseKeyCombo(k.combo), config: k, }); }); }, [keys]); // register keys with global context var _b = React.useContext(context_1.HotkeysContext), dispatch = _b[1]; React.useEffect(function () { var payload = tslib_1.__spreadArrays(globalKeys.map(function (k) { return k.config; }), localKeys.map(function (k) { return k.config; })); dispatch({ type: "ADD_HOTKEYS", payload: payload }); return function () { return dispatch({ type: "REMOVE_HOTKEYS", payload: payload }); }; }, [keys]); var invokeNamedCallbackIfComboRecognized = function (global, combo, callbackName, e) { var _a, _b; var isTextInput = isTargetATextInput(e); for (var _i = 0, _c = global ? globalKeys : localKeys; _i < _c.length; _i++) { var key = _c[_i]; var shouldIgnore = (isTextInput && !key.config.allowInInput) || key.config.disabled; if (!shouldIgnore && hotkeyParser_1.comboMatches(key.combo, combo)) { if (key.config.preventDefault) { e.preventDefault(); } if (key.config.stopPropagation) { // set a flag just for unit testing. not meant to be referenced in feature work. e.isPropagationStopped = true; e.stopPropagation(); } (_b = (_a = key.config)[callbackName]) === null || _b === void 0 ? void 0 : _b.call(_a, e); } } }; var handleGlobalKeyDown = React.useCallback(function (e) { // special case for global keydown: if '?' is pressed, open the hotkeys dialog var combo = hotkeyParser_1.getKeyCombo(e); var isTextInput = isTargetATextInput(e); if (!isTextInput && hotkeyParser_1.comboMatches(hotkeyParser_1.parseKeyCombo(showDialogKeyCombo), combo)) { dispatch({ type: "OPEN_DIALOG" }); } else { invokeNamedCallbackIfComboRecognized(true, hotkeyParser_1.getKeyCombo(e), "onKeyDown", e); } }, [globalKeys]); var handleGlobalKeyUp = React.useCallback(function (e) { return invokeNamedCallbackIfComboRecognized(true, hotkeyParser_1.getKeyCombo(e), "onKeyUp", e); }, [globalKeys]); var handleLocalKeyDown = React.useCallback(function (e) { return invokeNamedCallbackIfComboRecognized(false, hotkeyParser_1.getKeyCombo(e.nativeEvent), "onKeyDown", e.nativeEvent); }, [localKeys]); var handleLocalKeyUp = React.useCallback(function (e) { return invokeNamedCallbackIfComboRecognized(false, hotkeyParser_1.getKeyCombo(e.nativeEvent), "onKeyUp", e.nativeEvent); }, [localKeys]); React.useEffect(function () { document.addEventListener("keydown", handleGlobalKeyDown); document.addEventListener("keyup", handleGlobalKeyUp); return function () { document.removeEventListener("keydown", handleGlobalKeyDown); document.removeEventListener("keyup", handleGlobalKeyUp); }; }, [handleGlobalKeyDown, handleGlobalKeyUp]); return { handleKeyDown: handleLocalKeyDown, handleKeyUp: handleLocalKeyUp }; } exports.useHotkeys = useHotkeys; /** * @returns true if the event target is a text input which should take priority over hotkey bindings */ function isTargetATextInput(e) { var elem = e.target; // we check these cases for unit testing, but this should not happen // during normal operation if (elem == null || elem.closest == null) { return false; } var editable = elem.closest("input, textarea, [contenteditable=true]"); if (editable == null) { return false; } // don't let checkboxes, switches, and radio buttons prevent hotkey behavior if (editable.tagName.toLowerCase() === "input") { var inputType = editable.type; if (inputType === "checkbox" || inputType === "radio") { return false; } } // don't let read-only fields prevent hotkey behavior if (editable.readOnly) { return false; } return true; } //# sourceMappingURL=useHotkeys.js.map