import type { KeyboardEvent } from 'react' import type { Annotation } from '@faunosteo/core-wasm/types' import { useEditorStore } from '../state/editorStore' import { moveStep, type Frame } from './frame' import type { Drop } from './usePlacement' import { takeMove, takePreview, stepSelection } from './previewCommit' import { chooseVertex, freshChosen } from './zoneEdit' import { endMode, startMove, useAnnotationWork, type Preview, } from './workStore' export interface PointEditInput { /** The selection when it can be moved now: a circle (point, link) or a polygon. */ target: Annotation | null frame: Frame | null onDrop: Drop } export type Direction = 'left' | 'up' | 'down' | 'right' const DELTA: Record = { left: [-1, 0], up: [0, -1], down: [0, 1], right: [1, 0], } const KEY_DIRECTION: Record = { ArrowLeft: 'left', ArrowUp: 'up', ArrowDown: 'down', ArrowRight: 'right', } /** One step (R-11) of the target, into the move mode or a keyboard series. */ function step(input: PointEditInput, direction: Direction, big: boolean) { const { target, frame } = input if (!target?.geometry || !frame) return const size = moveStep(frame, big) const [x, y] = DELTA[direction] stepSelection(target.id, target.geometry, { x: x * size, y: y * size }) } /** A preview back on the stored shape (there and back, a pinned slider) is no change. */ function changed( preview: Preview | null, target: Annotation | null, ): preview is Preview { if (!preview || preview.annotationId !== target?.id) return false return JSON.stringify(preview.geometry) !== JSON.stringify(target.geometry) } /** The commits of the pending work: each takes its preview, fresh and changed only (R-13). */ function commits({ onDrop, target }: PointEditInput) { const keys = () => { const preview = takePreview('keys') if (changed(preview, target)) onDrop(preview.annotationId, preview.geometry) } const size = () => { const preview = takePreview('size') if (changed(preview, target)) { onDrop(preview.annotationId, preview.geometry, 'size') } } const move = () => { const vertex = useAnnotationWork.getState().mode?.vertex ?? null const preview = takeMove() if (!preview) return const { annotationId: id, geometry } = preview if (vertex === null) return void onDrop(id, geometry) // The moved vertex stays chosen on the new text. if (onDrop(id, geometry, 'vertex')) chooseVertex(id, vertex) } return { keys, size, move } } const inMoveMode = () => useAnnotationWork.getState().mode?.kind === 'move' /** The point handle's keys: arrows step (Shift ×10), Enter commits the series or the mode. */ function handleKeys(input: PointEditInput, commit: ReturnType) { return (event: KeyboardEvent) => { const direction = KEY_DIRECTION[event.key] if (direction) { event.preventDefault() return step(input, direction, event.shiftKey) } if (event.key !== 'Enter') return event.preventDefault() if (inMoveMode()) commit.move() else commit.keys() } } /** * Point editing (R-10 to R-13): the handle's keyboard series (one commit on Enter or * blur), the size slider's commit, and the move mode (« Déplacer le point », its arrows, * « Valider la position » committing once, « Annuler »). */ export function usePointEdit(input: PointEditInput) { const commit = commits(input) return { onHandleKey: handleKeys(input, commit), // BDR-050: leaving the handle commits its series; a move mode waits for « Valider ». onHandleBlur: () => commit.keys(), commitSize: commit.size, // « Déplacer le sommet » while a vertex is chosen, else the whole shape. startMove: () => { const id = input.target?.id if (!id) return const base = useEditorStore.getState().documentText startMove(id, base, freshChosen(id)) }, stepMove: (direction: Direction) => step(input, direction, false), confirmMove: commit.move, cancelMove: endMode, } } export type PointEdit = ReturnType