2025-07-18 07:37:12 +06:00
|
|
|
import { useEffect } from "react";
|
2025-07-18 08:05:02 +06:00
|
|
|
import { invokeAction } from "../constants/actions";
|
2025-07-18 07:37:12 +06:00
|
|
|
import { useKeybindingsStore } from "@/stores/keybindings-store";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A composable that hooks to the caller component's
|
|
|
|
|
* lifecycle and hooks to the keyboard events to fire
|
|
|
|
|
* the appropriate actions based on keybindings
|
|
|
|
|
*/
|
|
|
|
|
export function useKeybindingsListener() {
|
2025-07-26 12:54:50 +02:00
|
|
|
const { keybindings, getKeybindingString, keybindingsEnabled, isRecording } =
|
2025-07-18 08:05:02 +06:00
|
|
|
useKeybindingsStore();
|
2025-07-18 07:37:12 +06:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const handleKeyDown = (ev: KeyboardEvent) => {
|
|
|
|
|
// Do not check keybinds if the mode is disabled
|
|
|
|
|
if (!keybindingsEnabled) return;
|
2025-07-26 12:54:50 +02:00
|
|
|
// ignore key events if user is changing keybindings
|
|
|
|
|
if (isRecording) return;
|
2025-07-18 07:37:12 +06:00
|
|
|
|
|
|
|
|
const binding = getKeybindingString(ev);
|
|
|
|
|
if (!binding) return;
|
|
|
|
|
|
|
|
|
|
const boundAction = keybindings[binding];
|
|
|
|
|
if (!boundAction) return;
|
|
|
|
|
|
|
|
|
|
ev.preventDefault();
|
|
|
|
|
|
|
|
|
|
// Handle actions with default arguments
|
2025-07-24 14:41:34 -07:00
|
|
|
let actionArgs: any;
|
2025-07-18 07:37:12 +06:00
|
|
|
|
|
|
|
|
if (boundAction === "seek-forward") {
|
|
|
|
|
actionArgs = { seconds: 1 };
|
|
|
|
|
} else if (boundAction === "seek-backward") {
|
|
|
|
|
actionArgs = { seconds: 1 };
|
|
|
|
|
} else if (boundAction === "jump-forward") {
|
|
|
|
|
actionArgs = { seconds: 5 };
|
|
|
|
|
} else if (boundAction === "jump-backward") {
|
|
|
|
|
actionArgs = { seconds: 5 };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
invokeAction(boundAction, actionArgs, "keypress");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
document.addEventListener("keydown", handleKeyDown);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
document.removeEventListener("keydown", handleKeyDown);
|
|
|
|
|
};
|
2025-07-26 12:54:50 +02:00
|
|
|
}, [keybindings, getKeybindingString, keybindingsEnabled, isRecording]);
|
2025-07-18 07:37:12 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This composable allows for the UI component to be disabled if the component in question is mounted
|
|
|
|
|
*/
|
|
|
|
|
export function useKeybindingDisabler() {
|
2025-07-18 08:05:02 +06:00
|
|
|
const { disableKeybindings, enableKeybindings } = useKeybindingsStore();
|
2025-07-18 07:37:12 +06:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
disableKeybindings,
|
|
|
|
|
enableKeybindings,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Export the bindings for backward compatibility
|
|
|
|
|
export const bindings = {};
|