feat(app): editor Annotations tab, core-decided placement, repair rules

This commit is contained in:
bchanot committed 2026-11-04 13:49:41 +01:00
1 parent 92c595caa1
commit 26097852ff
37 files changed
+5001 -6

No files matched your search

@@ -0,0 +1,254 @@
import { useReducer, useState, type Dispatch } from 'react'
import type {
AnatomicalSheetDocument,
Annotation,
} from '@faunosteo/core-wasm/types'
import { type CommitContext } from '@/editor/annotations/annotationCommit'
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 {
mainViewId,
useUndoShortcuts,
useViewModel,
type ViewModel,
} from '@/editor/annotations/useViewModel'
import type { EditorTabProps } from '@/editor/tabs'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
import AnnotationCanvas from './AnnotationCanvas'
import AnnotationList, { annotationName } from './AnnotationList'
import CreationPanel from './CreationPanel'
import KeyboardAdd from './KeyboardAdd'
import PropertiesPanel, { UnmeasuredHint } from './PropertiesPanel'
import SelectionTag from './SelectionTag'
import TracingBar from './TracingBar'
import ViewSwitcher from './ViewSwitcher'
interface Props {
doc: AnatomicalSheetDocument
onSelectTab: EditorTabProps['onSelectTab']
}
export default function EditableAnnotations({ doc, onSelectTab }: Props) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const [ui, dispatch] = useReducer(uiReducer, initialUi)
const [viewBox, setViewBox] = useState<string | null>(null)
const viewId = ui.viewId ?? mainViewId(doc)
const model = useViewModel(doc, viewId)
const frame = model.measured ? parseFrame(viewBox) : null
const context: CommitContext = { t, locale }
const placement = usePlacement({ ui, dispatch, frame, doc, context })
useUndoShortcuts()
const selected = model.annotations.find((a) => a.id === ui.selectedId)
const scene = { doc, ui, dispatch, model, frame, selected, onSelectTab }
return (
<div className="grid gap-4 xl:grid-cols-[minmax(0,1fr)_22rem]">
<CanvasColumn {...scene} placement={placement} onRoot={setViewBox} />
<aside>
<SidePanel {...scene} viewId={viewId} context={context} />
</aside>
</div>
)
}
/** What both columns read: the document, the tab's state, the view and its frame. */
interface Scene extends Props {
ui: UiState
dispatch: Dispatch<UiAction>
model: ViewModel
frame: Frame | null
selected: Annotation | undefined
}
function CanvasColumn({
placement,
onRoot,
...scene
}: Scene & {
placement: ReturnType<typeof usePlacement>
onRoot: (viewBox: string | null) => void
}) {
const locale = useLocaleStore((s) => s.locale)
const { doc, ui, dispatch, model, frame, selected } = scene
const viewId = ui.viewId ?? mainViewId(doc)
const view = doc.views.find((v) => v.id === viewId)
const noCategory = doc.categories.length === 0
const blocked = !model.measured || noCategory
const select = (id: string) => dispatch({ type: 'select', id })
if (!view) return null
return (
<div className="min-w-0 space-y-3" onKeyDown={placement.onKeyDown}>
<ViewSwitcher
views={doc.views}
current={viewId}
onSelect={(id) => dispatch({ type: 'view', viewId: id })}
/>
<ToolNotes
tool={ui.tool}
hint={ui.hint}
model={model}
noCategory={noCategory}
onSelectTab={scene.onSelectTab}
/>
{selected && (
<SelectionTag
kind={selected.kind}
name={annotationName(selected, doc.views, locale)}
/>
)}
<AnnotationCanvas
viewId={viewId}
viewName={view.name[locale]}
tool={ui.tool}
selectedId={ui.selectedId}
moveTarget={moveTargetOf(selected, model, ui.tool)}
overlay={overlayOf(ui, selected, frame)}
tools={{
tool: ui.tool,
onTool: (tool) => dispatch({ type: 'tool', tool }),
drawingDisabled: blocked,
linkDisabled: model.children.length === 0,
}}
onRoot={onRoot}
onSelect={select}
onPlace={placement.onPlace}
onDrop={placement.onDrop}
/>
{ui.tracing && (
<TracingBar
count={ui.tracing.points.length}
onClose={placement.closeTracing}
onCancel={() => dispatch({ type: 'trace_cancel' })}
/>
)}
<KeyboardAdd
disabled={blocked || frame === null}
linkDisabled={model.children.length === 0}
onAdd={(kind) => addAtCentre(kind, frame, dispatch)}
/>
<AnnotationList
annotations={model.annotations}
views={doc.views}
selectedId={ui.selectedId}
onSelect={select}
/>
</div>
)
}
function addAtCentre(
kind: NewKind,
frame: Frame | null,
dispatch: Dispatch<UiAction>,
) {
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({
viewId,
context,
...scene
}: Scene & { viewId: string; context: CommitContext }) {
const t = useT()
const { doc, ui, dispatch, model, frame, selected } = scene
if (ui.pending) {
return (
<CreationPanel
key={ui.serial}
pending={ui.pending}
viewId={viewId}
doc={doc}
frame={frame}
dispatch={dispatch}
context={context}
/>
)
}
if (!selected) {
return (
<p className="text-sm text-[var(--color-ink-muted)]">
{t('editor.annotations.select_hint')}
</p>
)
}
return (
<PropertiesPanel
key={selected.id}
annotation={selected}
doc={doc}
readOnly={false}
geometryLocked={!model.measured}
dispatch={dispatch}
context={context}
onSelectTab={scene.onSelectTab}
/>
)
}
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,
onSelectTab,
}: {
tool: ToolId
hint: string | null
model: ViewModel
noCategory: boolean
onSelectTab: EditorTabProps['onSelectTab']
}) {
const t = useT()
const linkBlocked = tool === 'link' && model.children.length === 0
return (
<div className="space-y-1 text-sm text-[var(--color-ink-muted)]">
<p>{t(`editor.annotations.tool_hint.${tool}`)}</p>
{!model.measured && <UnmeasuredHint onSelectTab={onSelectTab} />}
{noCategory && <p>{t('editor.annotations.no_category')}</p>}
{linkBlocked && <p>{t('editor.annotations.no_child_view')}</p>}
{hint && (
<p role="alert" className="text-[var(--color-danger-strong)]">
{hint}
</p>
)}
</div>
)
}