rich-text-editor
Version:
Rich text editor
22 lines (21 loc) • 916 B
JavaScript
;
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;