76 lines
3.0 KiB
JavaScript
76 lines
3.0 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.useKeybinding = exports.areKeyboardShortcutsDisabled = void 0;
|
|
const react_1 = require("react");
|
|
const keybindings_1 = require("../state/keybindings");
|
|
const z_index_1 = require("../state/z-index");
|
|
if (!process.env.KEYBOARD_SHORTCUTS_ENABLED) {
|
|
// eslint-disable-next-line no-console
|
|
console.warn('Keyboard shortcuts disabled either due to: a) --disable-keyboard-shortcuts being passed b) Config.setKeyboardShortcutsEnabled(false) being set or c) a Remotion version mismatch.');
|
|
}
|
|
const areKeyboardShortcutsDisabled = () => {
|
|
return !process.env.KEYBOARD_SHORTCUTS_ENABLED;
|
|
};
|
|
exports.areKeyboardShortcutsDisabled = areKeyboardShortcutsDisabled;
|
|
const useKeybinding = () => {
|
|
const [paneId] = (0, react_1.useState)(() => String(Math.random()));
|
|
const context = (0, react_1.useContext)(keybindings_1.KeybindingContext);
|
|
const { isHighestContext } = (0, z_index_1.useZIndex)();
|
|
const registerKeybinding = (0, react_1.useCallback)((options) => {
|
|
if (!process.env.KEYBOARD_SHORTCUTS_ENABLED) {
|
|
return {
|
|
unregister: () => undefined,
|
|
};
|
|
}
|
|
if (!isHighestContext && !options.keepRegisteredWhenNotHighestContext) {
|
|
return {
|
|
unregister: () => undefined,
|
|
};
|
|
}
|
|
const listener = (e) => {
|
|
const commandKey = window.navigator.platform.startsWith('Mac')
|
|
? e.metaKey
|
|
: e.ctrlKey;
|
|
// Apparently, e.key can be undefined in Edge:
|
|
// https://github.com/remotion-dev/remotion/issues/5637
|
|
if (!e.key) {
|
|
return;
|
|
}
|
|
if (e.key.toLowerCase() === options.key.toLowerCase() &&
|
|
options.commandCtrlKey === commandKey) {
|
|
if (!options.triggerIfInputFieldFocused) {
|
|
const { activeElement } = document;
|
|
if (activeElement instanceof HTMLInputElement) {
|
|
return;
|
|
}
|
|
if (activeElement instanceof HTMLTextAreaElement) {
|
|
return;
|
|
}
|
|
}
|
|
options.callback(e);
|
|
if (options.preventDefault) {
|
|
e.preventDefault();
|
|
}
|
|
}
|
|
};
|
|
const toRegister = {
|
|
registeredFromPane: paneId,
|
|
event: options.event,
|
|
key: options.key,
|
|
callback: listener,
|
|
id: String(Math.random()),
|
|
};
|
|
context.registerKeybinding(toRegister);
|
|
return {
|
|
unregister: () => context.unregisterKeybinding(toRegister),
|
|
};
|
|
}, [context, isHighestContext, paneId]);
|
|
(0, react_1.useEffect)(() => {
|
|
return () => {
|
|
context.unregisterPane(paneId);
|
|
};
|
|
}, [context, paneId]);
|
|
return (0, react_1.useMemo)(() => ({ registerKeybinding, isHighestContext }), [registerKeybinding, isHighestContext]);
|
|
};
|
|
exports.useKeybinding = useKeybinding;
|