UNPKG

@blueprintjs/core

Version:

Core styles & components

110 lines 5.55 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; const tslib_1 = require("tslib"); const React = tslib_1.__importStar(require("react")); const errors_1 = require("../../common/errors"); const domUtils_1 = require("../../common/utils/domUtils"); const hotkeyParser_1 = require("../../components/hotkeys/hotkeyParser"); const context_1 = require("../../context"); /** * React hook to register global and local hotkeys for a component. * * @see https://blueprintjs.com/docs/#core/hooks/use-hotkeys * @param keys list of hotkeys to configure * @param options hook options */ function useHotkeys(keys, options = {}) { const { document = getDefaultDocument(), showDialogKeyCombo = "?" } = options; const localKeys = React.useMemo(() => keys .filter(k => !k.global) .map(k => ({ combo: (0, hotkeyParser_1.parseKeyCombo)(k.combo), config: k, })), [keys]); const globalKeys = React.useMemo(() => keys .filter(k => k.global) .map(k => ({ combo: (0, hotkeyParser_1.parseKeyCombo)(k.combo), config: k, })), [keys]); // register keys with global context const [state, dispatch] = React.useContext(context_1.HotkeysContext); React.useEffect(() => { if (!state.hasProvider) { console.warn(errors_1.HOTKEYS_PROVIDER_NOT_FOUND); } }, [state.hasProvider]); // we can still bind the hotkeys if there is no HotkeysProvider, they just won't show up in the dialog React.useEffect(() => { const payload = [...globalKeys.map(k => k.config), ...localKeys.map(k => k.config)]; dispatch({ payload, type: "ADD_HOTKEYS" }); return () => dispatch({ payload, type: "REMOVE_HOTKEYS" }); }, [dispatch, globalKeys, localKeys]); const invokeNamedCallbackIfComboRecognized = React.useCallback((global, combo, callbackName, e) => { var _a, _b; const isTextInput = (0, domUtils_1.elementIsTextInput)(e.target); for (const key of global ? globalKeys : localKeys) { const { allowInInput = false, disabled = false, preventDefault = false, stopPropagation = false, } = key.config; const shouldIgnore = (isTextInput && !allowInInput) || disabled; if (!shouldIgnore && (0, hotkeyParser_1.comboMatches)(key.combo, combo)) { if (preventDefault) { e.preventDefault(); } if (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); } } }, [globalKeys, localKeys]); const handleGlobalKeyDown = React.useCallback((e) => { // special case for global keydown: if '?' is pressed, open the hotkeys dialog const combo = (0, hotkeyParser_1.getKeyCombo)(e); const isTextInput = (0, domUtils_1.elementIsTextInput)(e.target); if (!isTextInput && (0, hotkeyParser_1.comboMatches)((0, hotkeyParser_1.parseKeyCombo)(showDialogKeyCombo), combo)) { dispatch({ type: "OPEN_DIALOG" }); } else { invokeNamedCallbackIfComboRecognized(true, (0, hotkeyParser_1.getKeyCombo)(e), "onKeyDown", e); } }, [dispatch, invokeNamedCallbackIfComboRecognized, showDialogKeyCombo]); const handleGlobalKeyUp = React.useCallback((e) => invokeNamedCallbackIfComboRecognized(true, (0, hotkeyParser_1.getKeyCombo)(e), "onKeyUp", e), [invokeNamedCallbackIfComboRecognized]); const handleLocalKeyDown = React.useCallback((e) => invokeNamedCallbackIfComboRecognized(false, (0, hotkeyParser_1.getKeyCombo)(e.nativeEvent), "onKeyDown", e.nativeEvent), [invokeNamedCallbackIfComboRecognized]); const handleLocalKeyUp = React.useCallback((e) => invokeNamedCallbackIfComboRecognized(false, (0, hotkeyParser_1.getKeyCombo)(e.nativeEvent), "onKeyUp", e.nativeEvent), [invokeNamedCallbackIfComboRecognized]); React.useEffect(() => { // document is guaranteed to be defined inside effects document.addEventListener("keydown", handleGlobalKeyDown); document.addEventListener("keyup", handleGlobalKeyUp); return () => { document.removeEventListener("keydown", handleGlobalKeyDown); document.removeEventListener("keyup", handleGlobalKeyUp); }; }, [document, handleGlobalKeyDown, handleGlobalKeyUp]); return { handleKeyDown: handleLocalKeyDown, handleKeyUp: handleLocalKeyUp }; } exports.useHotkeys = useHotkeys; function getDefaultDocument() { if (typeof window === "undefined") { return undefined; } return window.document; } //# sourceMappingURL=useHotkeys.js.map