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,208 @@
|
||||
import { useEffect, useRef, type PointerEvent } from 'react'
|
||||
import type { Geometry } from '@faunosteo/core-wasm/types'
|
||||
import Button from '@/components/ui/Button'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import { coreFailureMessage } from '@/core/failure'
|
||||
import { useCoreStopped } from '@/core/runtime'
|
||||
import { transformOf } from '@/catalogue/framing'
|
||||
import { useFraming } from '@/catalogue/useFraming'
|
||||
import { clientToDrawing } from '@/editor/annotations/gesture'
|
||||
import type { XY } from '@/editor/annotations/frame'
|
||||
import {
|
||||
annotationIdOf,
|
||||
useCanvasBuild,
|
||||
type RootAttributes,
|
||||
useSelectionMark,
|
||||
} from '@/editor/annotations/useCanvasBuild'
|
||||
import {
|
||||
useMoveGesture,
|
||||
type MoveTarget,
|
||||
} from '@/editor/annotations/useMoveGesture'
|
||||
import type { ToolId } from '@/editor/annotations/uiState'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import AnnotationOverlay, { type OverlayProps } from './AnnotationOverlay'
|
||||
import ToolColumn, { type ToolColumnProps } from './ToolColumn'
|
||||
|
||||
type OverlayData = Omit<
|
||||
OverlayProps,
|
||||
'svgRef' | 'viewBox' | 'ratio' | 'dragged'
|
||||
>
|
||||
|
||||
export interface AnnotationCanvasProps {
|
||||
viewId: string
|
||||
viewName: string
|
||||
tool: ToolId
|
||||
selectedId: string | null
|
||||
/** The selected annotation when it can be moved now. */
|
||||
moveTarget: MoveTarget | null
|
||||
overlay: OverlayData
|
||||
tools: Omit<ToolColumnProps, 'onZoomIn' | 'onZoomOut' | 'onFit'>
|
||||
/** The composed root's `viewBox` text, as rebuilt; null on failure. */
|
||||
onRoot: (viewBox: string | null) => void
|
||||
onSelect: (id: string) => void
|
||||
/** A click with a drawing tool, in drawing units. */
|
||||
onPlace: (at: XY) => void
|
||||
onDrop: (id: string, geometry: Geometry) => void
|
||||
}
|
||||
|
||||
/**
|
||||
* The edit canvas: the core's composed view, the numbers-only overlay above it in the
|
||||
* same transformed box, the tool column beside it. Select and place work on `click`
|
||||
* (a press that moved is a drag and swallows it); a drag from the selected marker moves
|
||||
* it after 4 CSS px. `touch-action: none` only while a drawing tool or a drag is active.
|
||||
*/
|
||||
export default function AnnotationCanvas(props: AnnotationCanvasProps) {
|
||||
const overlayRef = useRef<SVGSVGElement | null>(null)
|
||||
const viewport = useRef<HTMLDivElement | null>(null)
|
||||
const build = useCanvasBuild(props.viewId)
|
||||
const move = useMoveGesture({
|
||||
overlay: overlayRef,
|
||||
target: props.moveTarget,
|
||||
onDrop: props.onDrop,
|
||||
})
|
||||
const framing = useFraming(viewport, props.viewId, {
|
||||
canPan: (event) => props.tool === 'select' && !move.claims(event),
|
||||
})
|
||||
useSelectionMark(build.host, props.selectedId, build.root)
|
||||
const { onRoot } = props
|
||||
const viewBox = build.root?.viewBox ?? null
|
||||
useReportRoot(viewBox, onRoot)
|
||||
|
||||
const onClick = (event: React.MouseEvent) => {
|
||||
if (framing.swallowClick()) return
|
||||
const id = annotationIdOf(event.target)
|
||||
if (props.tool === 'select') return id ? props.onSelect(id) : undefined
|
||||
const at = clientToDrawing(overlayRef.current, {
|
||||
x: event.clientX,
|
||||
y: event.clientY,
|
||||
})
|
||||
if (at) props.onPlace(at)
|
||||
}
|
||||
const handlers = composed(framing.handlers, move.handlers)
|
||||
const touchNone =
|
||||
props.tool !== 'select' || move.armed || framing.framing.scale !== 1
|
||||
return (
|
||||
<div className="flex flex-col gap-2 lg:flex-row">
|
||||
<ToolColumn
|
||||
{...props.tools}
|
||||
onZoomIn={framing.zoomIn}
|
||||
onZoomOut={framing.zoomOut}
|
||||
onFit={framing.reset}
|
||||
/>
|
||||
<div className="relative min-w-0 flex-1 overflow-hidden rounded-xl border border-[var(--color-border)] bg-white">
|
||||
<div
|
||||
ref={viewport}
|
||||
tabIndex={0}
|
||||
role="group"
|
||||
aria-label={props.viewName}
|
||||
data-testid="annotation-canvas"
|
||||
className={`svg-canvas aspect-[4/3] w-full select-none focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-[var(--color-accent)] ${
|
||||
props.tool === 'select' ? 'cursor-grab' : 'cursor-crosshair'
|
||||
}`}
|
||||
style={{ touchAction: touchNone ? 'none' : 'pan-x pan-y' }}
|
||||
onClick={onClick}
|
||||
{...handlers}
|
||||
>
|
||||
<DrawingLayer
|
||||
host={build.host}
|
||||
root={build.root}
|
||||
transform={transformOf(framing.framing)}
|
||||
overlay={{
|
||||
...props.overlay,
|
||||
svgRef: overlayRef,
|
||||
dragged: move.preview,
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<CanvasFailure failure={build.failure} retry={build.retry} />
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/** The transformed box: the core's drawing and, in the same coordinates, the overlay. */
|
||||
function DrawingLayer({
|
||||
host,
|
||||
root,
|
||||
transform,
|
||||
overlay,
|
||||
}: {
|
||||
host: React.RefObject<HTMLDivElement | null>
|
||||
root: RootAttributes | null
|
||||
transform: string
|
||||
overlay: Omit<OverlayProps, 'viewBox' | 'ratio'>
|
||||
}) {
|
||||
return (
|
||||
<div
|
||||
className="relative h-full w-full origin-top-left"
|
||||
style={{ transform }}
|
||||
>
|
||||
<div
|
||||
ref={host}
|
||||
data-testid="annotation-canvas-drawing"
|
||||
className="h-full w-full [&>svg]:block [&>svg]:h-full [&>svg]:w-full"
|
||||
/>
|
||||
{root?.viewBox && (
|
||||
<AnnotationOverlay
|
||||
{...overlay}
|
||||
viewBox={root.viewBox}
|
||||
ratio={root.ratio}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function useReportRoot(
|
||||
viewBox: string | null,
|
||||
onRoot: (v: string | null) => void,
|
||||
) {
|
||||
useEffect(() => onRoot(viewBox), [viewBox, onRoot])
|
||||
}
|
||||
|
||||
/** Each pointer handler of the framing and of the move gesture, one after the other. */
|
||||
function composed(
|
||||
a: ReturnType<typeof useFraming>['handlers'],
|
||||
b: ReturnType<typeof useMoveGesture>['handlers'],
|
||||
) {
|
||||
const both =
|
||||
(first: (e: PointerEvent) => void, second: (e: PointerEvent) => void) =>
|
||||
(event: PointerEvent) => {
|
||||
first(event)
|
||||
second(event)
|
||||
}
|
||||
return {
|
||||
onPointerDown: both(a.onPointerDown, b.onPointerDown),
|
||||
onPointerMove: both(a.onPointerMove, b.onPointerMove),
|
||||
onPointerUp: both(a.onPointerUp, b.onPointerUp),
|
||||
onPointerCancel: both(a.onPointerCancel, b.onPointerCancel),
|
||||
onLostPointerCapture: b.onLostPointerCapture,
|
||||
onKeyDown: a.onKeyDown,
|
||||
}
|
||||
}
|
||||
|
||||
function CanvasFailure({
|
||||
failure,
|
||||
retry,
|
||||
}: {
|
||||
failure: 'engine' | 'view' | null
|
||||
retry: () => void
|
||||
}) {
|
||||
const t = useT()
|
||||
const stopped = useCoreStopped()
|
||||
if (!failure) return null
|
||||
return (
|
||||
<div className="absolute inset-0 grid place-items-center bg-[var(--color-surface)] p-4">
|
||||
<div className="max-w-sm space-y-3">
|
||||
<Notice tone="error">
|
||||
{failure === 'view'
|
||||
? t('catalogue.failure.view')
|
||||
: coreFailureMessage(t, 'sheet.core_unavailable')}
|
||||
</Notice>
|
||||
{failure === 'engine' && !stopped && (
|
||||
<Button onClick={retry}>{t('sheet.core_retry')}</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
import type {
|
||||
AnatomicalSheetDocument,
|
||||
Annotation,
|
||||
Laterality,
|
||||
} from '@faunosteo/core-wasm/types'
|
||||
import SelectField from '@/components/editor/fields/SelectField'
|
||||
import TextField from '@/components/editor/fields/TextField'
|
||||
import { LATERALITIES, directChildren } from '@/editor/annotations/creation'
|
||||
import { isBlank, nullable } from '@/editor/sheetDocument'
|
||||
import type { EditCommandApi } from '@/editor/useEditCommand'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
|
||||
interface Props {
|
||||
annotation: Annotation
|
||||
doc: AnatomicalSheetDocument
|
||||
readOnly: boolean
|
||||
edit: EditCommandApi
|
||||
}
|
||||
|
||||
/** One `update` carrying the whole annotation with `patch` applied. */
|
||||
function useSend({ annotation, edit }: Props) {
|
||||
return (field: string, patch: Record<string, unknown>) =>
|
||||
edit.run(field, {
|
||||
op: 'update',
|
||||
key: { kind: 'annotation', id: annotation.id },
|
||||
element: {
|
||||
kind: 'annotation',
|
||||
value: { ...annotation, ...patch } as Annotation,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const noneOption = (label: string) => ({ value: '', label })
|
||||
|
||||
/** Names: both or none for a link (its name is optional), both required otherwise. */
|
||||
function NameFields(props: Props) {
|
||||
const t = useT()
|
||||
const send = useSend(props)
|
||||
const { annotation, edit, readOnly } = props
|
||||
const isLink = annotation.kind === 'detail_trigger'
|
||||
const rename = (locale: 'fr' | 'en') => (value: string) => {
|
||||
const name = {
|
||||
fr: annotation.name?.fr ?? '',
|
||||
en: annotation.name?.en ?? '',
|
||||
[locale]: value,
|
||||
}
|
||||
const blanks = [name.fr, name.en].filter(isBlank).length
|
||||
if (isLink && blanks === 2) return send(locale, { name: null })
|
||||
if (blanks > 0) return edit.reject(locale, 'editor.field.required_names')
|
||||
return send(locale, { name })
|
||||
}
|
||||
return (
|
||||
<>
|
||||
{(['fr', 'en'] as const).map((locale) => (
|
||||
<TextField
|
||||
key={locale}
|
||||
id={`annotation-name-${locale}`}
|
||||
label={t(`editor.annotations.name_${locale}`)}
|
||||
value={annotation.name?.[locale] ?? ''}
|
||||
onCommit={rename(locale)}
|
||||
readOnly={readOnly}
|
||||
error={edit.fieldError(locale)}
|
||||
/>
|
||||
))}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default function AnnotationFields(props: Props) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const send = useSend(props)
|
||||
const { annotation, doc, readOnly, edit } = props
|
||||
const categories = doc.categories.map((c) => ({
|
||||
value: c.id,
|
||||
label: c.name[locale],
|
||||
}))
|
||||
const sides = LATERALITIES.map((l: Laterality) => ({
|
||||
value: l,
|
||||
label: t(`editor.annotations.laterality.${l}`),
|
||||
}))
|
||||
return (
|
||||
<div className="grid gap-3 sm:grid-cols-2">
|
||||
<NameFields {...props} />
|
||||
<SelectField
|
||||
id="annotation-category"
|
||||
label={t('editor.annotations.category')}
|
||||
value={annotation.category_id}
|
||||
options={categories}
|
||||
onCommit={(value) => send('category', { category_id: value })}
|
||||
readOnly={readOnly}
|
||||
error={edit.fieldError('category')}
|
||||
/>
|
||||
<TextField
|
||||
id="annotation-color"
|
||||
label={t('editor.annotations.color')}
|
||||
value={annotation.color ?? ''}
|
||||
onCommit={(value) => send('color', { color: nullable(value) })}
|
||||
readOnly={readOnly}
|
||||
hint={t('editor.annotations.color_hint')}
|
||||
error={edit.fieldError('color')}
|
||||
/>
|
||||
<SelectField
|
||||
id="annotation-laterality"
|
||||
label={t('editor.annotations.laterality')}
|
||||
value={annotation.laterality}
|
||||
options={sides}
|
||||
onCommit={(value) => send('laterality', { laterality: value })}
|
||||
readOnly={readOnly}
|
||||
error={edit.fieldError('laterality')}
|
||||
/>
|
||||
{annotation.kind === 'detail_trigger' ? (
|
||||
<OpensField {...props} />
|
||||
) : (
|
||||
<LinkFields {...props} annotation={annotation} />
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function OpensField({ annotation, doc, readOnly, edit }: Props) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const send = useSend({ annotation, doc, readOnly, edit })
|
||||
if (annotation.kind !== 'detail_trigger') return null
|
||||
const options = directChildren(doc.views, annotation.view_id).map((v) => ({
|
||||
value: v.id,
|
||||
label: v.name[locale],
|
||||
}))
|
||||
const current = doc.views.find((v) => v.id === annotation.opens_view_id)
|
||||
if (current && !options.some((o) => o.value === current.id)) {
|
||||
options.push({ value: current.id, label: current.name[locale] })
|
||||
}
|
||||
return (
|
||||
<SelectField
|
||||
id="annotation-opens"
|
||||
label={t('editor.annotations.opens')}
|
||||
value={annotation.opens_view_id}
|
||||
options={options}
|
||||
onCommit={(value) => send('opens', { opens_view_id: value })}
|
||||
readOnly={readOnly}
|
||||
error={edit.fieldError('opens')}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function LinkFields(props: Props) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const send = useSend(props)
|
||||
const { annotation, doc, readOnly, edit } = props
|
||||
if (annotation.kind === 'detail_trigger') return null
|
||||
const regions = [
|
||||
noneOption(t('editor.annotations.none')),
|
||||
...doc.vertebral_config.regions.map((r) => ({
|
||||
value: r.code,
|
||||
label: r.name[locale],
|
||||
})),
|
||||
]
|
||||
const zones = [
|
||||
noneOption(t('editor.annotations.none')),
|
||||
...doc.finding_zones.map((z) => ({ value: z.id, label: z.name[locale] })),
|
||||
]
|
||||
return (
|
||||
<>
|
||||
<SelectField
|
||||
id="annotation-region"
|
||||
label={t('editor.annotations.region')}
|
||||
value={annotation.vertebral_region_code ?? ''}
|
||||
options={regions}
|
||||
onCommit={(value) =>
|
||||
send('region', { vertebral_region_code: nullable(value) })
|
||||
}
|
||||
readOnly={readOnly}
|
||||
error={edit.fieldError('region')}
|
||||
/>
|
||||
<SelectField
|
||||
id="annotation-zone"
|
||||
label={t('editor.annotations.zone')}
|
||||
value={annotation.finding_zone_id ?? ''}
|
||||
options={zones}
|
||||
onCommit={(value) => send('zone', { finding_zone_id: nullable(value) })}
|
||||
readOnly={readOnly}
|
||||
error={edit.fieldError('zone')}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import type { Annotation, SheetView } from '@faunosteo/core-wasm/types'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import ReviewBadge from './ReviewBadge'
|
||||
|
||||
/** The name an annotation shows: its own, else (an unnamed link) the view it opens. */
|
||||
export function annotationName(
|
||||
annotation: Annotation,
|
||||
views: readonly SheetView[],
|
||||
locale: 'fr' | 'en',
|
||||
): string {
|
||||
if (annotation.name) return annotation.name[locale]
|
||||
if (annotation.kind !== 'detail_trigger') return annotation.id
|
||||
const opened = views.find((view) => view.id === annotation.opens_view_id)
|
||||
return opened?.name[locale] ?? annotation.id
|
||||
}
|
||||
|
||||
/**
|
||||
* The annotations of the view: name, kind, review badge; each row a button, reachable
|
||||
* from the keyboard. Selection works in every mode, so a read-only draft is browsable.
|
||||
*/
|
||||
export default function AnnotationList({
|
||||
annotations,
|
||||
views,
|
||||
selectedId,
|
||||
onSelect,
|
||||
}: {
|
||||
annotations: readonly Annotation[]
|
||||
views: readonly SheetView[]
|
||||
selectedId: string | null
|
||||
onSelect: (id: string) => void
|
||||
}) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
if (annotations.length === 0) {
|
||||
return (
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t('editor.annotations.list_empty')}
|
||||
</p>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<ul
|
||||
aria-label={t('editor.annotations.list')}
|
||||
className="max-h-64 divide-y divide-[var(--color-border)] overflow-y-auto rounded-lg border border-[var(--color-border)]"
|
||||
>
|
||||
{annotations.map((annotation) => (
|
||||
<li key={annotation.id}>
|
||||
<button
|
||||
type="button"
|
||||
aria-current={annotation.id === selectedId}
|
||||
onClick={() => onSelect(annotation.id)}
|
||||
className={`flex w-full items-center justify-between gap-2 px-3 py-2 text-left text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-[var(--color-accent)] ${
|
||||
annotation.id === selectedId
|
||||
? 'bg-[var(--color-surface-strong)]'
|
||||
: ''
|
||||
}`}
|
||||
>
|
||||
<span className="min-w-0 truncate">
|
||||
<span className="text-[var(--color-ink-muted)]">
|
||||
{t(`editor.annotations.kind.${annotation.kind}`)}
|
||||
</span>{' '}
|
||||
{annotationName(annotation, views, locale)}
|
||||
</span>
|
||||
<ReviewBadge status={annotation.review_status} />
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
import type { Ref } from 'react'
|
||||
import type { Geometry, Point } from '@faunosteo/core-wasm/types'
|
||||
import type { Correction } from '@/editor/annotations/correction'
|
||||
import type { Frame } from '@/editor/annotations/frame'
|
||||
import { isEditable } from '@/editor/annotations/geometryEdit'
|
||||
|
||||
export interface OverlayProps {
|
||||
svgRef: Ref<SVGSVGElement>
|
||||
/** The composed root's own attributes: the overlay shares its coordinates. */
|
||||
viewBox: string
|
||||
ratio: string
|
||||
frame: Frame | null
|
||||
selected: Geometry | null
|
||||
dragged: Geometry | null
|
||||
pending: Geometry | null
|
||||
tracing: Point[] | null
|
||||
correction: Correction | null
|
||||
}
|
||||
|
||||
const STROKE = { fill: 'none', vectorEffect: 'non-scaling-stroke' } as const
|
||||
|
||||
function Outline({
|
||||
geometry,
|
||||
dashed,
|
||||
colour,
|
||||
}: {
|
||||
geometry: Geometry
|
||||
dashed: boolean
|
||||
colour: string
|
||||
}) {
|
||||
const style = {
|
||||
...STROKE,
|
||||
stroke: colour,
|
||||
strokeWidth: 2,
|
||||
strokeDasharray: dashed ? '6 4' : undefined,
|
||||
}
|
||||
if (geometry.type === 'circle') {
|
||||
return (
|
||||
<circle cx={geometry.cx} cy={geometry.cy} r={geometry.r} {...style} />
|
||||
)
|
||||
}
|
||||
if (geometry.type === 'polygon') {
|
||||
return <polygon points={geometry.points.join(' ')} {...style} />
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
function Vertices({ geometry, size }: { geometry: Geometry; size: number }) {
|
||||
if (geometry.type !== 'polygon') return null
|
||||
return (
|
||||
<>
|
||||
{geometry.points.map(([x, y], index) => (
|
||||
<circle
|
||||
key={index}
|
||||
cx={x}
|
||||
cy={y}
|
||||
r={size}
|
||||
fill="white"
|
||||
stroke="var(--color-ink)"
|
||||
strokeWidth={1.5}
|
||||
vectorEffect="non-scaling-stroke"
|
||||
/>
|
||||
))}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
/** The hollow cross of the position the user chose, drawn apart from the proposal. */
|
||||
function HollowCross({
|
||||
at,
|
||||
size,
|
||||
}: {
|
||||
at: { x: number; y: number }
|
||||
size: number
|
||||
}) {
|
||||
const style = { ...STROKE, stroke: 'var(--color-ink)', strokeWidth: 2 }
|
||||
return (
|
||||
<g data-testid="overlay-click-cross">
|
||||
<path
|
||||
d={`M ${at.x - size} ${at.y - size} L ${at.x + size} ${at.y + size}`}
|
||||
{...style}
|
||||
/>
|
||||
<path
|
||||
d={`M ${at.x - size} ${at.y + size} L ${at.x + size} ${at.y - size}`}
|
||||
{...style}
|
||||
/>
|
||||
</g>
|
||||
)
|
||||
}
|
||||
|
||||
function TracingPath({ points, size }: { points: Point[]; size: number }) {
|
||||
if (points.length === 0) return null
|
||||
const shape: Geometry = { type: 'polygon', points }
|
||||
return (
|
||||
<>
|
||||
<polyline
|
||||
points={points.join(' ')}
|
||||
{...STROKE}
|
||||
stroke="var(--color-accent)"
|
||||
strokeWidth={2}
|
||||
strokeDasharray="6 4"
|
||||
/>
|
||||
<Vertices geometry={shape} size={size} />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* The page-drawn overlay (docs/ANNOTATION-RENDERING.md « Editor overlay »): numbers only,
|
||||
* no annotation. Selection outline, vertex circles, the tracing in progress, the pending
|
||||
* or dragged outline, a corrected position with the click marked apart. It never takes
|
||||
* the pointer: the canvas box does.
|
||||
*/
|
||||
export default function AnnotationOverlay(props: OverlayProps) {
|
||||
const { frame, selected, dragged, pending, tracing, correction } = props
|
||||
const size = frame ? Math.max(frame.w, frame.h) / 200 : 1
|
||||
const shown = dragged ?? selected
|
||||
return (
|
||||
<svg
|
||||
ref={props.svgRef}
|
||||
aria-hidden="true"
|
||||
data-testid="annotation-overlay"
|
||||
viewBox={props.viewBox}
|
||||
preserveAspectRatio={props.ratio}
|
||||
className="pointer-events-none absolute inset-0 h-full w-full"
|
||||
>
|
||||
{shown && isEditable(shown) && (
|
||||
<>
|
||||
<Outline geometry={shown} dashed colour="var(--color-ink)" />
|
||||
<Vertices geometry={shown} size={size} />
|
||||
</>
|
||||
)}
|
||||
{pending && (
|
||||
<Outline
|
||||
geometry={pending}
|
||||
dashed={false}
|
||||
colour="var(--color-accent)"
|
||||
/>
|
||||
)}
|
||||
{correction && (
|
||||
<>
|
||||
<Outline
|
||||
geometry={{ type: 'circle', ...correction.circle }}
|
||||
dashed
|
||||
colour="var(--color-accent)"
|
||||
/>
|
||||
<HollowCross at={correction.click} size={size * 1.5} />
|
||||
</>
|
||||
)}
|
||||
{tracing && <TracingPath points={tracing} size={size} />}
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import Button from '@/components/ui/Button'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import type { Correction } from '@/editor/annotations/correction'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/**
|
||||
* The corrected position is never taken silently: the page says where the point would
|
||||
* sit, the overlay shows it with the click marked apart, and only « Utiliser cette
|
||||
* position » inserts it.
|
||||
*/
|
||||
export default function CorrectionNotice({
|
||||
correction,
|
||||
onConfirm,
|
||||
onCancel,
|
||||
}: {
|
||||
correction: Correction
|
||||
onConfirm: () => void
|
||||
onCancel: () => void
|
||||
}) {
|
||||
const t = useT()
|
||||
const message = t('editor.annotations.correction')
|
||||
.replace('{x}', String(correction.circle.cx))
|
||||
.replace('{y}', String(correction.circle.cy))
|
||||
return (
|
||||
<Notice tone="info">
|
||||
<div className="space-y-2" data-testid="correction-notice">
|
||||
<p>{message}</p>
|
||||
<div className="flex gap-2">
|
||||
<Button type="button" size="sm" onClick={onConfirm}>
|
||||
{t('editor.annotations.correction_use')}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
variant="secondary"
|
||||
onClick={onCancel}
|
||||
>
|
||||
{t('editor.annotations.correction_cancel')}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Notice>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,342 @@
|
||||
import { useState, type Dispatch } from 'react'
|
||||
import type {
|
||||
AnatomicalSheetDocument,
|
||||
Laterality,
|
||||
} from '@faunosteo/core-wasm/types'
|
||||
import DraftField from '@/components/editor/fields/DraftField'
|
||||
import FormField from '@/components/ui/FormField'
|
||||
import Button from '@/components/ui/Button'
|
||||
import Select from '@/components/ui/Select'
|
||||
import {
|
||||
insertAnnotation,
|
||||
type CommitContext,
|
||||
type CommitResult,
|
||||
} from '@/editor/annotations/annotationCommit'
|
||||
import {
|
||||
attemptPoint,
|
||||
confirmCorrection,
|
||||
type Correction,
|
||||
type PointAttempt,
|
||||
} from '@/editor/annotations/correction'
|
||||
import {
|
||||
LATERALITIES,
|
||||
annotationOf,
|
||||
directChildren,
|
||||
emptyForm,
|
||||
formProblem,
|
||||
type CreationForm,
|
||||
type PendingElement,
|
||||
} from '@/editor/annotations/creation'
|
||||
import type { Frame } from '@/editor/annotations/frame'
|
||||
import type { EditableGeometry } from '@/editor/annotations/geometryEdit'
|
||||
import type { UiAction } from '@/editor/annotations/uiState'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import CorrectionNotice from './CorrectionNotice'
|
||||
import GeometryGroup from './GeometryGroup'
|
||||
|
||||
export interface CreationPanelProps {
|
||||
pending: PendingElement
|
||||
viewId: string
|
||||
doc: AnatomicalSheetDocument
|
||||
/** F of the current view; null while it is not measured. */
|
||||
frame: Frame | null
|
||||
dispatch: Dispatch<UiAction>
|
||||
context: CommitContext
|
||||
}
|
||||
|
||||
type Attempt = PointAttempt | { status: 'invalid'; messages: string[] }
|
||||
|
||||
/** The insert of this panel's element under a given geometry, through `accept`. */
|
||||
function inserterOf(props: CreationPanelProps, form: CreationForm) {
|
||||
return (geometry: EditableGeometry): CommitResult =>
|
||||
insertAnnotation(
|
||||
(id) =>
|
||||
annotationOf(id, props.viewId, { ...props.pending, geometry }, form),
|
||||
props.context,
|
||||
)
|
||||
}
|
||||
|
||||
/** « Créer »: a point lets the core's verdict offer C; nothing else is ever invented. */
|
||||
function createAttempt(
|
||||
props: CreationPanelProps,
|
||||
form: CreationForm,
|
||||
words: (problem: string) => string,
|
||||
): Attempt {
|
||||
const { pending, frame } = props
|
||||
const problem = formProblem(pending.kind, form)
|
||||
if (problem) return { status: 'invalid', messages: [words(problem)] }
|
||||
const insert = inserterOf(props, form)
|
||||
const { geometry } = pending
|
||||
if (pending.kind === 'point' && geometry.type === 'circle' && frame) {
|
||||
return attemptPoint(frame, geometry, (c) =>
|
||||
insert({ type: 'circle', ...c }),
|
||||
)
|
||||
}
|
||||
const result = insert(geometry)
|
||||
return result.ok
|
||||
? { status: 'created', id: result.id }
|
||||
: { status: 'refused', messages: result.messages }
|
||||
}
|
||||
|
||||
function confirmAttempt(
|
||||
props: CreationPanelProps,
|
||||
form: CreationForm,
|
||||
correction: Correction,
|
||||
): Attempt {
|
||||
const insert = inserterOf(props, form)
|
||||
return confirmCorrection(correction, (c) => insert({ type: 'circle', ...c }))
|
||||
}
|
||||
|
||||
/**
|
||||
* « Nouveau point / Nouvelle zone / Nouvel accès »: the fields the core requires are
|
||||
* collected first, then « Créer » inserts once through `accept` (no default labels).
|
||||
* The geometry stays here, outside the document, until then.
|
||||
*/
|
||||
export default function CreationPanel(props: CreationPanelProps) {
|
||||
const t = useT()
|
||||
const { pending, doc, dispatch } = props
|
||||
const [form, setForm] = useState<CreationForm>(() => emptyForm(doc))
|
||||
const [messages, setMessages] = useState<string[]>([])
|
||||
const update = (next: PendingElement) =>
|
||||
dispatch({ type: 'pending_update', pending: next })
|
||||
const settle = (attempt: Attempt) => {
|
||||
setMessages(
|
||||
attempt.status === 'refused' || attempt.status === 'invalid'
|
||||
? attempt.messages
|
||||
: [],
|
||||
)
|
||||
if (attempt.status === 'created') {
|
||||
dispatch({ type: 'select', id: attempt.id ?? null })
|
||||
}
|
||||
if (attempt.status === 'correction') {
|
||||
update({ ...pending, correction: attempt.correction })
|
||||
}
|
||||
}
|
||||
const words = (problem: string) => t(`editor.annotations.required.${problem}`)
|
||||
const setGeometry = (geometry: EditableGeometry) => {
|
||||
update({ ...pending, geometry, correction: null })
|
||||
return { ok: true as const }
|
||||
}
|
||||
return (
|
||||
<section
|
||||
aria-label={t(`editor.annotations.new.${pending.kind}`)}
|
||||
data-testid="annotation-creation"
|
||||
className="space-y-3 rounded-xl border border-[var(--color-border)] p-4"
|
||||
>
|
||||
<h3 className="text-base font-semibold">
|
||||
{t(`editor.annotations.new.${pending.kind}`)}
|
||||
</h3>
|
||||
<CreationFields
|
||||
kind={pending.kind}
|
||||
viewId={props.viewId}
|
||||
form={form}
|
||||
doc={doc}
|
||||
onChange={(patch) => setForm({ ...form, ...patch })}
|
||||
/>
|
||||
<GeometryGroup
|
||||
idPrefix="creation"
|
||||
geometry={pending.geometry}
|
||||
onApply={setGeometry}
|
||||
locked={false}
|
||||
/>
|
||||
{pending.correction && (
|
||||
<CorrectionNotice
|
||||
correction={pending.correction}
|
||||
onConfirm={() =>
|
||||
settle(confirmAttempt(props, form, pending.correction!))
|
||||
}
|
||||
onCancel={() => update({ ...pending, correction: null })}
|
||||
/>
|
||||
)}
|
||||
<Messages messages={messages} />
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
type="button"
|
||||
onClick={() => settle(createAttempt(props, form, words))}
|
||||
>
|
||||
{t('editor.annotations.create')}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="secondary"
|
||||
onClick={() => dispatch({ type: 'pending_drop' })}
|
||||
>
|
||||
{t('editor.annotations.create_cancel')}
|
||||
</Button>
|
||||
</div>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
function Messages({ messages }: { messages: string[] }) {
|
||||
if (messages.length === 0) return null
|
||||
return (
|
||||
<div
|
||||
role="alert"
|
||||
className="space-y-1 text-sm text-[var(--color-danger-strong)]"
|
||||
>
|
||||
{messages.map((message) => (
|
||||
<p key={message}>{message}</p>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
interface FieldsProps {
|
||||
doc: AnatomicalSheetDocument
|
||||
form: CreationForm
|
||||
onChange: (patch: Partial<CreationForm>) => void
|
||||
}
|
||||
|
||||
interface Option {
|
||||
value: string
|
||||
label: string
|
||||
}
|
||||
|
||||
function OptionSelect({
|
||||
id,
|
||||
label,
|
||||
value,
|
||||
options,
|
||||
blank,
|
||||
onChange,
|
||||
}: {
|
||||
id: string
|
||||
label: string
|
||||
value: string
|
||||
options: Option[]
|
||||
/** A « Choisir… » entry: the choice is required and has no default. */
|
||||
blank: boolean
|
||||
onChange: (value: string) => void
|
||||
}) {
|
||||
const t = useT()
|
||||
return (
|
||||
<FormField id={id} label={label}>
|
||||
<Select id={id} value={value} onChange={(e) => onChange(e.target.value)}>
|
||||
{blank && <option value="">{t('editor.field.choose')}</option>}
|
||||
{options.map((option) => (
|
||||
<option key={option.value} value={option.value}>
|
||||
{option.label}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
</FormField>
|
||||
)
|
||||
}
|
||||
|
||||
/** The fields the core requires, collected before the insert. */
|
||||
function CreationFields({
|
||||
kind,
|
||||
viewId,
|
||||
...fields
|
||||
}: FieldsProps & { kind: PendingElement['kind']; viewId: string }) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const { doc, form, onChange } = fields
|
||||
const categories = doc.categories.map((c) => ({
|
||||
value: c.id,
|
||||
label: c.name[locale],
|
||||
}))
|
||||
const sides = LATERALITIES.map((l: Laterality) => ({
|
||||
value: l,
|
||||
label: t(`editor.annotations.laterality.${l}`),
|
||||
}))
|
||||
return (
|
||||
<div className="grid gap-3 sm:grid-cols-2">
|
||||
<DraftField
|
||||
id="creation-fr"
|
||||
label={t('editor.annotations.name_fr')}
|
||||
value={form.fr}
|
||||
onChange={(fr) => onChange({ fr })}
|
||||
/>
|
||||
<DraftField
|
||||
id="creation-en"
|
||||
label={t('editor.annotations.name_en')}
|
||||
value={form.en}
|
||||
onChange={(en) => onChange({ en })}
|
||||
/>
|
||||
<OptionSelect
|
||||
id="creation-category"
|
||||
label={t('editor.annotations.category')}
|
||||
value={form.categoryId}
|
||||
options={categories}
|
||||
blank={false}
|
||||
onChange={(categoryId) => onChange({ categoryId })}
|
||||
/>
|
||||
<OptionSelect
|
||||
id="creation-laterality"
|
||||
label={t('editor.annotations.laterality')}
|
||||
value={form.laterality}
|
||||
options={sides}
|
||||
blank
|
||||
onChange={(laterality) =>
|
||||
onChange({ laterality: laterality as CreationForm['laterality'] })
|
||||
}
|
||||
/>
|
||||
{kind === 'detail_trigger' ? (
|
||||
<OpensSelect {...fields} viewId={viewId} />
|
||||
) : (
|
||||
<LinkSelects {...fields} />
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function OpensSelect({
|
||||
doc,
|
||||
form,
|
||||
onChange,
|
||||
viewId,
|
||||
}: FieldsProps & { viewId: string }) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const options = directChildren(doc.views, viewId).map((v) => ({
|
||||
value: v.id,
|
||||
label: v.name[locale],
|
||||
}))
|
||||
return (
|
||||
<OptionSelect
|
||||
id="creation-opens"
|
||||
label={t('editor.annotations.opens')}
|
||||
value={form.opensViewId}
|
||||
options={options}
|
||||
blank
|
||||
onChange={(opensViewId) => onChange({ opensViewId })}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
/** Findings zone and vertebral region: optional links of a point or a zone. */
|
||||
function LinkSelects({ doc, form, onChange }: FieldsProps) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const zones = doc.finding_zones.map((z) => ({
|
||||
value: z.id,
|
||||
label: z.name[locale],
|
||||
}))
|
||||
const regions = doc.vertebral_config.regions.map((r) => ({
|
||||
value: r.code,
|
||||
label: r.name[locale],
|
||||
}))
|
||||
return (
|
||||
<>
|
||||
<OptionSelect
|
||||
id="creation-zone"
|
||||
label={t('editor.annotations.zone')}
|
||||
value={form.zoneId}
|
||||
options={zones}
|
||||
blank
|
||||
onChange={(zoneId) => onChange({ zoneId })}
|
||||
/>
|
||||
<OptionSelect
|
||||
id="creation-region"
|
||||
label={t('editor.annotations.region')}
|
||||
value={form.regionCode}
|
||||
options={regions}
|
||||
blank
|
||||
onChange={(regionCode) => onChange({ regionCode })}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -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>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
import { useState } from 'react'
|
||||
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
|
||||
import Button from '@/components/ui/Button'
|
||||
import Input from '@/components/ui/Input'
|
||||
import {
|
||||
changedFieldNames,
|
||||
fieldsOf,
|
||||
parseFields,
|
||||
sameAsStored,
|
||||
withVertexAfter,
|
||||
withoutVertex,
|
||||
type EditableGeometry,
|
||||
type GroupFields,
|
||||
} from '@/editor/annotations/geometryEdit'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/** What applying the group answers: kept, or the words that say why not. */
|
||||
export type ApplyResult = { ok: true } | { ok: false; messages: string[] }
|
||||
|
||||
export interface GeometryGroupProps {
|
||||
idPrefix: string
|
||||
/** The stored (or pending) geometry the fields start from. */
|
||||
geometry: EditableGeometry
|
||||
/** Proposes the WHOLE group; the core's verdict comes back as the result. */
|
||||
onApply: (geometry: EditableGeometry) => ApplyResult
|
||||
/** Fields shown as text: read-only mode or an unmeasured view. */
|
||||
locked: boolean
|
||||
}
|
||||
|
||||
type Fields = GroupFields
|
||||
type PolygonFields = Extract<GroupFields, { type: 'polygon' }>
|
||||
|
||||
/**
|
||||
* The geometry fields as ONE group held in component state (plan « Repair through a
|
||||
* temporary form »): Enter or leaving a field proposes the whole group as one update;
|
||||
* a refusal keeps the typed values marked « non appliqué » with the core's message and
|
||||
* the document unchanged. Selecting another element unmounts it: an unapplied group was
|
||||
* never in the document and is dropped (no commit on unmount).
|
||||
*/
|
||||
export default function GeometryGroup(props: GeometryGroupProps) {
|
||||
const stored = fieldsOf(props.geometry)
|
||||
const storedKey = JSON.stringify(stored)
|
||||
const [draft, setDraft] = useState<Fields>(stored)
|
||||
const [seen, setSeen] = useState(storedKey)
|
||||
const [messages, setMessages] = useState<string[]>([])
|
||||
// A new stored value (undo, restore, an accepted move) replaces what was typed.
|
||||
if (seen !== storedKey) {
|
||||
setSeen(storedKey)
|
||||
setDraft(stored)
|
||||
setMessages([])
|
||||
}
|
||||
const apply = (next: Fields) => {
|
||||
setDraft(next)
|
||||
if (sameAsStored(next, props.geometry)) return setMessages([])
|
||||
const parsed = parseFields(next)
|
||||
if (!parsed.ok) return setMessages(['number'])
|
||||
const result = props.onApply(parsed.geometry)
|
||||
setMessages(result.ok ? [] : result.messages)
|
||||
}
|
||||
const revert = () => {
|
||||
setDraft(stored)
|
||||
setMessages([])
|
||||
}
|
||||
const pending = changedFieldNames(draft, props.geometry)
|
||||
const view = { ...props, draft, pending, messages }
|
||||
return (
|
||||
<GroupBody
|
||||
{...view}
|
||||
onChange={setDraft}
|
||||
onPropose={apply}
|
||||
onRevert={revert}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
interface BodyProps extends Omit<GeometryGroupProps, 'onApply'> {
|
||||
draft: Fields
|
||||
pending: Set<string>
|
||||
messages: string[]
|
||||
onChange: (fields: Fields) => void
|
||||
onPropose: (fields: Fields) => void
|
||||
onRevert: () => void
|
||||
}
|
||||
|
||||
function GroupBody(props: BodyProps) {
|
||||
const t = useT()
|
||||
const { draft, pending, messages } = props
|
||||
const unapplied = pending.size > 0 && messages.length > 0
|
||||
return (
|
||||
<fieldset className="space-y-2">
|
||||
<legend className="text-sm font-medium">
|
||||
{t(
|
||||
draft.type === 'circle'
|
||||
? 'editor.annotations.geometry.circle'
|
||||
: 'editor.annotations.geometry.polygon',
|
||||
)}
|
||||
</legend>
|
||||
{draft.type === 'circle' ? (
|
||||
<CircleRows {...props} fields={draft} />
|
||||
) : (
|
||||
<PolygonRows {...props} fields={draft} />
|
||||
)}
|
||||
{unapplied && <Unapplied {...props} />}
|
||||
</fieldset>
|
||||
)
|
||||
}
|
||||
|
||||
function Unapplied({ messages, onRevert }: BodyProps) {
|
||||
const t = useT()
|
||||
const words = messages.map((m) =>
|
||||
m === 'number' ? t('editor.annotations.geometry.number') : m,
|
||||
)
|
||||
return (
|
||||
<div role="alert" className="space-y-1 text-sm">
|
||||
<p className="font-medium text-[var(--color-danger-strong)]">
|
||||
{t('editor.annotations.geometry.unapplied')}
|
||||
</p>
|
||||
{words.map((word) => (
|
||||
<p key={word} className="text-[var(--color-danger-strong)]">
|
||||
{word}
|
||||
</p>
|
||||
))}
|
||||
<Button type="button" variant="secondary" size="sm" onClick={onRevert}>
|
||||
{t('editor.annotations.geometry.revert')}
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function NumberInput({
|
||||
id,
|
||||
label,
|
||||
value,
|
||||
unapplied,
|
||||
locked,
|
||||
onType,
|
||||
onApply,
|
||||
}: {
|
||||
id: string
|
||||
label: string
|
||||
value: string
|
||||
unapplied: boolean
|
||||
locked: boolean
|
||||
onType: (value: string) => void
|
||||
onApply: () => void
|
||||
}) {
|
||||
const t = useT()
|
||||
if (locked) {
|
||||
return (
|
||||
<p className="text-sm">
|
||||
<span className="font-medium">{label}</span> {value}
|
||||
</p>
|
||||
)
|
||||
}
|
||||
const hint = unapplied
|
||||
? t('editor.annotations.geometry.unapplied_tag')
|
||||
: undefined
|
||||
return (
|
||||
<FormField id={id} label={label} hint={hint}>
|
||||
<Input
|
||||
id={id}
|
||||
inputMode="decimal"
|
||||
value={value}
|
||||
aria-invalid={unapplied}
|
||||
aria-describedby={fieldDescribedBy(id, hint)}
|
||||
onChange={(e) => onType(e.target.value)}
|
||||
onBlur={onApply}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter' && !e.nativeEvent.isComposing) onApply()
|
||||
}}
|
||||
/>
|
||||
</FormField>
|
||||
)
|
||||
}
|
||||
|
||||
type RowsProps<F extends Fields> = BodyProps & { fields: F }
|
||||
|
||||
function CircleRows(props: RowsProps<Extract<Fields, { type: 'circle' }>>) {
|
||||
const t = useT()
|
||||
const { fields, pending, locked, idPrefix } = props
|
||||
return (
|
||||
<div className="grid grid-cols-3 gap-2">
|
||||
{(['cx', 'cy', 'r'] as const).map((key) => (
|
||||
<NumberInput
|
||||
key={key}
|
||||
id={`${idPrefix}-${key}`}
|
||||
label={t(`editor.annotations.geometry.${key}`)}
|
||||
value={fields[key]}
|
||||
unapplied={pending.has(key)}
|
||||
locked={locked}
|
||||
onType={(value) => props.onChange({ ...fields, [key]: value })}
|
||||
onApply={() => props.onPropose(fields)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function PolygonRows(props: RowsProps<PolygonFields>) {
|
||||
const t = useT()
|
||||
const [selected, setSelected] = useState(0)
|
||||
const { fields, locked } = props
|
||||
const change = (index: number, axis: 'x' | 'y', value: string) =>
|
||||
props.onChange({
|
||||
type: 'polygon',
|
||||
rows: fields.rows.map((row, i) =>
|
||||
i === index ? { ...row, [axis]: value } : row,
|
||||
),
|
||||
})
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t('editor.annotations.geometry.vertices').replace(
|
||||
'{count}',
|
||||
String(fields.rows.length),
|
||||
)}
|
||||
</p>
|
||||
{fields.rows.map((row, index) => (
|
||||
<div
|
||||
key={index}
|
||||
className="grid grid-cols-[2rem_1fr_1fr] items-end gap-2"
|
||||
onFocus={() => setSelected(index)}
|
||||
>
|
||||
<span className="pb-2 text-sm">{index + 1}</span>
|
||||
{(['x', 'y'] as const).map((axis) => (
|
||||
<NumberInput
|
||||
key={axis}
|
||||
id={`${props.idPrefix}-${axis}${index}`}
|
||||
label={t(`editor.annotations.geometry.vertex_${axis}`).replace(
|
||||
'{n}',
|
||||
String(index + 1),
|
||||
)}
|
||||
value={row[axis]}
|
||||
unapplied={props.pending.has(`${axis}${index}`)}
|
||||
locked={locked}
|
||||
onType={(value) => change(index, axis, value)}
|
||||
onApply={() => props.onPropose(fields)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
))}
|
||||
{!locked && (
|
||||
<VertexButtons
|
||||
fields={fields}
|
||||
selected={selected}
|
||||
onApply={props.onPropose}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function VertexButtons({
|
||||
fields,
|
||||
selected,
|
||||
onApply,
|
||||
}: {
|
||||
fields: PolygonFields
|
||||
selected: number
|
||||
onApply: (fields: Fields) => void
|
||||
}) {
|
||||
const t = useT()
|
||||
return (
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Button
|
||||
type="button"
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onClick={() => onApply(withVertexAfter(fields, selected))}
|
||||
>
|
||||
{t('editor.annotations.geometry.add_vertex')}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
disabled={fields.rows.length <= 3}
|
||||
onClick={() => onApply(withoutVertex(fields, selected))}
|
||||
>
|
||||
{t('editor.action.remove')}
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import Button from '@/components/ui/Button'
|
||||
import type { NewKind } from '@/editor/annotations/creation'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
const KINDS: readonly NewKind[] = ['point', 'zone', 'detail_trigger']
|
||||
|
||||
/**
|
||||
* The keyboard creation path (C7): each button holds a pending element at the centre of
|
||||
* the frame and opens the creation panel; every coordinate is then typed in the fields.
|
||||
*/
|
||||
export default function KeyboardAdd({
|
||||
disabled,
|
||||
linkDisabled,
|
||||
onAdd,
|
||||
}: {
|
||||
/** No measured frame, or no category: nothing can be placed. */
|
||||
disabled: boolean
|
||||
/** No detail view directly under this view: no link can be placed. */
|
||||
linkDisabled: boolean
|
||||
onAdd: (kind: NewKind) => void
|
||||
}) {
|
||||
const t = useT()
|
||||
return (
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{KINDS.map((kind) => (
|
||||
<Button
|
||||
key={kind}
|
||||
type="button"
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
disabled={disabled || (kind === 'detail_trigger' && linkDisabled)}
|
||||
onClick={() => onAdd(kind)}
|
||||
>
|
||||
{t(`editor.annotations.add.${kind}`)}
|
||||
</Button>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
import { useState, type Dispatch } from 'react'
|
||||
import type {
|
||||
AnatomicalSheetDocument,
|
||||
Annotation,
|
||||
Geometry,
|
||||
Marking,
|
||||
} from '@faunosteo/core-wasm/types'
|
||||
import ActionButton from '@/components/editor/fields/ActionButton'
|
||||
import DeleteDialog from '@/components/editor/DeleteDialog'
|
||||
import Button from '@/components/ui/Button'
|
||||
import {
|
||||
updateGeometry,
|
||||
type CommitContext,
|
||||
} from '@/editor/annotations/annotationCommit'
|
||||
import { isEditable } from '@/editor/annotations/geometryEdit'
|
||||
import type { UiAction } from '@/editor/annotations/uiState'
|
||||
import { geometryErrorsById } from '@/editor/issues'
|
||||
import type { EditorTabId } from '@/editor/tabs'
|
||||
import { useEditCommand } from '@/editor/useEditCommand'
|
||||
import { useEditorStore } from '@/editor/state/editorStore'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import AnnotationFields from './AnnotationFields'
|
||||
import type { ApplyResult } from './GeometryGroup'
|
||||
import { annotationName } from './AnnotationList'
|
||||
import GeometryGroup from './GeometryGroup'
|
||||
import ReviewBadge from './ReviewBadge'
|
||||
|
||||
export interface PropertiesPanelProps {
|
||||
annotation: Annotation
|
||||
doc: AnatomicalSheetDocument
|
||||
/** Not editable now: every field shows as text and no action is offered. */
|
||||
readOnly: boolean
|
||||
/** The view is not measured: geometry is shown as text only, the rest stays editable. */
|
||||
geometryLocked: boolean
|
||||
dispatch: Dispatch<UiAction>
|
||||
context: CommitContext
|
||||
onSelectTab: (tab: EditorTabId) => void
|
||||
}
|
||||
|
||||
/** What still names this annotation as invalid: never hidden, never shown as valid. */
|
||||
function useGeometryIssues(id: string): string[] {
|
||||
const state = useEditorStore((s) => s.previewState)
|
||||
const issues = state ? geometryErrorsById(state).get(id) : undefined
|
||||
return [...new Set((issues ?? []).map((issue) => issue.message))]
|
||||
}
|
||||
|
||||
export default function PropertiesPanel(props: PropertiesPanelProps) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const { annotation, doc, readOnly } = props
|
||||
const edit = useEditCommand()
|
||||
const name = annotationName(annotation, doc.views, locale)
|
||||
return (
|
||||
<section
|
||||
aria-label={t('editor.annotations.properties')}
|
||||
data-testid="annotation-properties"
|
||||
className="space-y-4 rounded-xl border border-[var(--color-border)] p-4"
|
||||
>
|
||||
<header className="flex items-start justify-between gap-2">
|
||||
<h3 className="text-base font-semibold">
|
||||
{t(`editor.annotations.kind.${annotation.kind}`)}
|
||||
</h3>
|
||||
<ReviewBadge status={annotation.review_status} />
|
||||
</header>
|
||||
<AnnotationFields
|
||||
annotation={annotation}
|
||||
doc={doc}
|
||||
readOnly={readOnly}
|
||||
edit={edit}
|
||||
/>
|
||||
<GeometrySection {...props} />
|
||||
<StatusActions annotation={annotation} readOnly={readOnly} edit={edit} />
|
||||
<Deletion id={annotation.id} name={name} readOnly={readOnly} />
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
function GeometrySection(props: PropertiesPanelProps) {
|
||||
const t = useT()
|
||||
const { annotation, readOnly, geometryLocked } = props
|
||||
const locked = readOnly || geometryLocked
|
||||
const { geometry } = annotation
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
{geometryLocked && !readOnly && <UnmeasuredHint {...props} />}
|
||||
{isEditable(geometry) ? (
|
||||
<GeometryGroup
|
||||
key={annotation.id}
|
||||
idPrefix="annotation"
|
||||
geometry={geometry}
|
||||
locked={locked}
|
||||
onApply={(next) => applyGeometry(annotation, next, props.context)}
|
||||
/>
|
||||
) : (
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t(
|
||||
geometry === null
|
||||
? 'editor.annotations.no_geometry'
|
||||
: 'editor.annotations.path',
|
||||
)}
|
||||
</p>
|
||||
)}
|
||||
{!locked && !isEditable(geometry) && (
|
||||
<TraceReplacement id={annotation.id} dispatch={props.dispatch} />
|
||||
)}
|
||||
<GeometryIssues id={annotation.id} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function applyGeometry(
|
||||
annotation: Annotation,
|
||||
next: Geometry,
|
||||
context: CommitContext,
|
||||
): ApplyResult {
|
||||
const result = updateGeometry(annotation, next, context)
|
||||
return result.ok ? { ok: true } : { ok: false, messages: result.messages }
|
||||
}
|
||||
|
||||
function TraceReplacement({
|
||||
id,
|
||||
dispatch,
|
||||
}: {
|
||||
id: string
|
||||
dispatch: Dispatch<UiAction>
|
||||
}) {
|
||||
const t = useT()
|
||||
return (
|
||||
<Button
|
||||
type="button"
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onClick={() =>
|
||||
dispatch({ type: 'trace_start', target: 'replace', annotationId: id })
|
||||
}
|
||||
>
|
||||
{t('editor.annotations.replace_polygon')}
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
|
||||
function GeometryIssues({ id }: { id: string }) {
|
||||
const issues = useGeometryIssues(id)
|
||||
if (issues.length === 0) return null
|
||||
return (
|
||||
<ul
|
||||
role="alert"
|
||||
data-testid="annotation-geometry-issues"
|
||||
className="list-disc pl-5 text-sm text-[var(--color-danger-strong)]"
|
||||
>
|
||||
{issues.map((issue) => (
|
||||
<li key={issue}>{issue}</li>
|
||||
))}
|
||||
</ul>
|
||||
)
|
||||
}
|
||||
|
||||
export function UnmeasuredHint({
|
||||
onSelectTab,
|
||||
}: Pick<PropertiesPanelProps, 'onSelectTab'>) {
|
||||
const t = useT()
|
||||
return (
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t('editor.annotations.unmeasured.hint')}{' '}
|
||||
<button
|
||||
type="button"
|
||||
className="underline"
|
||||
onClick={() => onSelectTab('views')}
|
||||
>
|
||||
{t('editor.tab.views')}
|
||||
</button>
|
||||
</p>
|
||||
)
|
||||
}
|
||||
|
||||
function StatusActions({
|
||||
annotation,
|
||||
readOnly,
|
||||
edit,
|
||||
}: {
|
||||
annotation: Annotation
|
||||
readOnly: boolean
|
||||
edit: ReturnType<typeof useEditCommand>
|
||||
}) {
|
||||
const t = useT()
|
||||
const mark = (status: Marking) =>
|
||||
edit.run('status', {
|
||||
op: 'set_status',
|
||||
key: { kind: 'annotation', id: annotation.id },
|
||||
status,
|
||||
})
|
||||
const reviewed = annotation.review_status === 'reviewed'
|
||||
return (
|
||||
<div className="space-y-1">
|
||||
<ActionButton
|
||||
readOnly={readOnly}
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onClick={() => mark(reviewed ? 'to_confirm' : 'reviewed')}
|
||||
>
|
||||
{t(
|
||||
reviewed
|
||||
? 'editor.annotations.to_confirm'
|
||||
: 'editor.annotations.mark_reviewed',
|
||||
)}
|
||||
</ActionButton>
|
||||
{edit.fieldError('status') && (
|
||||
<p role="alert" className="text-sm text-[var(--color-danger-strong)]">
|
||||
{edit.fieldError('status')}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function Deletion({
|
||||
id,
|
||||
name,
|
||||
readOnly,
|
||||
}: {
|
||||
id: string
|
||||
name: string
|
||||
readOnly: boolean
|
||||
}) {
|
||||
const t = useT()
|
||||
const [open, setOpen] = useState(false)
|
||||
return (
|
||||
<>
|
||||
<ActionButton
|
||||
readOnly={readOnly}
|
||||
variant="danger"
|
||||
size="sm"
|
||||
onClick={() => setOpen(true)}
|
||||
>
|
||||
{t('editor.action.delete')}
|
||||
</ActionButton>
|
||||
<DeleteDialog
|
||||
target={open ? { key: { kind: 'annotation', id }, label: name } : null}
|
||||
onClose={() => setOpen(false)}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import type { ReviewStatus } from '@faunosteo/core-wasm/types'
|
||||
import Badge from '@/components/ui/Badge'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
const TONES = {
|
||||
to_confirm: 'warn',
|
||||
reviewed: 'accent',
|
||||
inherited: 'neutral',
|
||||
} as const
|
||||
|
||||
export default function ReviewBadge({ status }: { status: ReviewStatus }) {
|
||||
const t = useT()
|
||||
return (
|
||||
<Badge tone={TONES[status]}>
|
||||
{t(`editor.annotations.status.${status}`)}
|
||||
</Badge>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/** Board 04's tag over the drawing: « Sélection : <type> « <nom> ». */
|
||||
export default function SelectionTag({
|
||||
kind,
|
||||
name,
|
||||
}: {
|
||||
kind: string
|
||||
name: string
|
||||
}) {
|
||||
const t = useT()
|
||||
return (
|
||||
<p
|
||||
data-testid="annotation-selection-tag"
|
||||
className="pointer-events-none inline-block rounded-md bg-[var(--color-surface-strong)] px-2 py-1 text-xs"
|
||||
>
|
||||
{t('editor.annotations.selection')
|
||||
.replace(
|
||||
'{kind}',
|
||||
t(`editor.annotations.kind.${kind}`).toLocaleLowerCase(),
|
||||
)
|
||||
.replace('{name}', name)}
|
||||
</p>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
import type { ReactNode } from 'react'
|
||||
import { canRedo, canUndo, useEditorStore } from '@/editor/state/editorStore'
|
||||
import type { ToolId } from '@/editor/annotations/uiState'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
export interface ToolColumnProps {
|
||||
tool: ToolId
|
||||
onTool: (tool: ToolId) => void
|
||||
/** Point, zone and link need a measured view and a category. */
|
||||
drawingDisabled: boolean
|
||||
/** The link tool needs a detail view directly under the current one. */
|
||||
linkDisabled: boolean
|
||||
onZoomIn: () => void
|
||||
onZoomOut: () => void
|
||||
onFit: () => void
|
||||
}
|
||||
|
||||
const GLYPHS: Record<string, string> = {
|
||||
select: 'M5 3l12 7-5 1-3 5z',
|
||||
point: 'M12 8a4 4 0 1 0 0 8 4 4 0 0 0 0-8zM12 3v3M12 18v3M3 12h3M18 12h3',
|
||||
zone: 'M12 3l8 6-3 10H7L4 9z',
|
||||
link: 'M4 4h8v8H4zM12 12h8v8h-8z',
|
||||
in: 'M11 5v12M5 11h12M20 20l-4-4',
|
||||
out: 'M5 11h12M20 20l-4-4',
|
||||
fit: 'M4 9V4h5M20 9V4h-5M4 15v5h5M20 15v5h-5',
|
||||
undo: 'M9 7L4 12l5 5M4 12h11a5 5 0 0 1 0 10',
|
||||
redo: 'M15 7l5 5-5 5M20 12H9a5 5 0 0 0 0 10',
|
||||
}
|
||||
|
||||
function ToolButton({
|
||||
glyph,
|
||||
label,
|
||||
pressed,
|
||||
disabled,
|
||||
onClick,
|
||||
}: {
|
||||
glyph: string
|
||||
label: string
|
||||
pressed?: boolean
|
||||
disabled?: boolean
|
||||
onClick: () => void
|
||||
}): ReactNode {
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
aria-label={label}
|
||||
title={label}
|
||||
aria-pressed={pressed}
|
||||
disabled={disabled}
|
||||
onClick={onClick}
|
||||
className={`grid h-11 w-11 place-items-center rounded-lg border border-[var(--color-border)] disabled:cursor-not-allowed disabled:opacity-40 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)] ${
|
||||
pressed
|
||||
? 'bg-[var(--color-ink)] text-white'
|
||||
: 'bg-[var(--color-surface)] text-[var(--color-ink)]'
|
||||
}`}
|
||||
>
|
||||
<svg
|
||||
viewBox="0 0 24 24"
|
||||
width="20"
|
||||
height="20"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="1.8"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<path d={GLYPHS[glyph]} />
|
||||
</svg>
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
/** Board 04's tool column: tools, zoom, then undo and redo (icon buttons). */
|
||||
export default function ToolColumn(props: ToolColumnProps) {
|
||||
const t = useT()
|
||||
const undoable = useEditorStore(canUndo)
|
||||
const redoable = useEditorStore(canRedo)
|
||||
const tool = (id: ToolId, disabled: boolean) => (
|
||||
<ToolButton
|
||||
glyph={id}
|
||||
label={t(`editor.annotations.tool.${id}`)}
|
||||
pressed={props.tool === id}
|
||||
disabled={disabled}
|
||||
onClick={() => props.onTool(id)}
|
||||
/>
|
||||
)
|
||||
return (
|
||||
<div
|
||||
role="toolbar"
|
||||
aria-orientation="vertical"
|
||||
aria-label={t('editor.annotations.tools')}
|
||||
className="flex flex-row gap-2 lg:flex-col"
|
||||
>
|
||||
{tool('select', false)}
|
||||
{tool('point', props.drawingDisabled)}
|
||||
{tool('zone', props.drawingDisabled)}
|
||||
{tool('link', props.drawingDisabled || props.linkDisabled)}
|
||||
<hr className="hidden border-[var(--color-border)] lg:block" />
|
||||
<ToolButton
|
||||
glyph="out"
|
||||
label={t('catalogue.zoom.out')}
|
||||
onClick={props.onZoomOut}
|
||||
/>
|
||||
<ToolButton
|
||||
glyph="in"
|
||||
label={t('catalogue.zoom.in')}
|
||||
onClick={props.onZoomIn}
|
||||
/>
|
||||
<ToolButton
|
||||
glyph="fit"
|
||||
label={t('catalogue.zoom.fit')}
|
||||
onClick={props.onFit}
|
||||
/>
|
||||
<hr className="hidden border-[var(--color-border)] lg:block" />
|
||||
<ToolButton
|
||||
glyph="undo"
|
||||
label={t('editor.annotations.undo')}
|
||||
disabled={!undoable}
|
||||
onClick={() => useEditorStore.getState().undoStep()}
|
||||
/>
|
||||
<ToolButton
|
||||
glyph="redo"
|
||||
label={t('editor.annotations.redo')}
|
||||
disabled={!redoable}
|
||||
onClick={() => useEditorStore.getState().redoStep()}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import Button from '@/components/ui/Button'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/** While a zone or link is being traced: close (3 vertices at least) or drop it. */
|
||||
export default function TracingBar({
|
||||
count,
|
||||
onClose,
|
||||
onCancel,
|
||||
}: {
|
||||
count: number
|
||||
onClose: () => void
|
||||
onCancel: () => void
|
||||
}) {
|
||||
const t = useT()
|
||||
return (
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t('editor.annotations.tracing').replace('{count}', String(count))}
|
||||
</p>
|
||||
<Button type="button" size="sm" disabled={count < 3} onClick={onClose}>
|
||||
{t('editor.annotations.trace_close')}
|
||||
</Button>
|
||||
<Button type="button" size="sm" variant="ghost" onClick={onCancel}>
|
||||
{t('editor.annotations.trace_cancel')}
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import type { SheetView } from '@faunosteo/core-wasm/types'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
|
||||
/** One chip per view (board 04): the main view, then each detail view. */
|
||||
export default function ViewSwitcher({
|
||||
views,
|
||||
current,
|
||||
onSelect,
|
||||
}: {
|
||||
views: readonly SheetView[]
|
||||
current: string
|
||||
onSelect: (viewId: string) => void
|
||||
}) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
return (
|
||||
<div
|
||||
role="group"
|
||||
aria-label={t('editor.annotations.views')}
|
||||
className="flex flex-wrap gap-2"
|
||||
>
|
||||
{views.map((view) => (
|
||||
<button
|
||||
key={view.id}
|
||||
type="button"
|
||||
aria-pressed={view.id === current}
|
||||
onClick={() => onSelect(view.id)}
|
||||
className={`rounded-full border border-[var(--color-border)] px-3 py-1 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)] ${
|
||||
view.id === current
|
||||
? 'bg-[var(--color-ink)] text-white'
|
||||
: 'bg-[var(--color-surface)]'
|
||||
}`}
|
||||
>
|
||||
{view.name[locale]}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user