Important 1: a touch press fires pointer leave between up and click; only a mouse leave (or a pointer cancel) forgets the refused field, so Annuler no longer undoes an unrelated change. Important 2: the LAN spec reads the revision before the attach, through shownRevision, which throws instead of NaN (spec stays not run). Minor 1: refocusIfHidden refocuses a still shown element the commit blurred (references search on a 1 024 px crossing). Minor 2: discardChanges keeps lastSave (reload path still drops it). Minor 3: the overview filter term lives in UiState / the read-only twin, not the card. Minor 4: categories section drops its inner h3 and the unused editor.general.categories keys. AC8: badge test. Assertions rewritten or added (R-24): - editor-tabs-general « Général no longer holds the categories editor »: queryByText of the deleted heading (now vacuous) -> queryByLabelText of the category name fields. - editor-focus-request « Annotations: selects the annotation »: findAllByText /anno-b|Sélection/ -> the properties panel holds Centre x = 20 (anno-b) and owns the focused heading. - editor-shell-layout panel walk: added after.at(-1) === 'side'. - editor-recovery title « Annuler les modifications » -> « Abandonner ». - new: touch/cancel/mouse press, references search crossing, discard keeps lastSave, filter kept, one categories heading, badge 1/2/0, shownRevision.
359 lines
10 KiB
TypeScript
359 lines
10 KiB
TypeScript
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<UiAction>
|
|
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<UiAction>,
|
|
) {
|
|
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<UiAction>,
|
|
) {
|
|
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<RootAttributes | null>(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 (
|
|
<div className="grid gap-4 lg:grid-cols-[minmax(0,1fr)_22rem]">
|
|
<CanvasColumn scene={scene} placement={placement} />
|
|
<aside>
|
|
<SidePanel scene={scene} context={context} />
|
|
</aside>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
type Placement = ReturnType<typeof usePlacement>
|
|
|
|
function CanvasColumn({
|
|
scene,
|
|
placement,
|
|
}: {
|
|
scene: Scene
|
|
placement: Placement
|
|
}) {
|
|
const { doc, ui, dispatch, model, selected } = scene
|
|
const noCategory = doc.categories.length === 0
|
|
return (
|
|
<div className="min-w-0 space-y-3">
|
|
<ViewSwitcher
|
|
views={doc.views}
|
|
current={scene.viewId}
|
|
onSelect={(id) => dispatch({ type: 'view', viewId: id })}
|
|
/>
|
|
<ToolNotes
|
|
tool={ui.tool}
|
|
hint={ui.hint}
|
|
model={model}
|
|
noCategory={noCategory}
|
|
viewId={scene.viewId}
|
|
navigate={scene.navigate}
|
|
/>
|
|
{selected && <TagOf annotation={selected} doc={doc} />}
|
|
<CanvasStage scene={scene} placement={placement} />
|
|
{ui.tracing && (
|
|
<TracingBar
|
|
count={ui.tracing.points.length}
|
|
onClose={placement.closeTracing}
|
|
onCancel={() => dispatch({ type: 'trace_cancel' })}
|
|
/>
|
|
)}
|
|
<KeyboardAdd
|
|
disabled={!model.measured || noCategory || scene.frame === null}
|
|
linkDisabled={model.children.length === 0}
|
|
onAdd={(kind) => addAtCentre(kind, scene.frame, dispatch)}
|
|
/>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function TagOf({
|
|
annotation,
|
|
doc,
|
|
}: {
|
|
annotation: Annotation
|
|
doc: AnatomicalSheetDocument
|
|
}) {
|
|
const locale = useLocaleStore((s) => s.locale)
|
|
return (
|
|
<SelectionTag
|
|
kind={annotation.kind}
|
|
name={annotationName(annotation, doc.views, locale)}
|
|
/>
|
|
)
|
|
}
|
|
|
|
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 (
|
|
<AnnotationCanvas
|
|
viewId={scene.viewId}
|
|
viewName={view.name[locale]}
|
|
tool={ui.tool}
|
|
selectedId={ui.selectedId}
|
|
moveTarget={shown ? moveTargetOf(selected, model, ui.tool) : null}
|
|
overlay={overlayOf(ui, selected, frame)}
|
|
tools={{
|
|
tool: ui.tool,
|
|
onTool: (tool) => 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<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({
|
|
scene,
|
|
context,
|
|
}: {
|
|
scene: Scene
|
|
context: CommitContext
|
|
}) {
|
|
const { doc, ui, dispatch, model, frame, selected } = scene
|
|
if (ui.pending) {
|
|
return (
|
|
<CreationPanel
|
|
key={ui.serial}
|
|
pending={ui.pending}
|
|
viewId={scene.viewId}
|
|
doc={doc}
|
|
frame={frame}
|
|
dispatch={dispatch}
|
|
context={context}
|
|
/>
|
|
)
|
|
}
|
|
if (!selected) {
|
|
return (
|
|
<OverviewPanel
|
|
doc={doc}
|
|
viewId={scene.viewId}
|
|
readOnly={false}
|
|
filter={ui.filter}
|
|
onFilter={(term) => dispatch({ type: 'filter', term })}
|
|
onSelectView={(id) => dispatch({ type: 'view', viewId: id })}
|
|
onSelectAnnotation={(id) => dispatch({ type: 'select', id })}
|
|
onOpenSection={scene.onOpenSection}
|
|
/>
|
|
)
|
|
}
|
|
return (
|
|
<PropertiesPanel
|
|
key={selected.id}
|
|
annotation={selected}
|
|
doc={doc}
|
|
readOnly={false}
|
|
geometryLocked={!model.measured}
|
|
dispatch={dispatch}
|
|
context={context}
|
|
navigate={scene.navigate}
|
|
onShowAll={() => 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 (
|
|
<div className="space-y-1 text-sm text-[var(--color-ink-muted)]">
|
|
<p>{t(`editor.annotations.tool_hint.${tool}`)}</p>
|
|
{!model.measured && (
|
|
<UnmeasuredHint navigate={navigate} viewId={viewId} />
|
|
)}
|
|
{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>
|
|
)
|
|
}
|