feat: add keyboard shortcuts R/S/L/D/T/? with shortcuts modal

This commit is contained in:
Marko Djordjevic 2026-02-17 20:19:25 +01:00
parent 028a3382d6
commit 2faa8879f3
2 changed files with 113 additions and 2 deletions

View file

@ -10,6 +10,8 @@ import SpanAnnotationList from '@/components/SpanAnnotationList';
import TalibPatternPanel from '@/components/TalibPatternPanel';
import TrainingPanel from '@/components/TrainingPanel';
import { ThemeToggle } from '@/components/ThemeToggle';
import KeyboardShortcutsModal from '@/components/KeyboardShortcutsModal';
import { useTheme } from 'next-themes';
import { Settings, Tag, Layers } from 'lucide-react';
import type { PredictionState, PredictionSpan, ModelInfoResponse, Disagreement, DisagreementType, PredictionSummary } from '@/types/predictions';
@ -159,7 +161,9 @@ interface SpanLabelType {
}
export default function Home() {
const { theme, setTheme } = useTheme();
const [settingsOpen, setSettingsOpen] = useState(false);
const [shortcutsOpen, setShortcutsOpen] = useState(false);
const [activeTool, setActiveTool] = useState<Tool | 'span'>(null);
const [selectedColor, setSelectedColor] = useState('#3b82f6');
const [selectedLabelId, setSelectedLabelId] = useState<number | null>(null);
@ -698,9 +702,14 @@ export default function Home() {
}
}, [predictionState.visible, predictionState.spans, spanAnnotations]);
// Keyboard handler for Delete/Backspace key
// Keyboard handler for Delete/Backspace key and tool shortcuts
useEffect(() => {
const handleKeyDown = async (e: KeyboardEvent) => {
// Ignore shortcuts when typing in an input/textarea/select
const tag = (e.target as HTMLElement).tagName;
if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT') return;
// Delete selected marker annotation
if ((e.key === 'Delete' || e.key === 'Backspace') && selectedLabelId !== null) {
try {
const response = await fetch(`/api/annotations/${selectedLabelId}`, {
@ -713,17 +722,41 @@ export default function Home() {
} catch (error) {
console.error('Failed to delete label:', error);
}
return;
}
// Tool shortcuts (only when not in a modal)
switch (e.key.toLowerCase()) {
case 'r':
setActiveTool((prev) => (prev === 'rectangle' ? null : 'rectangle'));
break;
case 's':
setActiveTool((prev) => (prev === 'span' ? null : 'span'));
break;
case 'l':
setActiveTool((prev) => (prev === 'line' ? null : 'line'));
break;
case 'd':
setActiveTool((prev) => (prev === 'delete' ? null : 'delete'));
break;
case 't':
setTheme(theme === 'dark' ? 'light' : 'dark');
break;
case '?':
setShortcutsOpen((prev) => !prev);
break;
}
};
window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
}, [selectedLabelId]);
}, [selectedLabelId, theme, setTheme]);
const activeChart = charts.find((c) => c.id === activeChartId);
return (
<div className="flex h-screen w-full overflow-hidden bg-background">
<KeyboardShortcutsModal open={shortcutsOpen} onClose={() => setShortcutsOpen(false)} />
{/* Sidebar */}
<div className="flex flex-col bg-sidebar border-r border-sidebar-border w-60 flex-shrink-0 animate-fade-in">
{/* Sidebar Header */}

View file

@ -0,0 +1,78 @@
'use client';
import { useEffect } from 'react';
import { X } from 'lucide-react';
interface KeyboardShortcutsModalProps {
open: boolean;
onClose: () => void;
}
const shortcuts = [
{ key: 'R', description: 'Rectangle tool' },
{ key: 'S', description: 'Span tool' },
{ key: 'L', description: 'Line tool' },
{ key: 'D', description: 'Delete tool' },
{ key: 'T', description: 'Toggle theme (light/dark)' },
{ key: 'Esc', description: 'Deselect tool / cancel action' },
{ key: 'Del / ⌫', description: 'Delete selected annotation' },
{ key: 'Enter', description: 'Edit selected span' },
{ key: '16', description: 'Quick-assign span label (during span creation)' },
{ key: '?', description: 'Show this help' },
];
export default function KeyboardShortcutsModal({ open, onClose }: KeyboardShortcutsModalProps) {
useEffect(() => {
if (!open) return;
const handleKey = (e: KeyboardEvent) => {
if (e.key === 'Escape' || e.key === '?') {
onClose();
}
};
window.addEventListener('keydown', handleKey);
return () => window.removeEventListener('keydown', handleKey);
}, [open, onClose]);
if (!open) return null;
return (
<div
className="fixed inset-0 z-50 flex items-center justify-center bg-black/50"
onClick={onClose}
>
<div
className="bg-background border border-border rounded-lg shadow-xl w-96 p-5"
onClick={(e) => e.stopPropagation()}
>
<div className="flex items-center justify-between mb-4">
<h2 className="text-sm font-semibold text-foreground">Keyboard Shortcuts</h2>
<button
onClick={onClose}
className="p-1 rounded text-muted-foreground hover:text-foreground hover:bg-secondary transition-colors"
>
<X className="w-4 h-4" />
</button>
</div>
<table className="w-full text-xs">
<tbody>
{shortcuts.map(({ key, description }) => (
<tr key={key} className="border-b border-border last:border-0">
<td className="py-1.5 pr-4 font-mono">
<kbd className="px-1.5 py-0.5 bg-secondary rounded text-foreground font-mono text-[11px]">
{key}
</kbd>
</td>
<td className="py-1.5 text-muted-foreground">{description}</td>
</tr>
))}
</tbody>
</table>
<p className="mt-4 text-[10px] text-muted-foreground text-center">
Press <kbd className="px-1 py-0.5 bg-secondary rounded font-mono">?</kbd> or <kbd className="px-1 py-0.5 bg-secondary rounded font-mono">Esc</kbd> to close
</p>
</div>
</div>
);
}