The open-source CapCut alternative
fix: prevent action to perform on key press when edit keybindings
Kha Nguyen901ddb509db2
diff --git a/apps/web/src/components/keyboard-shortcuts-help.tsx b/apps/web/src/components/keyboard-shortcuts-help.tsxindex 4c8bfff5..65f879a3 100644--- a/apps/web/src/components/keyboard-shortcuts-help.tsx+++ b/apps/web/src/components/keyboard-shortcuts-help.tsx@@ -144,6 +144,7 @@ export const KeyboardShortcutsHelp = () => {getKeybindingString,validateKeybinding,getKeybindingsForAction,+ setIsRecording,} = useKeybindingsStore();// Get shortcuts from centralized hook@@ -163,11 +164,11 @@ export const KeyboardShortcutsHelp = () => {// Auto-save the new keybindingconst conflict = validateKeybinding(keyString,- recordingShortcut.action+ recordingShortcut.action,);if (conflict) {toast.error(- `Key "${keyString}" is already bound to "${conflict.existingAction}"`+ `Key "${keyString}" is already bound to "${conflict.existingAction}"`,);setRecordingKey(null);setRecordingShortcut(null);@@ -181,14 +182,16 @@ export const KeyboardShortcutsHelp = () => {// Add new keybindingupdateKeybinding(keyString, recordingShortcut.action);+ setIsRecording(false);setRecordingKey(null);setRecordingShortcut(null);}};- const handleClickOutside = (e: MouseEvent) => {+ const handleClickOutside = () => {setRecordingKey(null);setRecordingShortcut(null);+ setIsRecording(false);};document.addEventListener("keydown", handleKeyDown);@@ -211,6 +214,7 @@ export const KeyboardShortcutsHelp = () => {const handleStartRecording = (keyId: string, shortcut: KeyboardShortcut) => {setRecordingKey(keyId);setRecordingShortcut(shortcut);+ setIsRecording(true);};return (diff --git a/apps/web/src/hooks/use-keybindings.ts b/apps/web/src/hooks/use-keybindings.tsindex 7887f3f1..0e2045ac 100644--- a/apps/web/src/hooks/use-keybindings.ts+++ b/apps/web/src/hooks/use-keybindings.ts@@ -8,13 +8,15 @@ import { useKeybindingsStore } from "@/stores/keybindings-store";* the appropriate actions based on keybindings*/export function useKeybindingsListener() {- const { keybindings, getKeybindingString, keybindingsEnabled } =+ const { keybindings, getKeybindingString, keybindingsEnabled, isRecording } =useKeybindingsStore();useEffect(() => {const handleKeyDown = (ev: KeyboardEvent) => {// Do not check keybinds if the mode is disabledif (!keybindingsEnabled) return;+ // ignore key events if user is changing keybindings+ if (isRecording) return;const binding = getKeybindingString(ev);if (!binding) return;@@ -45,7 +47,7 @@ export function useKeybindingsListener() {return () => {document.removeEventListener("keydown", handleKeyDown);};- }, [keybindings, getKeybindingString, keybindingsEnabled]);+ }, [keybindings, getKeybindingString, keybindingsEnabled, isRecording]);}/**diff --git a/apps/web/src/stores/keybindings-store.ts b/apps/web/src/stores/keybindings-store.tsindex 3e525b86..ce2d839d 100644--- a/apps/web/src/stores/keybindings-store.ts+++ b/apps/web/src/stores/keybindings-store.ts@@ -39,6 +39,7 @@ interface KeybindingsState {keybindings: KeybindingConfig;isCustomized: boolean;keybindingsEnabled: boolean;+ isRecording: boolean;// ActionsupdateKeybinding: (key: ShortcutKey, action: ActionWithOptionalArgs) => void;@@ -48,11 +49,12 @@ interface KeybindingsState {exportKeybindings: () => KeybindingConfig;enableKeybindings: () => void;disableKeybindings: () => void;+ setIsRecording: (isRecording: boolean) => void;// ValidationvalidateKeybinding: (key: ShortcutKey,- action: ActionWithOptionalArgs+ action: ActionWithOptionalArgs,) => KeybindingConflict | null;getKeybindingsForAction: (action: ActionWithOptionalArgs) => ShortcutKey[];@@ -66,6 +68,7 @@ export const useKeybindingsStore = create<KeybindingsState>()(keybindings: { ...defaultKeybindings },isCustomized: false,keybindingsEnabled: true,+ isRecording: false,updateKeybinding: (key: ShortcutKey, action: ActionWithOptionalArgs) => {set((state) => {@@ -126,7 +129,7 @@ export const useKeybindingsStore = create<KeybindingsState>()(validateKeybinding: (key: ShortcutKey,- action: ActionWithOptionalArgs+ action: ActionWithOptionalArgs,) => {const { keybindings } = get();const existingAction = keybindings[key];@@ -141,11 +144,14 @@ export const useKeybindingsStore = create<KeybindingsState>()(return null;},+ setIsRecording: (isRecording: boolean) => {+ set({ isRecording });+ },getKeybindingsForAction: (action: ActionWithOptionalArgs) => {const { keybindings } = get();return Object.keys(keybindings).filter(- (key) => keybindings[key as ShortcutKey] === action+ (key) => keybindings[key as ShortcutKey] === action,) as ShortcutKey[];},@@ -156,8 +162,8 @@ export const useKeybindingsStore = create<KeybindingsState>()({name: "opencut-keybindings",version: 1,- }- )+ },+ ),);// Utility functions