Files
faunosteo/apps/app/src/components/editor/annotations/AnnotationCanvas.tsx
T

189 lines
5.6 KiB
TypeScript

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<ToolColumnProps, 'onZoomIn' | 'onZoomOut' | 'onFit'>
/** 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<SVGSVGElement | null>(null)
const viewport = useRef<HTMLDivElement | null>(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 (
<div className="flex flex-col gap-2 lg:flex-row">
<ToolColumn
{...props.tools}
onZoomIn={gestures.framing.zoomIn}
onZoomOut={gestures.framing.zoomOut}
onFit={gestures.framing.reset}
/>
<CanvasBox
{...props}
build={build}
viewport={viewport}
boxProps={gestures.boxProps}
layer={{
transform: transformOf(gestures.framing.framing),
overlay: {
...props.overlay,
svgRef: overlay,
dragged: gestures.drag,
},
}}
/>
</div>
)
}
type Build = ReturnType<typeof useCanvasBuild>
function CanvasBox({
viewId,
viewName,
tool,
build,
viewport,
boxProps,
layer,
}: AnnotationCanvasProps & {
build: Build
viewport: RefObject<HTMLDivElement | null>
boxProps: ReturnType<typeof useCanvasGestures>['boxProps']
layer: Omit<LayerProps, 'host' | 'root'>
}) {
const root = build.root?.viewId === viewId ? build.root : null
const cursor = tool === 'select' ? 'cursor-grab' : 'cursor-crosshair'
return (
<div className={BOX}>
<div
ref={viewport}
tabIndex={0}
role="group"
aria-label={viewName}
data-testid="annotation-canvas"
className={`svg-canvas aspect-[4/3] w-full select-none ${cursor} ${FOCUS_RING}`}
{...boxProps}
>
<DrawingLayer host={build.host} root={root} {...layer} />
</div>
<CanvasFailure failure={build.failure} retry={build.retry} />
</div>
)
}
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<HTMLDivElement | null>
root: RootAttributes | null
transform: string
overlay: Omit<OverlayProps, 'viewBox' | 'ratio'>
}
/** The transformed box: the core's drawing and, in the same coordinates, the overlay. */
function DrawingLayer({ host, root, transform, overlay }: LayerProps) {
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(
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 (
<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>
)
}