Files
faunosteo/apps/app/src/components/editor/annotations/ZoneHandles.tsx
T
bchanot 6211046537 feat(editor): vertex handles, insert and remove, zone move, redraw
U2 Task 5. A selected polygon gets its vertex, « + » and « − » handles
(R-9, hidden « + » under two hit radii, R-18 zoom hint in the strip),
the handle cap at 40 (R-7), vertex drag and keys, the move mode on the
zone or the chosen vertex, « Redessiner » as a redraw mode kept in step
with its tracing (R-12 guard, R-13 drop), close radius in CSS px
(R-11). The numeric group leaves the properties panel (D1).

Removed or rewritten assertions (numeric group -> replacement):
- tab « shows its exact stored numbers » (Centre x field) -> the overlay
  selection circle's cx attribute, same exact value.
- tab « a numeric edit » x3 -> « an edit on the drawing »: Enter on the
  handle without a step commits nothing, a step + Enter commits once;
  a refused handle drag keeps the stored shape then a drag inside the
  frame repairs it (issues gone); Escape drops a pending series.
- tab C26 « Centre x 15 » -> overlay cx 15.
- tab C32 unmeasured « Centre x as text » -> no handle, no field.
- tab « adds and removes a vertex as part of the whole group » -> « + »
  then « − » on the drawing; « never offers to remove below three » ->
  « − » aria-disabled at 3.
- tab H8 « refused Remplacer drops the tracing » -> keeps it open with
  « Fermeture refusée » (board 33 A); « refused vertex add/remove stay
  non appliqué » -> a refused vertex drag restores the shape exactly.
- tab Désélectionner and D6b (typed Centre x committed, refused, undone)
  x6 and strip « a new commit replaces the message » -> the same with
  the Nom (FR) field (a blank name is the refused value).
- focus-request « Centre x 20 » -> overlay cx 20.
- escape « strip commands keep their Escape » -> a redraw: Escape on the
  strip or the box cancels it (R-12 row 4); kept for a new zone tracing.
- keep-alive « Remplacer tracing kept across Aller à » -> ended by a tab
  when empty, asked once a vertex is placed (R-12).
- e2e annotations: numeric cx edit + undo/redo -> handle keys + undo/
  redo; jitter/drag and Task 4 point reads -> overlay attributes (the
  stored place during the mode -> overlay-old-place); keyboard-only cy
  and vertex x typed -> handle and vertex keys.
- e2e deselect: Centre x typed/refused/committed -> Nom (FR).
- e2e geometry, shell-crossing: Centre x/cy reads -> overlay attributes.
- e2e journey: cx +3 and vertex x +4 typed -> 3 and 4 arrow steps + Enter;
  a far typed value + revert -> a far handle drag refused.
- e2e equine latency: typed cx rounds -> handle arrow + Enter rounds.
2026-11-17 20:24:49 +01:00

326 lines
8.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useLayoutEffect, useRef, type KeyboardEvent } from 'react'
import type { Point } from '@faunosteo/core-wasm/types'
import type { HandleSizes } from '@/editor/annotations/handleSize'
import { MIN_VERTICES, showsInsert } from '@/editor/annotations/polygonEdit'
import type { ZoneEdit } from '@/editor/annotations/useZoneEdit'
import { useAnnotationWork } from '@/editor/annotations/workStore'
import { useT } from '@/i18n/useT'
/** The selected polygon whose vertex, « + » and « − » handles the layer draws. */
export interface ZoneHandle {
annotationId: string
points: Point[]
}
export type ZoneKeys = Pick<
ZoneEdit,
| 'onVertexKey'
| 'onVertexFocus'
| 'onVertexBlur'
| 'onInsertKey'
| 'onRemoveKey'
>
interface Scale {
/** Drawing units per CSS px. */
unit: number
sizes: HandleSizes
}
const segmentPx = (a: Point, b: Point, unit: number) =>
Math.hypot(b[0] - a[0], b[1] - a[1]) / unit
/** R-9: the « + » of segment `i` (vertex i to the next) shows at the current zoom. */
export function shownInserts(points: readonly Point[], { unit, sizes }: Scale) {
return points.map((p, i) =>
showsInsert(segmentPx(p, points[(i + 1) % points.length], unit), sizes.hit),
)
}
const RING = 'invisible group-focus-visible:visible'
function FocusRing({ cx, cy, r }: { cx: number; cy: number; r: number }) {
return (
<circle
cx={cx}
cy={cy}
r={r}
fill="none"
stroke="var(--color-accent)"
strokeWidth={2}
vectorEffect="non-scaling-stroke"
className={RING}
/>
)
}
const LINE = { vectorEffect: 'non-scaling-stroke', strokeWidth: 2 } as const
function Insert({
at,
after,
label,
scale,
onKeyDown,
}: {
at: [number, number]
after: number
label: string
scale: Scale
onKeyDown: (event: KeyboardEvent) => void
}) {
const { unit, sizes } = scale
const [cx, cy] = at
const r = (sizes.insert / 2) * unit
const arm = r * 0.5
return (
<g
role="button"
tabIndex={0}
aria-label={label}
className="group outline-none"
onKeyDown={onKeyDown}
>
<circle
cx={cx}
cy={cy}
r={r}
fill="white"
stroke="var(--color-accent)"
{...LINE}
/>
<path
d={`M ${cx - arm} ${cy} H ${cx + arm} M ${cx} ${cy - arm} V ${cy + arm}`}
stroke="var(--color-accent)"
{...LINE}
/>
<circle
cx={cx}
cy={cy}
r={Math.max(sizes.hit * unit, r)}
data-handle={JSON.stringify({ kind: 'insert', after })}
fill="transparent"
className="pointer-events-auto cursor-copy"
/>
<FocusRing cx={cx} cy={cy} r={r + 3 * unit} />
</g>
)
}
function Vertex({
at,
index,
chosen,
label,
scale,
keys,
}: {
at: Point
index: number
chosen: boolean
label: string
scale: Scale
keys: ZoneKeys
}) {
const { unit, sizes } = scale
const [cx, cy] = at
// A chosen vertex is bigger, filled and ringed: never told by colour alone.
const r = (sizes.vertex / 2 + (chosen ? 2 : 0)) * unit
return (
<g
role="button"
tabIndex={0}
aria-label={label}
aria-pressed={chosen}
data-vertex={index}
// Its arrow series commits on blur; the drawing guard blurs it before leaving.
data-series=""
className="group outline-none"
onKeyDown={(event) => keys.onVertexKey(index, event)}
onFocus={() => keys.onVertexFocus(index)}
onBlur={keys.onVertexBlur}
>
{chosen && (
<circle
cx={cx}
cy={cy}
r={r + 3 * unit}
fill="none"
stroke="var(--color-accent)"
{...LINE}
/>
)}
<circle
cx={cx}
cy={cy}
r={r}
fill={chosen ? 'var(--color-accent)' : 'white'}
stroke="var(--color-ink)"
{...LINE}
/>
<circle
cx={cx}
cy={cy}
r={Math.max(sizes.hit * unit, r)}
data-handle={JSON.stringify({ kind: 'vertex', index })}
fill="transparent"
className="pointer-events-auto cursor-move"
/>
<FocusRing cx={cx} cy={cy} r={r + 6 * unit} />
</g>
)
}
/** The « − » bubble over the chosen vertex (board 05), greyed at three vertices (12 B). */
function Remove({
at,
index,
disabled,
label,
scale,
onKeyDown,
}: {
at: Point
index: number
disabled: boolean
label: string
scale: Scale
onKeyDown: (event: KeyboardEvent) => void
}) {
const { unit, sizes } = scale
const w = sizes.bubble.w * unit
const h = sizes.bubble.h * unit
const x = at[0] - w / 2
const y = at[1] - (sizes.vertex / 2 + 6) * unit - h
const fill = disabled
? 'var(--color-ink-muted)'
: 'var(--color-danger-strong)'
return (
<g
role="button"
tabIndex={0}
aria-label={label}
aria-disabled={disabled}
className="group outline-none"
onKeyDown={onKeyDown}
>
<rect
x={x}
y={y}
width={w}
height={h}
rx={6 * unit}
fill={fill}
data-handle={JSON.stringify({ kind: 'remove', index })}
className={`pointer-events-auto ${disabled ? 'cursor-not-allowed' : 'cursor-pointer'}`}
/>
<path
d={`M ${x + w * 0.3} ${y + h / 2} H ${x + w * 0.7}`}
stroke="white"
{...LINE}
/>
<rect
x={x - 3 * unit}
y={y - 3 * unit}
width={w + 6 * unit}
height={h + 6 * unit}
rx={8 * unit}
fill="none"
stroke="var(--color-accent)"
{...LINE}
className={RING}
/>
</g>
)
}
/** The chosen vertex of this polygon, as the store holds it now. */
function useChosen(annotationId: string, count: number): number | null {
return useAnnotationWork((s) => {
const chosen = s.chosen
if (chosen?.annotationId !== annotationId) return null
return chosen.vertex < count ? chosen.vertex : null
})
}
/**
* A keyboard choice (« + », Insert, Delete) moves the focus to the vertex now chosen, so
* the keys keep acting where the user looks; a pointer choice leaves the focus alone.
*/
function useFocusFollowsChoice(chosen: number | null) {
const group = useRef<SVGGElement | null>(null)
useLayoutEffect(() => {
const active = document.activeElement
if (chosen === null || !group.current?.contains(active)) return
if (active?.getAttribute('data-vertex') === String(chosen)) return
const target = group.current.querySelector(`[data-vertex="${chosen}"]`)
if (target instanceof SVGElement) target.focus()
}, [chosen])
return group
}
const midpoint = (a: Point, b: Point): [number, number] => [
(a[0] + b[0]) / 2,
(a[1] + b[1]) / 2,
]
/**
* The polygon's handles (docs/ANNOTATION-RENDERING.md « Editor overlay »): the « + » of the
* segments long enough on screen, every vertex, then the « − » bubble of the chosen one,
* in that order (Tab walks them so). No handle covers the interior.
*/
export default function ZoneHandles({
zone,
scale,
keys,
}: {
zone: ZoneHandle
scale: Scale
keys: ZoneKeys
}) {
const t = useT()
const { points } = zone
const chosen = useChosen(zone.annotationId, points.length)
const group = useFocusFollowsChoice(chosen)
const inserts = shownInserts(points, scale)
const named = (key: string, n: number) =>
t(`editor.annotations.handle.${key}`).replace('{n}', String(n + 1))
return (
<g ref={group} data-testid="zone-handles">
{points.map((p, i) =>
inserts[i] ? (
<Insert
key={`insert-${i}`}
at={midpoint(p, points[(i + 1) % points.length])}
after={i}
label={named('insert', i)}
scale={scale}
onKeyDown={(event) => keys.onInsertKey(i, event)}
/>
) : null,
)}
{points.map((p, i) => (
<Vertex
key={`vertex-${i}`}
at={p}
index={i}
chosen={chosen === i}
label={named('vertex', i)}
scale={scale}
keys={keys}
/>
))}
{chosen !== null && (
<Remove
at={points[chosen]}
index={chosen}
disabled={points.length <= MIN_VERTICES}
label={named('remove', chosen)}
scale={scale}
onKeyDown={(event) => keys.onRemoveKey(chosen, event)}
/>
)}
</g>
)
}