mirror of
https://github.com/OpenCut-app/OpenCut.git
synced 2026-07-13 21:52:53 +02:00
feat: enhance keybinding import validation and improve error handling
This commit is contained in:
@@ -224,10 +224,21 @@ export const KeybindingEditor = ({
|
|||||||
reader.onload = (e) => {
|
reader.onload = (e) => {
|
||||||
try {
|
try {
|
||||||
const config = JSON.parse(e.target?.result as string);
|
const config = JSON.parse(e.target?.result as string);
|
||||||
|
// Validate config structure
|
||||||
|
if (!config || typeof config !== "object") {
|
||||||
|
throw new Error("Invalid configuration format");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate each keybinding
|
||||||
|
for (const [key, action] of Object.entries(config)) {
|
||||||
|
if (typeof key !== "string" || typeof action !== "string") {
|
||||||
|
throw new Error(`Invalid keybinding: ${key} -> ${action}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
importKeybindings(config);
|
importKeybindings(config);
|
||||||
toast.success("Keybindings imported successfully");
|
toast.success("Keybindings imported successfully");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
toast.error("Failed to import keybindings file");
|
toast.error(`Failed to import keybindings: ${error}`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
reader.readAsText(file);
|
reader.readAsText(file);
|
||||||
|
|||||||
@@ -233,19 +233,19 @@ export function useActionHandler<A extends Action>(
|
|||||||
// Handle ref-based isActive changes
|
// Handle ref-based isActive changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isActive && typeof isActive === "object" && "current" in isActive) {
|
if (isActive && typeof isActive === "object" && "current" in isActive) {
|
||||||
const checkActive = () => {
|
// Poll for ref changes
|
||||||
|
const interval = setInterval(() => {
|
||||||
const shouldBind = isActive.current;
|
const shouldBind = isActive.current;
|
||||||
if (shouldBind && !isBound) {
|
if (shouldBind !== isBound) {
|
||||||
bindAction(action, stableHandler);
|
if (shouldBind) {
|
||||||
setIsBound(true);
|
bindAction(action, stableHandler);
|
||||||
} else if (!shouldBind && isBound) {
|
} else {
|
||||||
unbindAction(action, stableHandler);
|
unbindAction(action, stableHandler);
|
||||||
setIsBound(false);
|
}
|
||||||
|
setIsBound(shouldBind);
|
||||||
}
|
}
|
||||||
};
|
}, 100);
|
||||||
|
return () => clearInterval(interval);
|
||||||
// Initial check
|
|
||||||
checkActive();
|
|
||||||
}
|
}
|
||||||
}, [action, stableHandler, isActive, isBound]);
|
}, [action, stableHandler, isActive, isBound]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -112,6 +112,13 @@ export const useKeybindingsStore = create<KeybindingsState>()(
|
|||||||
},
|
},
|
||||||
|
|
||||||
importKeybindings: (config: KeybindingConfig) => {
|
importKeybindings: (config: KeybindingConfig) => {
|
||||||
|
// Validate all keys and actions
|
||||||
|
for (const [key, action] of Object.entries(config)) {
|
||||||
|
// Validate the key format
|
||||||
|
if (typeof key !== "string" || key.length === 0) {
|
||||||
|
throw new Error(`Invalid key format: ${key}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
set({
|
set({
|
||||||
keybindings: { ...config },
|
keybindings: { ...config },
|
||||||
isCustomized: true,
|
isCustomized: true,
|
||||||
@@ -198,6 +205,7 @@ function getPressedKey(ev: KeyboardEvent): string | null {
|
|||||||
|
|
||||||
// Check for special keys
|
// Check for special keys
|
||||||
if (key === "tab") return "tab";
|
if (key === "tab") return "tab";
|
||||||
|
if (key === " " || key === "space") return "space";
|
||||||
if (key === "home") return "home";
|
if (key === "home") return "home";
|
||||||
if (key === "end") return "end";
|
if (key === "end") return "end";
|
||||||
if (key === "delete") return "delete";
|
if (key === "delete") return "delete";
|
||||||
|
|||||||
Reference in New Issue
Block a user