import { useEffect, useReducer, useState, type Dispatch } from 'react' import type { AnatomicalSheetDocument, Annotation, } from '@faunosteo/core-wasm/types' import type { CommitContext } from '@/editor/annotations/annotationCommit' import { useAnnotationRequest } from '@/editor/annotations/focusTarget' import { keyboardPending, type NewKind } from '@/editor/annotations/creation' import { parseFrame, type Frame } from '@/editor/annotations/frame' import { isEditable } from '@/editor/annotations/geometryEdit' import { usePlacement } from '@/editor/annotations/usePlacement' import { initialUi, uiReducer, type ToolId, type UiAction, type UiState, } from '@/editor/annotations/uiState' import type { RootAttributes } from '@/editor/annotations/useCanvasBuild' import { mainViewId, useViewModel, type ViewModel, } from '@/editor/annotations/useViewModel' import type { AnnotationSection } from '@/editor/navigation' import { usePlaceVisible } from '@/lib/placeVisibility' import type { EditorPlaceProps } from '@/editor/tabs' import { useT } from '@/i18n/useT' import { useLocaleStore } from '@/state/localeStore' import AnnotationCanvas from './AnnotationCanvas' import { annotationName } from './AnnotationList' import CreationPanel from './CreationPanel' import KeyboardAdd from './KeyboardAdd' import OverviewPanel from './OverviewPanel' import PropertiesPanel, { UnmeasuredHint } from './PropertiesPanel' import SelectionTag from './SelectionTag' import TracingBar from './TracingBar' import ViewSwitcher from './ViewSwitcher' interface Props { doc: AnatomicalSheetDocument navigate: EditorPlaceProps['navigate'] onOpenSection: (section: AnnotationSection, opener: HTMLElement) => void focus: EditorPlaceProps['focus'] } /** What both columns read: the tab's state, the view, its frame and its selection. */ interface Scene { doc: AnatomicalSheetDocument ui: UiState dispatch: Dispatch viewId: string model: ViewModel /** F of the current view, only while the core measures it and the build is its own. */ frame: Frame | null selected: Annotation | undefined setRoot: (root: RootAttributes | null) => void navigate: EditorPlaceProps['navigate'] onOpenSection: Props['onOpenSection'] } /** * The shown view no longer exists (an undo, a deletion in the views section): the full * reset to the main view drops the pending creation, the tracing and the selection, so * nothing pending is inserted on another view. It fires only for a view absent from the * document, since `view` always returns a new state. */ function useViewFallback( doc: AnatomicalSheetDocument, shown: string | null, dispatch: Dispatch, ) { useEffect(() => { if (shown === null || doc.views.some((view) => view.id === shown)) return const main = mainViewId(doc) if (doc.views.some((view) => view.id === main)) { dispatch({ type: 'view', viewId: main }) } }, [doc, shown, dispatch]) } /** « Aller à » an annotation: the request, with what it needs of the drawing's state. */ function useSceneRequest( { doc, focus }: Props, ui: UiState, viewId: string, dispatch: Dispatch, ) { useViewFallback(doc, ui.viewId, dispatch) useAnnotationRequest(focus, doc, { viewId, selectedId: ui.selectedId, busy: ui.pending ? 'creation' : ui.tracing ? 'tracing' : null, show: (view, id) => { dispatch({ type: 'view', viewId: view }) dispatch({ type: 'select', id }) }, }) } function useScene(props: Props): Scene { const { doc, navigate, onOpenSection } = props const [ui, dispatch] = useReducer(uiReducer, initialUi) const [root, setRoot] = useState(null) const viewId = ui.viewId ?? mainViewId(doc) const model = useViewModel(doc, viewId) const own = model.measured && root?.viewId === viewId useSceneRequest(props, ui, viewId, dispatch) return { doc, ui, dispatch, viewId, model, frame: own ? parseFrame(root.viewBox) : null, selected: model.annotations.find((a) => a.id === ui.selectedId), setRoot, navigate, onOpenSection, } } function useCommitContext(): CommitContext { const t = useT() const locale = useLocaleStore((s) => s.locale) return { t, locale } } export default function EditableAnnotations(props: Props) { const scene = useScene(props) const context = useCommitContext() const placement = usePlacement({ ...scene, context }) return (
) } type Placement = ReturnType function CanvasColumn({ scene, placement, }: { scene: Scene placement: Placement }) { const { doc, ui, dispatch, model, selected } = scene const noCategory = doc.categories.length === 0 return (
dispatch({ type: 'view', viewId: id })} /> {selected && } {ui.tracing && ( dispatch({ type: 'trace_cancel' })} /> )} addAtCentre(kind, scene.frame, dispatch)} />
) } function TagOf({ annotation, doc, }: { annotation: Annotation doc: AnatomicalSheetDocument }) { const locale = useLocaleStore((s) => s.locale) return ( ) } function CanvasStage({ scene, placement, }: { scene: Scene placement: Placement }) { const locale = useLocaleStore((s) => s.locale) // Hidden (the editor is read-only): no drag in flight survives, none is committed. const shown = usePlaceVisible() const { doc, ui, dispatch, model, frame, selected } = scene const view = doc.views.find((v) => v.id === scene.viewId) const blocked = !model.measured || doc.categories.length === 0 if (!view) return null return ( dispatch({ type: 'tool', tool }), drawingDisabled: blocked, linkDisabled: model.children.length === 0, }} onRoot={scene.setRoot} onSelect={(id) => dispatch({ type: 'select', id })} onPlace={placement.onPlace} onDrop={placement.onDrop} onTraceKey={placement.onTraceKey} /> ) } function addAtCentre( kind: NewKind, frame: Frame | null, dispatch: Dispatch, ) { if (!frame) return dispatch({ type: 'pending_new', pending: keyboardPending(kind, frame) }) } /** The creation panel while an element is pending, else the selection's properties. */ function SidePanel({ scene, context, }: { scene: Scene context: CommitContext }) { const { doc, ui, dispatch, model, frame, selected } = scene if (ui.pending) { return ( ) } if (!selected) { return ( dispatch({ type: 'filter', term })} onSelectView={(id) => dispatch({ type: 'view', viewId: id })} onSelectAnnotation={(id) => dispatch({ type: 'select', id })} onOpenSection={scene.onOpenSection} /> ) } return ( dispatch({ type: 'select', id: null })} /> ) } function moveTargetOf( selected: Annotation | undefined, model: ViewModel, tool: ToolId, ) { if (!selected || !model.measured || tool !== 'select') return null return isEditable(selected.geometry) ? { id: selected.id, geometry: selected.geometry } : null } function overlayOf( ui: UiState, selected: Annotation | undefined, frame: Frame | null, ) { return { frame, selected: selected?.geometry ?? null, pending: ui.pending?.geometry ?? null, tracing: ui.tracing?.points ?? null, correction: ui.pending?.correction ?? null, } } /** The tool line and the reasons a tool cannot be used now. */ function ToolNotes({ tool, hint, model, noCategory, viewId, navigate, }: { tool: ToolId hint: string | null model: ViewModel noCategory: boolean viewId: string navigate: EditorPlaceProps['navigate'] }) { const t = useT() const linkBlocked = tool === 'link' && model.children.length === 0 return (

{t(`editor.annotations.tool_hint.${tool}`)}

{!model.measured && ( )} {noCategory &&

{t('editor.annotations.no_category')}

} {linkBlocked &&

{t('editor.annotations.no_child_view')}

} {hint && (

{hint}

)}
) }