fix(app): annotations tab split, editor-wide undo keys, keyboard point fits, typed radius message

This commit is contained in:
bchanot committed 2026-11-04 14:00:57 +01:00
1 parent 447d0a4272
commit 3f15762d95
25 files changed
+1398 -653

No files matched your search

@@ -1,23 +1,18 @@
import { useEffect, useRef, type PointerEvent } from 'react'
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 { 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 { 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'
@@ -37,101 +32,107 @@ export interface AnnotationCanvasProps {
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
/** 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. `touch-action: none` only while a drawing tool or a drag is active.
* it after 4 CSS px.
*/
export default function AnnotationCanvas(props: AnnotationCanvasProps) {
const overlayRef = useRef<SVGSVGElement | null>(null)
const overlay = 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),
})
const gestures = useCanvasGestures({ ...props, overlay, viewport })
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
useReportRoot(build.root, props.onRoot)
return (
<div className="flex flex-col gap-2 lg:flex-row">
<ToolColumn
{...props.tools}
onZoomIn={framing.zoomIn}
onZoomOut={framing.zoomOut}
onFit={framing.reset}
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 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>
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"
@@ -154,31 +155,10 @@ function DrawingLayer({
}
function useReportRoot(
viewBox: string | null,
onRoot: (v: string | null) => void,
root: RootAttributes | null,
onRoot: (root: RootAttributes | 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,
}
useEffect(() => onRoot(root), [root, onRoot])
}
function CanvasFailure({