HandleLayer: its own svg over the overlay, not aria-hidden; the selected
point (or link circle) gets a focusable « Point {nom} » handle whose hit
circle (data-handle) the press machine reads, sized in CSS px under zoom
(R-9, read once from any-pointer: coarse). The Task 2 stand-in (the
selected marker as its own handle) is removed.
Keys on the handle: arrows step 0.5 % of the frame (Shift x10) as a
stamped series, Enter or blur commit once, Escape drops it (R-11, R-13).
SizeSlider in the strip for a selected point and for the Point tool (A5):
the view's sizes as ticks, the core minimum as a red mark, snap 8 px,
arrows 1 %, Page Up/Down next size, one commit on release, Enter or blur;
a refusal restores the size and draws the refused place in red dashes.
« Déplacer le point » opens the move mode (board 06 B): drag anywhere or
four arrows move a preview only, clicks select nothing, « Valider la
position » commits once, « Annuler » and Escape restore; the focus comes
back to the button. Escape follows board 32 (escapeAction). The overlay
reads the preview from the work store and draws the old place in grey
dashes. Ctrl+Z now also works from a slider (not a text field).
Rewritten assertions:
- editor-annotations-tab « a finger drags the selected point »:
drag(marker('anno-a')) -> drag(pointHandle()), same expected geometry.
- editor-geometry.spec « a point inherited out of its frame is repaired
through the geometry group » (cx/cy typed 200) -> « ... repaired with
the move mode »: a partial move refused and restored (cx 110, issue
kept), then a move inside kept (issue gone, fewer errors).
162 lines
5.9 KiB
TypeScript
162 lines
5.9 KiB
TypeScript
import type { Dispatch, KeyboardEvent } from 'react'
|
|
import type {
|
|
Annotation,
|
|
AnatomicalSheetDocument,
|
|
Geometry,
|
|
Point,
|
|
} from '@faunosteo/core-wasm/types'
|
|
import { useT } from '@/i18n/useT'
|
|
import { updateGeometry, type CommitContext } from './annotationCommit'
|
|
import { pendingForClick, type NewKind } from './creation'
|
|
import { round2, type Frame, type XY } from './frame'
|
|
import { newPointSize } from './pointSize'
|
|
import type { ToolId, Tracing, UiAction, UiState } from './uiState'
|
|
import type { GestureMessages } from './useGestureMessage'
|
|
|
|
/** A click on the first vertex closes the tracing when within this share of F's span. */
|
|
const CLOSE_SHARE = 0.015
|
|
|
|
const TOOL_TARGET: Record<Exclude<ToolId, 'select'>, NewKind> = {
|
|
point: 'point',
|
|
zone: 'zone',
|
|
link: 'detail_trigger',
|
|
}
|
|
|
|
export interface PlacementInput {
|
|
ui: UiState
|
|
dispatch: Dispatch<UiAction>
|
|
frame: Frame | null
|
|
doc: AnatomicalSheetDocument
|
|
viewId: string
|
|
context: CommitContext
|
|
/** The line under the drawing: a kept drop says so with « Défaire », a refused one in red. */
|
|
message: Pick<GestureMessages, 'show'>
|
|
}
|
|
|
|
const asPoint = (at: XY): Point => [round2(at.x), round2(at.y)]
|
|
|
|
function nearFirstVertex(tracing: Tracing, at: XY, frame: Frame): boolean {
|
|
const first = tracing.points[0]
|
|
if (!first || tracing.points.length < 3) return false
|
|
const reach = CLOSE_SHARE * Math.max(frame.w, frame.h)
|
|
return Math.hypot(first[0] - at.x, first[1] - at.y) <= reach
|
|
}
|
|
|
|
/**
|
|
* What closing a tracing does: a new zone or link becomes the pending element; a polygon
|
|
* replacing a geometry is committed at once under `accept`, and a refusal drops the
|
|
* tracing with the core's message (the annotation is unchanged).
|
|
*/
|
|
export function closingActions(
|
|
tracing: Tracing | null,
|
|
doc: AnatomicalSheetDocument,
|
|
context: CommitContext,
|
|
): UiAction[] {
|
|
if (!tracing || tracing.points.length < 3) return []
|
|
const polygon = { type: 'polygon' as const, points: tracing.points }
|
|
if (tracing.target !== 'replace') {
|
|
const pending = { kind: tracing.target, geometry: polygon }
|
|
return [{ type: 'pending_new', pending }]
|
|
}
|
|
const annotation = doc.annotations.find((a) => a.id === tracing.annotationId)
|
|
if (!annotation) return [{ type: 'trace_cancel' }]
|
|
const result = updateGeometry(annotation, polygon, context)
|
|
if (result.ok) return [{ type: 'select', id: annotation.id }]
|
|
return [
|
|
{ type: 'trace_cancel' },
|
|
{ type: 'hint', hint: result.messages.join(' ') },
|
|
]
|
|
}
|
|
|
|
/** Tracing with the zone and link tools: vertices, closing, Enter, Backspace and Escape. */
|
|
function tracingActions(input: PlacementInput) {
|
|
const { ui, dispatch, frame } = input
|
|
const closeTracing = () =>
|
|
closingActions(ui.tracing, input.doc, input.context).forEach(dispatch)
|
|
const trace = (tool: Exclude<ToolId, 'select' | 'point'>, at: XY) => {
|
|
if (!frame) return
|
|
if (ui.tracing && nearFirstVertex(ui.tracing, at, frame)) {
|
|
return closeTracing()
|
|
}
|
|
if (!ui.tracing)
|
|
dispatch({ type: 'trace_start', target: TOOL_TARGET[tool] })
|
|
dispatch({ type: 'trace_add', point: asPoint(at) })
|
|
}
|
|
const onTraceKey = (event: KeyboardEvent) => {
|
|
if (!ui.tracing) return
|
|
if (event.key === 'Enter') closeTracing()
|
|
if (event.key === 'Backspace') {
|
|
event.preventDefault()
|
|
dispatch({ type: 'trace_pop' })
|
|
}
|
|
if (event.key === 'Escape') {
|
|
event.preventDefault()
|
|
dispatch({ type: 'trace_cancel' })
|
|
}
|
|
}
|
|
return { closeTracing, trace, onTraceKey }
|
|
}
|
|
|
|
/** A move (drag, move mode, arrow keys) or a size change of the selection. */
|
|
export type GeometryChange = 'move' | 'size'
|
|
|
|
/** The message keys of a kept and of a refused change. */
|
|
function changeKeys(change: GeometryChange, kind: Annotation['kind']) {
|
|
return change === 'size'
|
|
? { kept: 'resized', refused: 'size_refused' }
|
|
: { kept: `moved.${kind}`, refused: 'move_refused' }
|
|
}
|
|
|
|
/**
|
|
* A drop commits one update; the line under the drawing says it, or the core's refusal
|
|
* with the refused place drawn (the shape stays where it was).
|
|
*/
|
|
function dropAction({ doc, context, message, dispatch }: PlacementInput) {
|
|
return (id: string, geometry: Geometry, change: GeometryChange = 'move') => {
|
|
const annotation = doc.annotations.find((a) => a.id === id)
|
|
if (!annotation) return
|
|
const keys = changeKeys(change, annotation.kind)
|
|
const result = updateGeometry(annotation, geometry, context)
|
|
if (result.ok) {
|
|
// A kept move ends any refusal the strip still shows.
|
|
dispatch({ type: 'hint', hint: null })
|
|
const text = context.t(`editor.annotations.message.${keys.kept}`)
|
|
return message.show(text, { undo: true, tone: 'info' })
|
|
}
|
|
const text = context
|
|
.t(`editor.annotations.message.${keys.refused}`)
|
|
.replace('{text}', () => result.messages.join(' '))
|
|
message.show(text, { undo: false, tone: 'error', place: geometry })
|
|
}
|
|
}
|
|
|
|
/**
|
|
* What a click, a key or a drop does with the current tool. Every geometry it proposes is
|
|
* arithmetic over F; the core decides at « Créer » or at the commit of an existing one.
|
|
* `onTraceKey` is for the canvas viewport only (a key from a button is the button's).
|
|
*/
|
|
export function usePlacement(input: PlacementInput) {
|
|
const t = useT()
|
|
const tracing = tracingActions(input)
|
|
const { ui, dispatch, frame } = input
|
|
const onPlace = (at: XY) => {
|
|
if (ui.tool === 'zone' || ui.tool === 'link')
|
|
return tracing.trace(ui.tool, at)
|
|
if (ui.tool !== 'point' || !frame) return
|
|
const { doc, viewId } = input
|
|
const size = newPointSize(doc.annotations, viewId, frame, ui.size)
|
|
const outcome = pendingForClick(frame, at, size)
|
|
if (outcome.kind === 'pending') {
|
|
return dispatch({ type: 'pending_new', pending: outcome.pending })
|
|
}
|
|
dispatch({ type: 'hint', hint: t('editor.annotations.outside') })
|
|
}
|
|
return {
|
|
onPlace,
|
|
onDrop: dropAction(input),
|
|
onTraceKey: tracing.onTraceKey,
|
|
closeTracing: tracing.closeTracing,
|
|
popVertex: () => dispatch({ type: 'trace_pop' }),
|
|
}
|
|
}
|