Files
OpenCut/apps/web/src/components/keyboard-shortcuts-help.tsx
T

173 lines
5.1 KiB
TypeScript
Raw Normal View History

"use client";
import { useKeyboardShortcuts } from "@/hooks/use-keyboard-shortcuts";
import { useState } from "react";
import { Button } from "./ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "./ui/dialog";
import { getPlatformSpecialKey } from "@/lib/utils";
import { Badge } from "./ui/badge";
import { Keyboard, Settings } from "lucide-react";
import {
useKeyboardShortcutsHelp,
KeyboardShortcut,
} from "@/hooks/use-keyboard-shortcuts-help";
import { KeybindingEditor } from "./keybinding-editor";
const modifier: {
[key: string]: string;
} = {
Shift: "Shift",
Alt: "Alt",
ArrowLeft: "←",
ArrowRight: "→",
ArrowUp: "↑",
ArrowDown: "↓",
Space: "Space",
};
function getKeyWithModifier(key: string) {
if (key === "Ctrl") return getPlatformSpecialKey();
return modifier[key] || key;
}
const ShortcutItem = ({ shortcut }: { shortcut: KeyboardShortcut }) => {
2025-07-16 16:05:30 +02:00
// Filter out lowercase duplicates for display - if both "j" and "J" exist, only show "J"
const displayKeys = shortcut.keys.filter((key: string) => {
if (
key.includes("Cmd") &&
shortcut.keys.includes(key.replace("Cmd", "Ctrl"))
)
2025-07-16 16:05:30 +02:00
return false;
return true;
});
return (
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
{shortcut.icon && (
<div className="text-muted-foreground">{shortcut.icon}</div>
)}
<span className="text-sm">{shortcut.description}</span>
</div>
<div className="flex items-center gap-1">
{displayKeys.map((key: string, index: number) => (
<div key={index} className="flex items-center gap-1">
<div className="flex items-center">
{key.split("+").map((keyPart: string, partIndex: number) => (
2025-07-18 11:26:49 +02:00
<ShortcutKey key={partIndex}>
{getKeyWithModifier(keyPart)}
2025-07-18 11:26:49 +02:00
</ShortcutKey>
2025-07-16 16:05:30 +02:00
))}
</div>
{index < displayKeys.length - 1 && (
<span className="text-xs text-muted-foreground">or</span>
)}
</div>
))}
</div>
</div>
2025-07-16 16:05:30 +02:00
);
};
export const KeyboardShortcutsHelp = () => {
const [open, setOpen] = useState(false);
const [showEditor, setShowEditor] = useState(false);
// Get shortcuts from centralized hook
const { shortcuts } = useKeyboardShortcutsHelp();
2025-07-16 14:37:08 +02:00
const categories = Array.from(new Set(shortcuts.map((s) => s.category)));
if (showEditor) {
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button variant="text" size="sm" className="gap-2">
<Keyboard className="w-4 h-4" />
Shortcuts
</Button>
</DialogTrigger>
<DialogContent className="max-w-6xl max-h-[90vh] overflow-y-auto">
<KeybindingEditor
shortcuts={shortcuts}
onClose={() => setShowEditor(false)}
/>
</DialogContent>
</Dialog>
);
}
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button variant="text" size="sm" className="gap-2">
<Keyboard className="w-4 h-4" />
Shortcuts
</Button>
</DialogTrigger>
<DialogContent className="max-w-2xl max-h-[80vh] overflow-y-auto">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<Keyboard className="w-5 h-5" />
Keyboard Shortcuts
</DialogTitle>
<DialogDescription>
Speed up your video editing workflow with these keyboard shortcuts.
Most shortcuts work when the timeline is focused.
</DialogDescription>
</DialogHeader>
2025-07-16 14:37:08 +02:00
<div className="flex justify-end mb-4">
<Button
variant="outline"
size="sm"
onClick={() => setShowEditor(true)}
>
<Settings className="w-4 h-4 mr-2" />
Customize
</Button>
</div>
<div className="space-y-6">
2025-07-16 14:37:08 +02:00
{categories.map((category) => (
2025-07-16 15:52:25 +02:00
<div key={category} className="flex flex-col gap-1">
<h3 className="text-xs text-muted-foreground uppercase tracking-wide font-medium">
{category}
</h3>
2025-07-16 15:52:25 +02:00
<div className="space-y-0.5">
{shortcuts
2025-07-16 14:37:08 +02:00
.filter((shortcut) => shortcut.category === category)
.map((shortcut, index) => (
<ShortcutItem key={index} shortcut={shortcut} />
))}
</div>
</div>
))}
</div>
</DialogContent>
</Dialog>
);
2025-07-16 14:37:08 +02:00
};
2025-07-18 11:26:49 +02:00
function ShortcutKey({ children }: { children: React.ReactNode }) {
return (
<kbd
2025-07-18 11:26:49 +02:00
className="inline-flex font-sans text-xs rounded px-2 min-w-[1.5rem] min-h-[1.5rem] leading-none items-center justify-center shadow-sm border mr-1"
style={{
backgroundColor: "rgba(0, 0, 0, 0.2)",
borderColor: "rgba(255, 255, 255, 0.1)",
2025-07-18 11:26:49 +02:00
}}
>
{children}
</kbd>
);
}