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>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user