summaryrefslogtreecommitdiff
path: root/apps/web/src/hooks/use-keybindings.ts
diff options
context:
space:
mode:
authorMaze Winther <mazewinther@gmail.com>2025-07-18 11:37:48 +0200
committerMaze Winther <mazewinther@gmail.com>2025-07-18 11:37:48 +0200
commit74e7d427aaed3517ce578c2192f783dacffc02fc (patch)
treec6ec27070940778e64ca3ad50cd937227f87f3ac /apps/web/src/hooks/use-keybindings.ts
parentc4e6a9a45ab0f0afdde4f0c9991e39cd5d6205d0 (diff)
parent23001f8442e60999e34c0d6c348fecf0867b6cad (diff)
downloadopencut-74e7d427aaed3517ce578c2192f783dacffc02fc.tar.gz
Merge branch 'feat/shortcut-actions' into staging
Diffstat (limited to 'apps/web/src/hooks/use-keybindings.ts')
-rw-r--r--apps/web/src/hooks/use-keybindings.ts64
1 files changed, 64 insertions, 0 deletions
diff --git a/apps/web/src/hooks/use-keybindings.ts b/apps/web/src/hooks/use-keybindings.ts
new file mode 100644
index 00000000..7887f3f1
--- /dev/null
+++ b/apps/web/src/hooks/use-keybindings.ts
@@ -0,0 +1,64 @@
+import { useEffect } from "react";
+import { invokeAction } from "../constants/actions";
+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() {
+ const { keybindings, getKeybindingString, keybindingsEnabled } =
+ useKeybindingsStore();
+
+ useEffect(() => {
+ const handleKeyDown = (ev: KeyboardEvent) => {
+ // Do not check keybinds if the mode is disabled
+ if (!keybindingsEnabled) return;
+
+ const binding = getKeybindingString(ev);
+ if (!binding) return;
+
+ const boundAction = keybindings[binding];
+ if (!boundAction) return;
+
+ ev.preventDefault();
+
+ // Handle actions with default arguments
+ let actionArgs: any = undefined;
+
+ 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);
+ };
+ }, [keybindings, getKeybindingString, keybindingsEnabled]);
+}
+
+/**
+ * This composable allows for the UI component to be disabled if the component in question is mounted
+ */
+export function useKeybindingDisabler() {
+ const { disableKeybindings, enableKeybindings } = useKeybindingsStore();
+
+ return {
+ disableKeybindings,
+ enableKeybindings,
+ };
+}
+
+// Export the bindings for backward compatibility
+export const bindings = {};