feat: Add keyboard shortcut enhancements with Kbd component
- Add shadcn kbd component for displaying keyboard shortcuts - Enhance file manager context menu to display shortcuts with Kbd component - Add 5 new keyboard shortcuts to file manager: - Ctrl+D: Download selected files - Ctrl+N: Create new file - Ctrl+Shift+N: Create new folder - Ctrl+U: Upload files - Enter: Open/run selected file - Add keyboard shortcut hints to command palette footer - Create helper function to parse and render keyboard shortcuts
This commit is contained in:
28
src/components/ui/kbd.tsx
Normal file
28
src/components/ui/kbd.tsx
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { cn } from "@/lib/utils"
|
||||||
|
|
||||||
|
function Kbd({ className, ...props }: React.ComponentProps<"kbd">) {
|
||||||
|
return (
|
||||||
|
<kbd
|
||||||
|
data-slot="kbd"
|
||||||
|
className={cn(
|
||||||
|
"bg-muted text-muted-foreground pointer-events-none inline-flex h-5 w-fit min-w-5 items-center justify-center gap-1 rounded-sm px-1 font-sans text-xs font-medium select-none",
|
||||||
|
"[&_svg:not([class*='size-'])]:size-3",
|
||||||
|
"[[data-slot=tooltip-content]_&]:bg-background/20 [[data-slot=tooltip-content]_&]:text-background dark:[[data-slot=tooltip-content]_&]:bg-background/10",
|
||||||
|
className
|
||||||
|
)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function KbdGroup({ className, ...props }: React.ComponentProps<"div">) {
|
||||||
|
return (
|
||||||
|
<kbd
|
||||||
|
data-slot="kbd-group"
|
||||||
|
className={cn("inline-flex items-center gap-1", className)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export { Kbd, KbdGroup }
|
||||||
@@ -8,6 +8,7 @@ import {
|
|||||||
} from "@/components/ui/command.tsx";
|
} from "@/components/ui/command.tsx";
|
||||||
import React, { useEffect, useRef, useState } from "react";
|
import React, { useEffect, useRef, useState } from "react";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
|
import { Kbd, KbdGroup } from "@/components/ui/kbd";
|
||||||
import {
|
import {
|
||||||
Key,
|
Key,
|
||||||
Server,
|
Server,
|
||||||
@@ -377,6 +378,20 @@ export function CommandPalette({
|
|||||||
</CommandItem>
|
</CommandItem>
|
||||||
</CommandGroup>
|
</CommandGroup>
|
||||||
</CommandList>
|
</CommandList>
|
||||||
|
<div className="border-t border-dark-border px-4 py-2 bg-dark-hover/50 flex items-center justify-between text-xs text-muted-foreground">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<span>Press</span>
|
||||||
|
<KbdGroup>
|
||||||
|
<Kbd>Shift</Kbd>
|
||||||
|
<Kbd>Shift</Kbd>
|
||||||
|
</KbdGroup>
|
||||||
|
<span>to toggle</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<span>Close</span>
|
||||||
|
<Kbd>Esc</Kbd>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</Command>
|
</Command>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1928,6 +1928,8 @@ function FileManagerContent({ initialHost, onClose }: FileManagerProps) {
|
|||||||
createIntent={createIntent}
|
createIntent={createIntent}
|
||||||
onConfirmCreate={handleConfirmCreate}
|
onConfirmCreate={handleConfirmCreate}
|
||||||
onCancelCreate={handleCancelCreate}
|
onCancelCreate={handleCancelCreate}
|
||||||
|
onNewFile={handleCreateNewFile}
|
||||||
|
onNewFolder={handleCreateNewFolder}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<FileManagerContextMenu
|
<FileManagerContextMenu
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import {
|
|||||||
Bookmark,
|
Bookmark,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
import { Kbd, KbdGroup } from "@/components/ui/kbd";
|
||||||
|
|
||||||
interface FileItem {
|
interface FileItem {
|
||||||
name: string;
|
name: string;
|
||||||
@@ -425,6 +426,20 @@ export function FileManagerContextMenu({
|
|||||||
return index > 0 && index < filteredMenuItems.length - 1;
|
return index > 0 && index < filteredMenuItems.length - 1;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const renderShortcut = (shortcut: string) => {
|
||||||
|
const keys = shortcut.split("+");
|
||||||
|
if (keys.length === 1) {
|
||||||
|
return <Kbd>{keys[0]}</Kbd>;
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<KbdGroup>
|
||||||
|
{keys.map((key, index) => (
|
||||||
|
<Kbd key={index}>{key}</Kbd>
|
||||||
|
))}
|
||||||
|
</KbdGroup>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="fixed inset-0 z-[99990]" />
|
<div className="fixed inset-0 z-[99990]" />
|
||||||
@@ -470,9 +485,9 @@ export function FileManagerContextMenu({
|
|||||||
<span className="flex-1">{item.label}</span>
|
<span className="flex-1">{item.label}</span>
|
||||||
</div>
|
</div>
|
||||||
{item.shortcut && (
|
{item.shortcut && (
|
||||||
<span className="text-xs text-muted-foreground ml-2 flex-shrink-0">
|
<div className="ml-2 flex-shrink-0">
|
||||||
{item.shortcut}
|
{renderShortcut(item.shortcut)}
|
||||||
</span>
|
</div>
|
||||||
)}
|
)}
|
||||||
</button>
|
</button>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -92,6 +92,8 @@ interface FileManagerGridProps {
|
|||||||
createIntent?: CreateIntent | null;
|
createIntent?: CreateIntent | null;
|
||||||
onConfirmCreate?: (name: string) => void;
|
onConfirmCreate?: (name: string) => void;
|
||||||
onCancelCreate?: () => void;
|
onCancelCreate?: () => void;
|
||||||
|
onNewFile?: () => void;
|
||||||
|
onNewFolder?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const getFileIcon = (file: FileItem, viewMode: "grid" | "list" = "grid") => {
|
const getFileIcon = (file: FileItem, viewMode: "grid" | "list" = "grid") => {
|
||||||
@@ -192,6 +194,8 @@ export function FileManagerGrid({
|
|||||||
createIntent,
|
createIntent,
|
||||||
onConfirmCreate,
|
onConfirmCreate,
|
||||||
onCancelCreate,
|
onCancelCreate,
|
||||||
|
onNewFile,
|
||||||
|
onNewFolder,
|
||||||
}: FileManagerGridProps) {
|
}: FileManagerGridProps) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const gridRef = useRef<HTMLDivElement>(null);
|
const gridRef = useRef<HTMLDivElement>(null);
|
||||||
@@ -772,6 +776,42 @@ export function FileManagerGrid({
|
|||||||
onUndo();
|
onUndo();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case "d":
|
||||||
|
case "D":
|
||||||
|
if (
|
||||||
|
(event.ctrlKey || event.metaKey) &&
|
||||||
|
selectedFiles.length > 0 &&
|
||||||
|
onDownload
|
||||||
|
) {
|
||||||
|
event.preventDefault();
|
||||||
|
onDownload(selectedFiles);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "n":
|
||||||
|
case "N":
|
||||||
|
if (event.ctrlKey || event.metaKey) {
|
||||||
|
event.preventDefault();
|
||||||
|
if (event.shiftKey && onNewFolder) {
|
||||||
|
onNewFolder();
|
||||||
|
} else if (!event.shiftKey && onNewFile) {
|
||||||
|
onNewFile();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "u":
|
||||||
|
case "U":
|
||||||
|
if ((event.ctrlKey || event.metaKey) && onUpload) {
|
||||||
|
event.preventDefault();
|
||||||
|
const input = document.createElement("input");
|
||||||
|
input.type = "file";
|
||||||
|
input.multiple = true;
|
||||||
|
input.onchange = (e) => {
|
||||||
|
const files = (e.target as HTMLInputElement).files;
|
||||||
|
if (files) onUpload(files);
|
||||||
|
};
|
||||||
|
input.click();
|
||||||
|
}
|
||||||
|
break;
|
||||||
case "Delete":
|
case "Delete":
|
||||||
if (selectedFiles.length > 0 && onDelete) {
|
if (selectedFiles.length > 0 && onDelete) {
|
||||||
onDelete(selectedFiles);
|
onDelete(selectedFiles);
|
||||||
@@ -783,6 +823,12 @@ export function FileManagerGrid({
|
|||||||
onStartEdit(selectedFiles[0]);
|
onStartEdit(selectedFiles[0]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case "Enter":
|
||||||
|
if (selectedFiles.length === 1) {
|
||||||
|
event.preventDefault();
|
||||||
|
onFileOpen(selectedFiles[0]);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case "y":
|
case "y":
|
||||||
case "Y":
|
case "Y":
|
||||||
if (event.ctrlKey || event.metaKey) {
|
if (event.ctrlKey || event.metaKey) {
|
||||||
|
|||||||
Reference in New Issue
Block a user