UNPKG

rich-text-editor

Version:
22 lines (21 loc) 916 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useKeyboardEventListener = void 0; const react_1 = require("react"); const useKeyboardEventListener = (shortcuts) => { (0, react_1.useEffect)(() => { const handleKeyDown = (event) => { const shortcut = shortcuts.find(({ keyMatch }) => keyMatch(event)); if (shortcut) { event.preventDefault(); event.stopPropagation(); // Prevent browser's native handling of the shortcut, as it would cause strange behaviour especially when mixed with our own implementation shortcut.fn(event); } }; window.addEventListener('keydown', handleKeyDown); return () => { window.removeEventListener('keydown', handleKeyDown); }; }, [shortcuts]); }; exports.useKeyboardEventListener = useKeyboardEventListener;