import { useEffect, useRef, type KeyboardEvent, type RefObject } 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 type { XY } from '@/editor/annotations/frame' import { useCanvasBuild, type RootAttributes, useSelectionMark, } from '@/editor/annotations/useCanvasBuild' import { useCanvasGestures } from '@/editor/annotations/useCanvasGestures' import 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 /** The composed root as rebuilt (with its view); null on failure. */ onRoot: (root: RootAttributes | 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 onTraceKey: (event: KeyboardEvent) => 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. */ export default function AnnotationCanvas(props: AnnotationCanvasProps) { const overlay = useRef(null) const viewport = useRef(null) const build = useCanvasBuild(props.viewId) const gestures = useCanvasGestures({ ...props, overlay, viewport }) useSelectionMark(build.host, props.selectedId, build.root) useReportRoot(build.root, props.onRoot) return (
) } type Build = ReturnType function CanvasBox({ viewId, viewName, tool, build, viewport, boxProps, layer, }: AnnotationCanvasProps & { build: Build viewport: RefObject boxProps: ReturnType['boxProps'] layer: Omit }) { const root = build.root?.viewId === viewId ? build.root : null const cursor = tool === 'select' ? 'cursor-grab' : 'cursor-crosshair' return (
) } const BOX = 'relative min-w-0 flex-1 overflow-hidden rounded-xl border ' + 'border-[var(--color-border)] bg-white' const FOCUS_RING = 'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset ' + 'focus-visible:ring-[var(--color-accent)]' interface LayerProps { host: RefObject root: RootAttributes | null transform: string overlay: Omit } /** The transformed box: the core's drawing and, in the same coordinates, the overlay. */ function DrawingLayer({ host, root, transform, overlay }: LayerProps) { return (
{root?.viewBox && ( )}
) } function useReportRoot( root: RootAttributes | null, onRoot: (root: RootAttributes | null) => void, ) { useEffect(() => onRoot(root), [root, onRoot]) } function CanvasFailure({ failure, retry, }: { failure: 'engine' | 'view' | null retry: () => void }) { const t = useT() const stopped = useCoreStopped() if (!failure) return null return (
{failure === 'view' ? t('catalogue.failure.view') : coreFailureMessage(t, 'sheet.core_unavailable')} {failure === 'engine' && !stopped && ( )}
) }