feat: copy and paste shapes in the review editor (REQ-195)
Two ways to copy a shape, both writing a real manual label: - Ctrl/Cmd + drag on a shape: a dashed ghost follows the pointer and the copy is written where the button is released, keeping the source class. The original is never moved or resized; Ctrl wins over the resize, vertex and midpoint handles. - Ctrl/Cmd+C / Ctrl/Cmd+V: the editor-local clipboard holds shapes only, never the system clipboard. Paste lands 0.015 down-right, clamped inside the frame. Ctrl+C and Ctrl+V are the only browser combos the editor claims; every other combo still reaches the browser, and typing in a field triggers nothing.
This commit is contained in:
1 parent
de5693457c
commit
d53e800fd2
10 files changed
+293
-24
No files matched your search
+1
-1
@@ -350,7 +350,7 @@ Pages (11, routed by hash in `App.jsx`):
|
||||
4. **Data Prep** (`/data-prep/{pid}`) — the merge gate: pick batches, review frames, merge into datasets (REQ-052).
|
||||
5. **Datasets** (`/datasets/{pid}`) — dataset list and membership.
|
||||
6. **Trim** (`/trim/{pid}`) — `<video>` + in/out timeline, fps input, estimated frame count, extract button.
|
||||
7. **Review** (`/batches/{bid}`) — status-coloured filmstrip, canvas editor, class panel, go-to-frame box (`G`), shortcuts (`←`/`→` frame, `A` approve, `X` reject, `Del` delete shape, `T` track-forward, `C` copy, `U` next-unreviewed, `Shift+H` reveal-all overlay, digits set active class), *Approve batch* button.
|
||||
7. **Review** (`/batches/{bid}`) — status-coloured filmstrip, canvas editor, class panel, go-to-frame box (`G`), shortcuts (`←`/`→` frame, `A` approve, `X` reject, `Del` delete shape, `T` track-forward, `C` copy, `U` next-unreviewed, `Shift+H` reveal-all overlay, digits set active class), *Approve batch* button. **Shape clipboard (REQ-195):** `Ctrl`/`Cmd`+drag on a shape clones it — a dashed ghost follows the pointer, the copy is written where the button is released and keeps the source class; `Ctrl`/`Cmd`+`C` / `Ctrl`/`Cmd`+`V` copy and paste shapes editor-locally (`frontend/src/hooks/useShapeClipboard.js`, paste offset `0.015` down-right, clamped inside the frame). These two combos are the only browser shortcuts the editor claims — every other Ctrl/Cmd combo stays with the browser.
|
||||
8. **Models** (`/models/{pid}`) — engine selection cards, train button, job progress, base-vs-new mAP comparison, download & promote.
|
||||
9. **Counting Bench** (`/counting-bench/{pid}`) — offline counting accuracy runs (REQ-146…).
|
||||
10. **Live Counting** (`/live-count/{pid}`) — live session: source, line, tracker, count.
|
||||
|
||||
@@ -509,3 +509,17 @@ changes.
|
||||
scaling — and again at `1200 px`; below `560 px` the GPU/VRAM diagnostic badges drop out and
|
||||
the wordmark shrinks, while the SAM3 badge, error notices and the theme toggle always survive.
|
||||
The bar stays 48 px and the page itself never scrolls horizontally.
|
||||
- **REQ-195** — The review editor copies shapes two ways, and only these two. **(1) Ctrl/Cmd
|
||||
+ drag:** pressing Ctrl/Cmd and clicking a shape starts a clone — a dashed ghost follows the
|
||||
pointer and, on release, a **new** shape with the **source's class** is written at the drop
|
||||
point; the original is never moved or resized, and the copy stays draggable afterwards.
|
||||
**(2) Ctrl/Cmd+C / Ctrl/Cmd+V:** copy takes the selected shape (or, in Select mode, every
|
||||
marked shape) and paste writes it onto the current frame, offset `0.015` down-right and
|
||||
clamped so it stays inside the frame. Both write real `source: "manual"` labels — a copy
|
||||
counts like any hand-drawn shape in merge and training. The clipboard is **editor-local**
|
||||
(shapes only, never the system clipboard), survives frame changes inside the review page and
|
||||
dies with the page. **Ctrl/Cmd+C and Ctrl/Cmd+V are the only browser combinations the editor
|
||||
claims** — every other combo (Ctrl+S, Ctrl+A, Ctrl+X…) still belongs to the browser, and
|
||||
typing in a field never triggers either. The Shift and Alt variants (Ctrl+Shift+C is the
|
||||
browser's developer tools) stay with the browser as well. With nothing selected, Ctrl/Cmd+C
|
||||
does nothing and the browser's own copy is left alone.
|
||||
@@ -1659,6 +1659,44 @@ read it.
|
||||
`scrollLeft: 464` — active-tab scroll-in works. Caveat: Firefox headless clamps the window to
|
||||
500 px, so the true-375 media query was not exercised by the harness.
|
||||
|
||||
## Task — Copy and paste shapes in the review editor (REQ-195) `[DONE]`
|
||||
|
||||
- Two ways to copy, both writing a real `source: "manual"` label (so a copy counts in merge
|
||||
and training like any drawn shape): **Ctrl/Cmd + drag** a shape, or **Ctrl/Cmd+C /
|
||||
Ctrl/Cmd+V**.
|
||||
- `frontend/src/geometry.js` — `shift(geometry, dx, dy)` moved out of `AnnotationCanvas.jsx`
|
||||
(it was module-private) so the clipboard can offset a copy; it clamps the offset so the
|
||||
shape stays inside the frame.
|
||||
- `frontend/src/hooks/useShapeClipboard.js` — the editor-local clipboard: `copy()` takes the
|
||||
selected shape (or every marked shape in Select mode), `paste()` writes it onto the current
|
||||
frame `0.015` down-right, `create()` writes the single shape a clone drag drops. Shapes
|
||||
only — never the system clipboard; it survives frame changes and dies with the page.
|
||||
- `frontend/src/components/AnnotationCanvas.jsx` — `clone` state next to `draft`: Ctrl/Cmd
|
||||
starts a clone drag in Draw mode (Select mode keeps its marquee toggle), Ctrl/Cmd wins over
|
||||
the resize/vertex/midpoint handles, `Escape` cancels the ghost, and the ghost renders dashed
|
||||
in the source class colour. The original shape is never moved or resized.
|
||||
- `frontend/src/pages/ReviewPage.jsx` — the Ctrl/Cmd carve-out sits **before** the
|
||||
`ctrlKey || metaKey || altKey` bail-out, so Ctrl+S and friends still reach the browser, and
|
||||
the combo is swallowed only when it actually copied or pasted something. New shapes land
|
||||
like any drawn shape: appended, selected, counted on the frame.
|
||||
- `frontend/src/app.css` — `.canvas-wrap .clone` (dashed, semi-transparent,
|
||||
`pointer-events: none`, `non-scaling-stroke`).
|
||||
- `frontend/src/components/ShortcutsPanel.jsx` — `Ctrl+Click` and `Ctrl+C / V` rows.
|
||||
- Docs: `requirements.md` REQ-195, `ui-spec.md` §5.4 gesture table + clone ghost, §5.8
|
||||
keyboard map and the Ctrl bullet, §6.3 rule 19 exception, Indonesian summary;
|
||||
`design.md` Frontend section.
|
||||
→ verify: **[DONE]** `npm run build` green, `git diff --check` clean. `/tmp/opencode/shape_copy_check.py`
|
||||
drives headless Firefox through geckodriver against a **copied** database
|
||||
(`APP_DATA_DIR=/tmp/opencode/testdata`, `app.db` copied, `projects`/`base_models`/`archive`
|
||||
symlinked) with a throwaway backend on :8001 — **20/20 PASS**: the clone drag creates exactly
|
||||
one annotation, lands it at the drop point (`delta=(0.0804,0.0919)` — expected the same),
|
||||
keeps the source class, is `source: "manual"` and leaves the original alone; Ctrl+click
|
||||
without movement copies in place; Ctrl+drag on a resize handle clones instead of resizing;
|
||||
Ctrl+C/Ctrl+V pastes one shape offset `0.015` down-right; Select mode copies the marked set
|
||||
and the pasted shape joins it; Ctrl+S changes nothing; no context menu on ctrl+click; no JS
|
||||
errors. The production `data/app.db` was never touched (frame 24125 stayed at 9 annotations
|
||||
while the test database climbed past 30).
|
||||
|
||||
## Known open points
|
||||
|
||||
- *Not closed by any task, by choice:* **any rebuild kills the running job.** Task 14's resume
|
||||
|
||||
+29
-3
@@ -1004,6 +1004,8 @@ when marked. When selected *and* editable it also gets a label badge reading
|
||||
| Drag on a shape | moves it | toggles its marked state — never moves it |
|
||||
| Click on empty canvas | clears the selected shape | clears the marked set |
|
||||
| Escape | cancels the gesture in flight (never commits it) | clears the marked set |
|
||||
| Ctrl/Cmd + drag on a shape | **clones** it (REQ-195): a dashed ghost follows the pointer and the copy is written where the button is released | toggles its marked state — never moves it |
|
||||
| Ctrl/Cmd + click on a shape (no movement) | clones it **in place** — same geometry, one new shape | toggles its marked state |
|
||||
|
||||
Drags shorter than `0.004` normalized on both axes are treated as clicks.
|
||||
|
||||
@@ -1016,6 +1018,13 @@ shape the user was only trying to tick off, so handles are not rendered there at
|
||||
pointer-up sends one `PATCH /annotations/{id}`. A failed patch **rolls the shape back to its
|
||||
previous geometry** and shows the error. Same optimistic-with-rollback pattern for delete.
|
||||
|
||||
**Clone ghost (REQ-195).** While a Ctrl/Cmd-drag is in flight the copy is drawn as a dashed,
|
||||
half-transparent outline in the source class's colour — a ghost, never a pointer target, never
|
||||
a real shape. It is dropped as a new shape where the button is released; the original is left
|
||||
exactly where it was. Ctrl/Cmd wins over every editing handle (resize corner, polygon vertex,
|
||||
midpoint): it always means *clone*, never *resize*. The copy keeps the **source's** class, not
|
||||
the active one.
|
||||
|
||||
### 5.5 Modes and bars
|
||||
|
||||
**Mode switch** (`Draw` / `Select`, `V` toggles). Switching clears the marked set.
|
||||
@@ -1173,6 +1182,11 @@ Handlers are attached in the **capture** phase on `document`, and:
|
||||
- **ignore events whose target is an `input`, `textarea`, `select` or `contenteditable`**;
|
||||
- **ignore anything with Ctrl / Cmd / Alt** — those belong to the browser and the OS. Without
|
||||
this, Ctrl+S approves the frame and Ctrl+A/C/X/N/T all fire review actions;
|
||||
- **except `Ctrl`/`Cmd`+`C` and `Ctrl`/`Cmd`+`V`** (REQ-195), the only two combos the editor
|
||||
claims: they copy and paste shapes and are swallowed only when they actually did something
|
||||
(something was copied / something was pasted). With nothing selected, Ctrl+C is left to the
|
||||
browser untouched; the Shift and Alt variants stay with the browser too (Ctrl+Shift+C is the
|
||||
developer tools);
|
||||
- `preventDefault` + `stopPropagation` only for keys that are actually shortcuts.
|
||||
|
||||
| Key | Draw mode | Select mode |
|
||||
@@ -1184,6 +1198,8 @@ Handlers are attached in the **capture** phase on `document`, and:
|
||||
| `U` | jump to next pending frame (`GET /batches/{id}/next-pending?after_idx=`) | same |
|
||||
| `N` | jump to next annotated frame | same |
|
||||
| `C` | copy annotations from previous frame | same |
|
||||
| `Ctrl`/`Cmd`+`C` | copy the selected shape into the editor clipboard (REQ-195) | copy **all marked** shapes |
|
||||
| `Ctrl`/`Cmd`+`V` | paste the clipboard onto this frame, `0.015` down-right (REQ-195) | same |
|
||||
| `T` | track selected shape forward N frames (N = the count input) | same |
|
||||
| `1`–`9` | set active class; reclass the selected shape if any | reclass **all marked** shapes |
|
||||
| `Del` / `Backspace` | delete the selected shape | delete **all marked** shapes (confirmed) |
|
||||
@@ -1333,7 +1349,11 @@ Removing any of these breaks the pipeline, corrupts data, or produces numbers th
|
||||
|
||||
18. **Every Review action has a keyboard shortcut, and the Shortcuts panel stays visible.** The
|
||||
mouse is for drawing shapes, not for navigating.
|
||||
19. **Ctrl/Cmd/Alt combos are never intercepted.** Ctrl+S must not approve a frame.
|
||||
19. **Ctrl/Cmd/Alt combos are never intercepted.** Ctrl+S must not approve a frame. **Exception
|
||||
(REQ-195): `Ctrl`/`Cmd`+`C` and `Ctrl`/`Cmd`+`V` are the editor's shape clipboard** — they are
|
||||
the only two combos the editor claims, they carry shapes only (never text, never the system
|
||||
clipboard), they are swallowed only when they actually copied or pasted something, and their
|
||||
Shift/Alt variants (Ctrl+Shift+C = developer tools) stay with the browser.
|
||||
20. **Typing in an input never triggers a shortcut.**
|
||||
21. **Select mode does not move shapes; draw mode does not marquee.** Mixing them silently
|
||||
rewrites geometry the user was only ticking off.
|
||||
@@ -1619,9 +1639,15 @@ Shortcuts).
|
||||
Overlap/NMS, Min box size, Max box size, Max shapes), lalu baru **Apply** yang menulis. Shift-drag = contoh
|
||||
negatif ("bukan ini"). Panel filter duduk di **sidebar**, bukan menutupi frame.
|
||||
- Tahan `S` = SAM3 click-assist (satu box → satu shape).
|
||||
- **Salin shape (REQ-195) dua cara:** tahan `Ctrl`/`Cmd` lalu klik-tarik shape → ghost putus-putus
|
||||
mengikuti pointer, dilepas = shape **baru** dengan class yang sama muncul di titik lepas;
|
||||
atau `Ctrl`/`Cmd`+`C` menyalin (di Select mode: semua shape yang ditandai) lalu
|
||||
`Ctrl`/`Cmd`+`V` menempelnya di frame ini dengan offset `0.015` ke kanan-bawah. Clipboard
|
||||
milik editor saja (bukan system clipboard), bertahan saat pindah frame, mati bersama halaman.
|
||||
- Keyboard-first: `V ← → A X U N C T 1–9 Del H Esc Enter`. Semua aksi punya shortcut; panel
|
||||
Shortcuts selalu terlihat. Ctrl/Cmd/Alt **tidak pernah** di-intercept; mengetik di input tidak
|
||||
memicu shortcut.
|
||||
Shortcuts selalu terlihat. Ctrl/Cmd/Alt **tidak pernah** di-intercept — kecuali
|
||||
`Ctrl`/`Cmd`+`C` dan `Ctrl`/`Cmd`+`V` untuk salin-tempel shape (REQ-195); mengetik di input
|
||||
tidak memicu shortcut.
|
||||
- Approve/Reject langsung pindah frame dan meng-update status lokal sebelum request selesai.
|
||||
|
||||
### Aturan mutlak — melanggar ini merusak pipeline atau membuat angkanya berbohong
|
||||
|
||||
@@ -555,6 +555,17 @@ main.page {
|
||||
vector-effect: non-scaling-stroke;
|
||||
}
|
||||
|
||||
/* REQ-195: the clone ghost — the copy being dragged to its drop point. It is
|
||||
dashed and half transparent so it reads as a copy, not as the real shape. */
|
||||
.canvas-wrap .clone {
|
||||
fill: rgba(255, 255, 255, 0.08);
|
||||
stroke-width: 2;
|
||||
stroke-dasharray: 6 4;
|
||||
opacity: 0.9;
|
||||
pointer-events: none;
|
||||
vector-effect: non-scaling-stroke;
|
||||
}
|
||||
|
||||
.frame-bar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useEffect, useLayoutEffect, useRef, useState } from 'react'
|
||||
import { classColor } from '../api'
|
||||
import { shift } from '../geometry'
|
||||
import Shape from './Shape'
|
||||
|
||||
/* The annotation surface: the frame with an SVG overlay on top.
|
||||
@@ -40,13 +41,14 @@ function overlaps(geometry, [mx0, my0, mx1, my1]) {
|
||||
export default function AnnotationCanvas({
|
||||
frame, imageUrl, annotations, selectedId, activeClass, assistMode, classes,
|
||||
mode = 'draw', selectedIds, exemplars = [], preview = null,
|
||||
onSelect, onCreate, onUpdate, onAssist, onExemplar, onMarquee,
|
||||
onSelect, onCreate, onUpdate, onAssist, onExemplar, onMarquee, onClone,
|
||||
}) {
|
||||
const wrapRef = useRef(null)
|
||||
const svgRef = useRef(null)
|
||||
const [displayWidth, setDisplayWidth] = useState(0)
|
||||
const [draft, setDraft] = useState(null) // box being drawn
|
||||
const [drag, setDrag] = useState(null) // move/resize in progress
|
||||
const [clone, setClone] = useState(null) // copy being dragged into place
|
||||
const selecting = mode === 'select'
|
||||
const marked = selectedIds instanceof Set ? selectedIds : new Set(selectedIds ?? [])
|
||||
|
||||
@@ -95,8 +97,29 @@ export default function AnnotationCanvas({
|
||||
event.currentTarget.setPointerCapture(event.pointerId)
|
||||
}
|
||||
|
||||
// REQ-195: with Ctrl/Cmd held, a drag on a shape copies it and the copy
|
||||
// follows the pointer until the button is released. preventDefault stops
|
||||
// the Linux context menu, which a ctrl+click would otherwise open.
|
||||
function startClone(event, annotation) {
|
||||
event.preventDefault()
|
||||
if (event.button !== 0) return
|
||||
const [x, y] = pointAt(event)
|
||||
setClone({ classId: annotation.class_id, geometry: annotation.geometry })
|
||||
setDrag({ kind: 'clone', origin: [x, y], start: annotation.geometry,
|
||||
classId: annotation.class_id })
|
||||
event.currentTarget.setPointerCapture(event.pointerId)
|
||||
}
|
||||
|
||||
function startMove(event, annotation) {
|
||||
event.stopPropagation()
|
||||
if (event.ctrlKey || event.metaKey) {
|
||||
if (selecting) {
|
||||
onMarquee?.([annotation.id], { toggle: true })
|
||||
return
|
||||
}
|
||||
startClone(event, annotation)
|
||||
return
|
||||
}
|
||||
// Select mode never moves a shape: a stray drag on top of a box would
|
||||
// silently edit the geometry the user was only trying to tick off.
|
||||
if (selecting) {
|
||||
@@ -112,6 +135,8 @@ export default function AnnotationCanvas({
|
||||
|
||||
function startResize(event, annotation, corner) {
|
||||
event.stopPropagation()
|
||||
// Ctrl wins over every editing handle: it means "clone", not "resize".
|
||||
if (event.ctrlKey || event.metaKey) { startClone(event, annotation); return }
|
||||
onSelect(annotation.id)
|
||||
setDrag({ kind: 'resize', id: annotation.id, corner,
|
||||
start: annotation.geometry })
|
||||
@@ -120,6 +145,7 @@ export default function AnnotationCanvas({
|
||||
|
||||
function startVertex(event, annotation, pointIndex) {
|
||||
event.stopPropagation()
|
||||
if (event.ctrlKey || event.metaKey) { startClone(event, annotation); return }
|
||||
onSelect(annotation.id)
|
||||
setDrag({ kind: 'vertex', id: annotation.id, pointIndex, start: annotation.geometry })
|
||||
event.currentTarget.setPointerCapture(event.pointerId)
|
||||
@@ -127,6 +153,7 @@ export default function AnnotationCanvas({
|
||||
|
||||
function startMidpoint(event, annotation, afterIndex, newPoint) {
|
||||
event.stopPropagation()
|
||||
if (event.ctrlKey || event.metaKey) { startClone(event, annotation); return }
|
||||
onSelect(annotation.id)
|
||||
const newPoints = [...annotation.geometry.points]
|
||||
newPoints.splice(afterIndex + 1, 0, newPoint)
|
||||
@@ -160,6 +187,10 @@ export default function AnnotationCanvas({
|
||||
if (drag.kind === 'move') {
|
||||
const [dx, dy] = [x - drag.origin[0], y - drag.origin[1]]
|
||||
onUpdate(drag.id, shift(drag.start, dx, dy), { local: true })
|
||||
} else if (drag.kind === 'clone') {
|
||||
// The copy follows the pointer; nothing is written until pointer-up.
|
||||
const [dx, dy] = [x - drag.origin[0], y - drag.origin[1]]
|
||||
setClone({ classId: drag.classId, geometry: shift(drag.start, dx, dy) })
|
||||
} else {
|
||||
const [x0, y0, x1, y1] = boxPoints(drag.start)
|
||||
const next = drag.corner === 'nw' ? [x, y, x1, y1]
|
||||
@@ -194,6 +225,14 @@ export default function AnnotationCanvas({
|
||||
return
|
||||
}
|
||||
if (drag) {
|
||||
if (drag.kind === 'clone') {
|
||||
// The copy becomes a real shape where the pointer was let go.
|
||||
const geometry = clone?.geometry
|
||||
setClone(null)
|
||||
setDrag(null)
|
||||
if (geometry) onClone?.(drag.classId, geometry)
|
||||
return
|
||||
}
|
||||
onUpdate(drag.id, null, { commit: true })
|
||||
setDrag(null)
|
||||
}
|
||||
@@ -205,6 +244,7 @@ export default function AnnotationCanvas({
|
||||
if (event.key !== 'Escape') return
|
||||
setDraft(null)
|
||||
setDrag(null)
|
||||
setClone(null)
|
||||
}
|
||||
window.addEventListener('keydown', onKey)
|
||||
return () => window.removeEventListener('keydown', onKey)
|
||||
@@ -295,6 +335,30 @@ export default function AnnotationCanvas({
|
||||
)
|
||||
})}
|
||||
|
||||
{/* REQ-195: the clone ghost — the copy being dragged into place. It
|
||||
sits on top of everything and is never a pointer target. */}
|
||||
{clone && (() => {
|
||||
const colour = classColor(clone.classId)
|
||||
if (clone.geometry.type === 'polygon') {
|
||||
return (
|
||||
<polygon
|
||||
className="clone"
|
||||
points={clone.geometry.points.map(([px, py]) => `${px * width},${py * height}`).join(' ')}
|
||||
stroke={colour}
|
||||
/>
|
||||
)
|
||||
}
|
||||
const [x0, y0, x1, y1] = clone.geometry.points
|
||||
return (
|
||||
<rect
|
||||
className="clone"
|
||||
x={x0 * width} y={y0 * height}
|
||||
width={(x1 - x0) * width} height={(y1 - y0) * height}
|
||||
stroke={colour}
|
||||
/>
|
||||
)
|
||||
})()}
|
||||
|
||||
{draft && (() => {
|
||||
const [x0, y0, x1, y1] = normalise(draft)
|
||||
return (
|
||||
@@ -313,19 +377,3 @@ export default function AnnotationCanvas({
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function shift(geometry, dx, dy) {
|
||||
if (geometry.type === 'bbox') {
|
||||
const [x0, y0, x1, y1] = geometry.points
|
||||
const clampedX = Math.min(Math.max(dx, -x0), 1 - x1)
|
||||
const clampedY = Math.min(Math.max(dy, -y0), 1 - y1)
|
||||
return { type: 'bbox',
|
||||
points: [x0 + clampedX, y0 + clampedY, x1 + clampedX, y1 + clampedY] }
|
||||
}
|
||||
const xs = geometry.points.map((p) => p[0])
|
||||
const ys = geometry.points.map((p) => p[1])
|
||||
const clampedX = Math.min(Math.max(dx, -Math.min(...xs)), 1 - Math.max(...xs))
|
||||
const clampedY = Math.min(Math.max(dy, -Math.min(...ys)), 1 - Math.max(...ys))
|
||||
return { type: 'polygon',
|
||||
points: geometry.points.map(([x, y]) => [x + clampedX, y + clampedY]) }
|
||||
}
|
||||
@@ -39,6 +39,14 @@ export default function ShortcutsPanel() {
|
||||
<dt><kbd>1</kbd>–<kbd>9</kbd></dt>
|
||||
<dd>Pick class</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt><kbd>Ctrl</kbd>+<kbd>Click</kbd></dt>
|
||||
<dd>Clone a shape → drag it to the target</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt><kbd>Ctrl</kbd>+<kbd>C</kbd> <kbd>V</kbd></dt>
|
||||
<dd>Copy / paste shapes</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt><kbd>Del</kbd></dt>
|
||||
<dd>Remove selected</dd>
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
/* Normalized geometry helpers shared by the canvas and the shape clipboard.
|
||||
*
|
||||
* Everything here works on the normalized 0–1 coordinates the app stores:
|
||||
* bbox {points:[x0,y0,x1,y1]}, polygon {points:[[x,y],…]}. */
|
||||
|
||||
export function shift(geometry, dx, dy) {
|
||||
if (geometry.type === 'bbox') {
|
||||
const [x0, y0, x1, y1] = geometry.points
|
||||
const clampedX = Math.min(Math.max(dx, -x0), 1 - x1)
|
||||
const clampedY = Math.min(Math.max(dy, -y0), 1 - y1)
|
||||
return { type: 'bbox', points: [x0 + clampedX, y0 + clampedY, x1 + clampedX, y1 + clampedY] }
|
||||
}
|
||||
const xs = geometry.points.map((p) => p[0])
|
||||
const ys = geometry.points.map((p) => p[1])
|
||||
const clampedX = Math.min(Math.max(dx, -Math.min(...xs)), 1 - Math.max(...xs))
|
||||
const clampedY = Math.min(Math.max(dy, -Math.min(...ys)), 1 - Math.max(...ys))
|
||||
return {
|
||||
type: 'polygon',
|
||||
points: geometry.points.map(([x, y]) => [x + clampedX, y + clampedY]),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import { useCallback, useRef } from 'react'
|
||||
import { api } from '../api'
|
||||
import { shift } from '../geometry'
|
||||
|
||||
/* The review editor's shape clipboard (REQ-195).
|
||||
*
|
||||
* It holds shapes, never text, and never the system clipboard — annotations
|
||||
* do not leave the app that way. It survives frame changes (same page
|
||||
* session) and dies with the review page.
|
||||
*
|
||||
* Two ways to copy:
|
||||
* - Ctrl/Cmd+drag on a shape — the clone ghost in the canvas calls
|
||||
* `create`, and the copy lands where the pointer was let go.
|
||||
* - Ctrl/Cmd+C then Ctrl/Cmd+V — `copy` takes the selected shape (or every
|
||||
* marked shape in Select mode), `paste` writes it onto this frame.
|
||||
*
|
||||
* A paste lands a little down and right of the source so the copy is visible
|
||||
* instead of hiding under the original. */
|
||||
|
||||
const PASTE_OFFSET = 0.015
|
||||
|
||||
export default function useShapeClipboard({ frame, annotations, selectedId, markedIds, onCreated, onError }) {
|
||||
const clipboard = useRef([])
|
||||
|
||||
const copy = useCallback(() => {
|
||||
const ids = markedIds?.length ? markedIds : (selectedId != null ? [selectedId] : [])
|
||||
const rows = ids.map((id) => annotations.find((row) => row.id === id)).filter(Boolean)
|
||||
if (!rows.length) return false
|
||||
clipboard.current = rows.map((row) => ({
|
||||
class_id: row.class_id,
|
||||
geometry: structuredClone(row.geometry),
|
||||
}))
|
||||
return true
|
||||
}, [annotations, markedIds, selectedId])
|
||||
|
||||
// One new annotation row — used by the clone drag.
|
||||
const create = useCallback(async (classId, geometry) => {
|
||||
if (!frame) return
|
||||
try {
|
||||
const created = await api.addAnnotation(frame.id, { class_id: classId, geometry })
|
||||
onCreated?.([created])
|
||||
} catch (exc) {
|
||||
onError?.(exc.message)
|
||||
}
|
||||
}, [frame, onCreated, onError])
|
||||
|
||||
const paste = useCallback(() => {
|
||||
const items = clipboard.current
|
||||
if (!items.length || !frame) return false
|
||||
;(async () => {
|
||||
const created = []
|
||||
try {
|
||||
// Sequential, not Promise.all: one failed shape must not lose the rest.
|
||||
for (const item of items) {
|
||||
const geometry = shift(item.geometry, PASTE_OFFSET, PASTE_OFFSET)
|
||||
created.push(await api.addAnnotation(frame.id, { class_id: item.class_id, geometry }))
|
||||
}
|
||||
onCreated?.(created)
|
||||
} catch (exc) {
|
||||
onError?.(exc.message)
|
||||
}
|
||||
})()
|
||||
return true
|
||||
}, [frame, onCreated, onError])
|
||||
|
||||
return { copy, paste, create }
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import QuickReclassBar from '../components/QuickReclassBar'
|
||||
import ReviewSidebar from '../components/ReviewSidebar'
|
||||
import ExemplarFilterPanel from '../components/ExemplarFilterPanel'
|
||||
import useExemplarPool from '../hooks/useExemplarPool'
|
||||
import useShapeClipboard from '../hooks/useShapeClipboard'
|
||||
|
||||
export default function ReviewPage({ batchId: rawBatchId, projectId, onProject }) {
|
||||
const [batch, setBatch] = useState(null)
|
||||
@@ -131,6 +132,24 @@ export default function ReviewPage({ batchId: rawBatchId, projectId, onProject }
|
||||
} catch (exc) { setError(exc.message) }
|
||||
}
|
||||
|
||||
// A new shape row lands the same way whether it was cloned, pasted or
|
||||
// drawn: appended, selected, and counted on the frame (REQ-195).
|
||||
const onShapesCreated = useCallback((created) => {
|
||||
if (!created?.length || !frame) return
|
||||
setAnnotations((rows) => [...rows, ...created])
|
||||
setSelectedId(created[created.length - 1].id)
|
||||
if (mode === 'select') setMarkedIds((current) => [...current, ...created.map((row) => row.id)])
|
||||
created.forEach((row) => {
|
||||
if (hiddenClasses.has(row.class_id)) setOverriddenShapeIds((current) => new Set(current).add(row.id))
|
||||
})
|
||||
patchFrameLocally(frame.id, { annotation_count: (frame.annotation_count ?? 0) + created.length })
|
||||
}, [frame, mode, hiddenClasses])
|
||||
|
||||
const clipboard = useShapeClipboard({
|
||||
frame, annotations, selectedId, markedIds,
|
||||
onCreated: onShapesCreated, onError: setError,
|
||||
})
|
||||
|
||||
// The exemplar pool (REQ-173/174/175) owns the drag gesture in draw mode.
|
||||
const onExemplarApplied = useCallback((rows) => {
|
||||
setAnnotations(rows)
|
||||
@@ -478,13 +497,29 @@ export default function ReviewPage({ batchId: rawBatchId, projectId, onProject }
|
||||
}, [annotations, selectedId, index, frames, busy, trackFrames, project])
|
||||
|
||||
const stateRef = useRef({})
|
||||
stateRef.current = { frames, index, project, selectedId, setStatus, removeSelected, reclass, jumpToPending, jumpToNextAnnotated, setAssistMode, copyPrevious, trackForward, mode, setMode, markedIds, removeMarked, reclassMarked, setMarkedIds, toggleHideSelected, revealAll, setRevealAll }
|
||||
stateRef.current = { frames, index, project, selectedId, setStatus, removeSelected, reclass, jumpToPending, jumpToNextAnnotated, setAssistMode, copyPrevious, trackForward, mode, setMode, markedIds, removeMarked, reclassMarked, setMarkedIds, toggleHideSelected, revealAll, setRevealAll, copyShapes: clipboard.copy, pasteShapes: clipboard.paste }
|
||||
|
||||
useEffect(() => {
|
||||
function onKeyDown(event) {
|
||||
if (event.target?.matches?.('input, textarea, select, [contenteditable="true"]')) return
|
||||
// Ctrl/Cmd/Alt combos belong to the browser and the OS: without this,
|
||||
// Ctrl+S approves the frame and Ctrl+A/C/X/N/T all fire review actions.
|
||||
// REQ-195: Ctrl/Cmd+C and Ctrl/Cmd+V are the only browser combos the
|
||||
// review editor claims — they copy and paste shapes. Everything else
|
||||
// (Ctrl+S, Ctrl+A…) still belongs to the browser and the OS: without
|
||||
// that, Ctrl+S approves the frame and Ctrl+A/C/X/N/T all fire review
|
||||
// actions. Shift is left out too: Ctrl+Shift+C is the browser's
|
||||
// developer tools.
|
||||
if ((event.ctrlKey || event.metaKey) && !event.altKey && !event.shiftKey) {
|
||||
const combo = event.key.toLowerCase()
|
||||
if (combo === 'c') {
|
||||
if (stateRef.current.copyShapes?.()) { event.preventDefault(); event.stopPropagation() }
|
||||
return
|
||||
}
|
||||
if (combo === 'v') {
|
||||
if (stateRef.current.pasteShapes?.()) { event.preventDefault(); event.stopPropagation() }
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
if (event.ctrlKey || event.metaKey || event.altKey) return
|
||||
const { frames, project, setStatus, removeSelected, reclass, jumpToPending, jumpToNextAnnotated, setAssistMode, copyPrevious, trackForward, mode, setMode, markedIds, removeMarked, reclassMarked, setMarkedIds, toggleHideSelected, revealAll, setRevealAll } = stateRef.current
|
||||
const key = event.key
|
||||
@@ -653,6 +688,7 @@ export default function ReviewPage({ batchId: rawBatchId, projectId, onProject }
|
||||
onAssist={assist}
|
||||
onExemplar={pool.add}
|
||||
onMarquee={onMarquee}
|
||||
onClone={clipboard.create}
|
||||
/>
|
||||
)}
|
||||
|
||||
|
||||
Reference in new issue
Block a user