feat: scaffold complete danable frontend architecture with all pages, layouts, stores, and 3 annotator modes
This commit is contained in:
commit
78848dd7d1
173 changed files with 18649 additions and 0 deletions
147
src/hooks/useKeyboardShortcuts.ts
Normal file
147
src/hooks/useKeyboardShortcuts.ts
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
import { useEffect } from 'react'
|
||||
import { useAnnotationStore } from '@/stores/annotationStore'
|
||||
import { useCanvasStore } from '@/stores/canvasStore'
|
||||
import { LabelClass } from '@/types'
|
||||
|
||||
export interface KeyboardShortcutsOptions {
|
||||
classes?: LabelClass[]
|
||||
onSave?: () => void
|
||||
onSubmit?: () => void
|
||||
onPrevImage?: () => void
|
||||
onNextImage?: () => void
|
||||
isClassification?: boolean
|
||||
enabled?: boolean
|
||||
}
|
||||
|
||||
export function useKeyboardShortcuts({
|
||||
classes = [],
|
||||
onSave,
|
||||
onSubmit,
|
||||
onPrevImage,
|
||||
onNextImage,
|
||||
isClassification = false,
|
||||
enabled = true,
|
||||
}: KeyboardShortcutsOptions = {}) {
|
||||
const setActiveTool = useCanvasStore((s) => s.setActiveTool)
|
||||
const selectedShapeId = useAnnotationStore((s) => s.selectedShapeId)
|
||||
const deleteShape = useAnnotationStore((s) => s.deleteShape)
|
||||
const updateShape = useAnnotationStore((s) => s.updateShape)
|
||||
const setActiveLabelId = useAnnotationStore((s) => s.setActiveLabelId)
|
||||
const setClassificationLabel = useAnnotationStore((s) => s.setClassificationLabel)
|
||||
const undo = useAnnotationStore((s) => s.undo)
|
||||
const redo = useAnnotationStore((s) => s.redo)
|
||||
|
||||
useEffect(() => {
|
||||
if (!enabled) return
|
||||
|
||||
const handleKeyDown = (event: KeyboardEvent) => {
|
||||
const target = event.target as HTMLElement | null
|
||||
if (
|
||||
target &&
|
||||
(target.tagName === 'INPUT' ||
|
||||
target.tagName === 'TEXTAREA' ||
|
||||
target.tagName === 'SELECT' ||
|
||||
target.isContentEditable)
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
const key = event.key.toLowerCase()
|
||||
|
||||
// Ctrl+S / Cmd+S: Lưu
|
||||
if ((event.ctrlKey || event.metaKey) && key === 's') {
|
||||
event.preventDefault()
|
||||
onSave?.()
|
||||
return
|
||||
}
|
||||
|
||||
// Ctrl+Z / Cmd+Z: Undo / Redo
|
||||
if ((event.ctrlKey || event.metaKey) && key === 'z') {
|
||||
event.preventDefault()
|
||||
if (event.shiftKey) {
|
||||
redo()
|
||||
} else {
|
||||
undo()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Phím tắt công cụ: V (Select), R/W (BBox), P (Polygon)
|
||||
if (!event.ctrlKey && !event.metaKey && !event.altKey) {
|
||||
if (key === 'v') {
|
||||
setActiveTool('select')
|
||||
return
|
||||
}
|
||||
if (key === 'r' || key === 'w') {
|
||||
setActiveTool('bbox')
|
||||
return
|
||||
}
|
||||
if (key === 'p') {
|
||||
setActiveTool('polygon')
|
||||
return
|
||||
}
|
||||
|
||||
// A / ArrowLeft: Ảnh trước, D / ArrowRight: Ảnh tiếp
|
||||
if (key === 'a' || event.key === 'ArrowLeft') {
|
||||
onPrevImage?.()
|
||||
return
|
||||
}
|
||||
if (key === 'd' || event.key === 'ArrowRight') {
|
||||
onNextImage?.()
|
||||
return
|
||||
}
|
||||
|
||||
// Enter: Nộp duyệt hoặc xác nhận
|
||||
if (event.key === 'Enter' && onSubmit) {
|
||||
event.preventDefault()
|
||||
onSubmit()
|
||||
return
|
||||
}
|
||||
|
||||
// Delete / Backspace: Xóa shape đang chọn
|
||||
if ((event.key === 'Delete' || event.key === 'Backspace') && selectedShapeId) {
|
||||
event.preventDefault()
|
||||
deleteShape(selectedShapeId)
|
||||
return
|
||||
}
|
||||
|
||||
// Phím số 1-9: Chọn lớp nhãn
|
||||
if (/^[1-9]$/.test(key)) {
|
||||
const idx = parseInt(key, 10) - 1
|
||||
const targetClass = classes[idx]
|
||||
if (targetClass) {
|
||||
setActiveLabelId(targetClass.id)
|
||||
if (isClassification) {
|
||||
setClassificationLabel(targetClass.id)
|
||||
} else if (selectedShapeId) {
|
||||
updateShape(selectedShapeId, {
|
||||
labelId: targetClass.id,
|
||||
labelName: targetClass.name,
|
||||
color: targetClass.color,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener('keydown', handleKeyDown)
|
||||
return () => window.removeEventListener('keydown', handleKeyDown)
|
||||
}, [
|
||||
enabled,
|
||||
classes,
|
||||
isClassification,
|
||||
selectedShapeId,
|
||||
setActiveTool,
|
||||
deleteShape,
|
||||
updateShape,
|
||||
setActiveLabelId,
|
||||
setClassificationLabel,
|
||||
undo,
|
||||
redo,
|
||||
onSave,
|
||||
onSubmit,
|
||||
onPrevImage,
|
||||
onNextImage,
|
||||
])
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue