feat(app): editor Annotations tab, core-decided placement, repair rules
This commit is contained in:
1 parent
92c595caa1
commit
26097852ff
37 files changed
+5001
-6
No files matched your search
@@ -0,0 +1,109 @@
|
||||
import type { Dispatch, KeyboardEvent } from 'react'
|
||||
import type {
|
||||
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 type { ToolId, Tracing, UiAction, UiState } from './uiState'
|
||||
|
||||
/** 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
|
||||
context: CommitContext
|
||||
}
|
||||
|
||||
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(' ') },
|
||||
]
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
export function usePlacement(input: PlacementInput) {
|
||||
const t = useT()
|
||||
const { ui, dispatch, frame, doc, context } = input
|
||||
|
||||
const placePoint = (frame: Frame, at: XY) => {
|
||||
const outcome = pendingForClick(frame, at)
|
||||
if (outcome.kind === 'pending') {
|
||||
return dispatch({ type: 'pending_new', pending: outcome.pending })
|
||||
}
|
||||
dispatch({ type: 'hint', hint: t('editor.annotations.outside') })
|
||||
}
|
||||
const closeTracing = () =>
|
||||
closingActions(ui.tracing, doc, 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 onPlace = (at: XY) => {
|
||||
if (ui.tool === 'point' && frame) placePoint(frame, at)
|
||||
else if (ui.tool === 'zone' || ui.tool === 'link') trace(ui.tool, at)
|
||||
}
|
||||
const onDrop = (id: string, geometry: Geometry) => {
|
||||
const annotation = doc.annotations.find((a) => a.id === id)
|
||||
if (!annotation) return
|
||||
const result = updateGeometry(annotation, geometry, context)
|
||||
const hint = result.ok ? null : result.messages.join(' ')
|
||||
dispatch({ type: 'hint', hint })
|
||||
}
|
||||
const onKeyDown = (event: KeyboardEvent) => {
|
||||
if (!ui.tracing) return
|
||||
if (event.key === 'Enter') closeTracing()
|
||||
if (event.key === 'Escape') dispatch({ type: 'trace_cancel' })
|
||||
}
|
||||
return { onPlace, onDrop, onKeyDown, closeTracing }
|
||||
}
|
||||
Reference in new issue
Block a user