feat(editor): vertex handles, insert and remove, zone move, redraw
U2 Task 5. A selected polygon gets its vertex, « + » and « − » handles (R-9, hidden « + » under two hit radii, R-18 zoom hint in the strip), the handle cap at 40 (R-7), vertex drag and keys, the move mode on the zone or the chosen vertex, « Redessiner » as a redraw mode kept in step with its tracing (R-12 guard, R-13 drop), close radius in CSS px (R-11). The numeric group leaves the properties panel (D1). Removed or rewritten assertions (numeric group -> replacement): - tab « shows its exact stored numbers » (Centre x field) -> the overlay selection circle's cx attribute, same exact value. - tab « a numeric edit » x3 -> « an edit on the drawing »: Enter on the handle without a step commits nothing, a step + Enter commits once; a refused handle drag keeps the stored shape then a drag inside the frame repairs it (issues gone); Escape drops a pending series. - tab C26 « Centre x 15 » -> overlay cx 15. - tab C32 unmeasured « Centre x as text » -> no handle, no field. - tab « adds and removes a vertex as part of the whole group » -> « + » then « − » on the drawing; « never offers to remove below three » -> « − » aria-disabled at 3. - tab H8 « refused Remplacer drops the tracing » -> keeps it open with « Fermeture refusée » (board 33 A); « refused vertex add/remove stay non appliqué » -> a refused vertex drag restores the shape exactly. - tab Désélectionner and D6b (typed Centre x committed, refused, undone) x6 and strip « a new commit replaces the message » -> the same with the Nom (FR) field (a blank name is the refused value). - focus-request « Centre x 20 » -> overlay cx 20. - escape « strip commands keep their Escape » -> a redraw: Escape on the strip or the box cancels it (R-12 row 4); kept for a new zone tracing. - keep-alive « Remplacer tracing kept across Aller à » -> ended by a tab when empty, asked once a vertex is placed (R-12). - e2e annotations: numeric cx edit + undo/redo -> handle keys + undo/ redo; jitter/drag and Task 4 point reads -> overlay attributes (the stored place during the mode -> overlay-old-place); keyboard-only cy and vertex x typed -> handle and vertex keys. - e2e deselect: Centre x typed/refused/committed -> Nom (FR). - e2e geometry, shell-crossing: Centre x/cy reads -> overlay attributes. - e2e journey: cx +3 and vertex x +4 typed -> 3 and 4 arrow steps + Enter; a far typed value + revert -> a far handle drag refused. - e2e equine latency: typed cx rounds -> handle arrow + Enter rounds.
This commit is contained in:
1 parent
ba8e766fe8
commit
6211046537
44 files changed
+3790
-1050
No files matched your search
@@ -13,6 +13,8 @@ import {
|
||||
} from '@/editor/annotations/useCanvasBuild'
|
||||
import { useCanvasGestures } from '@/editor/annotations/useCanvasGestures'
|
||||
import type { MoveTarget } from '@/editor/annotations/useBlockDrag'
|
||||
import type { Drop } from '@/editor/annotations/usePlacement'
|
||||
import type { HandleAction } from '@/editor/annotations/usePressGesture'
|
||||
import type { ToolId } from '@/editor/annotations/uiState'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import AnnotationOverlay, { type OverlayProps } from './AnnotationOverlay'
|
||||
@@ -27,7 +29,7 @@ type OverlayData = Omit<
|
||||
/** The handle layer's content: the handle and its keys (the layer measures itself). */
|
||||
export type HandleData = Pick<
|
||||
HandleLayerProps,
|
||||
'handle' | 'onKeyDown' | 'onBlur'
|
||||
'handle' | 'onKeyDown' | 'onBlur' | 'zone' | 'zoneKeys' | 'onInsertsHidden'
|
||||
>
|
||||
|
||||
export interface AnnotationCanvasProps {
|
||||
@@ -43,9 +45,10 @@ export interface AnnotationCanvasProps {
|
||||
/** 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
|
||||
/** A click with a drawing tool, in drawing units, and its closing reach (R-11). */
|
||||
onPlace: (at: XY, reach: number) => void
|
||||
onDrop: Drop
|
||||
onHandle: (action: HandleAction) => void
|
||||
onDeselect: () => void
|
||||
onTraceKey: (event: KeyboardEvent) => void
|
||||
}
|
||||
@@ -88,7 +91,11 @@ export default function AnnotationCanvas(props: AnnotationCanvasProps) {
|
||||
svgRef: overlay,
|
||||
dragged: gestures.drag,
|
||||
},
|
||||
handles: { ...props.handles, scale: gestures.framing.framing.scale },
|
||||
handles: {
|
||||
...props.handles,
|
||||
scale: gestures.framing.framing.scale,
|
||||
dragged: gestures.drag,
|
||||
},
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
@@ -144,7 +151,7 @@ interface LayerProps {
|
||||
root: RootAttributes | null
|
||||
transform: string
|
||||
overlay: Omit<OverlayProps, 'viewBox' | 'ratio'>
|
||||
handles: HandleData & { scale: number }
|
||||
handles: HandleData & { scale: number; dragged: Geometry | null }
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,6 +19,8 @@ export interface OverlayProps {
|
||||
pending: Geometry | null
|
||||
tracing: Point[] | null
|
||||
correction: Correction | null
|
||||
/** A redraw runs: the stored outline stays as the old place until the close (06 C). */
|
||||
redrawing: boolean
|
||||
}
|
||||
|
||||
const STROKE = { fill: 'none', vectorEffect: 'non-scaling-stroke' } as const
|
||||
@@ -130,7 +132,8 @@ export default function AnnotationOverlay(props: OverlayProps) {
|
||||
const size = frame ? Math.max(frame.w, frame.h) / 200 : 1
|
||||
const preview = usePreviewOf(props.selectedId)
|
||||
const moved = props.dragged ?? preview
|
||||
const shown = moved ?? selected
|
||||
const old = moved !== null || props.redrawing
|
||||
const shown = props.redrawing ? null : (moved ?? selected)
|
||||
return (
|
||||
<svg
|
||||
ref={props.svgRef}
|
||||
@@ -140,7 +143,7 @@ export default function AnnotationOverlay(props: OverlayProps) {
|
||||
preserveAspectRatio={props.ratio}
|
||||
className="pointer-events-none absolute inset-0 h-full w-full"
|
||||
>
|
||||
{moved && selected && isEditable(selected) && (
|
||||
{old && selected && isEditable(selected) && (
|
||||
<g data-testid="overlay-old-place">
|
||||
<Outline geometry={selected} dashed colour="var(--color-ink-muted)" />
|
||||
</g>
|
||||
@@ -155,10 +158,10 @@ export default function AnnotationOverlay(props: OverlayProps) {
|
||||
</g>
|
||||
)}
|
||||
{shown && isEditable(shown) && (
|
||||
<>
|
||||
<g data-testid="overlay-selection">
|
||||
<Outline geometry={shown} dashed colour="var(--color-ink)" />
|
||||
<Vertices geometry={shown} size={size} />
|
||||
</>
|
||||
</g>
|
||||
)}
|
||||
{pending && (
|
||||
<Outline
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { useEffect, useReducer, useState, type Dispatch } from 'react'
|
||||
import Button from '@/components/ui/Button'
|
||||
import type {
|
||||
AnatomicalSheetDocument,
|
||||
Annotation,
|
||||
@@ -12,7 +11,14 @@ import { parseFrame, type Frame } from '@/editor/annotations/frame'
|
||||
import { newPointSize } from '@/editor/annotations/pointSize'
|
||||
import { guardDrawing } from '@/editor/annotations/drawingGuard'
|
||||
import { usePointEdit } from '@/editor/annotations/usePointEdit'
|
||||
import { endMode, useAnnotationWork } from '@/editor/annotations/workStore'
|
||||
import { useZoneEdit } from '@/editor/annotations/useZoneEdit'
|
||||
import { useRedrawFollowsTracing } from '@/editor/annotations/redraw'
|
||||
import {
|
||||
useDroppedNotice,
|
||||
useModeFollowsSelection,
|
||||
} from '@/editor/annotations/modeEnds'
|
||||
import { handledPoints } from '@/editor/annotations/zoneEdit'
|
||||
import { useAnnotationWork } from '@/editor/annotations/workStore'
|
||||
import { isEditable } from '@/editor/annotations/geometryEdit'
|
||||
import { useSelectionEscape } from '@/editor/annotations/deselect'
|
||||
import { usePlacement } from '@/editor/annotations/usePlacement'
|
||||
@@ -20,7 +26,6 @@ import { stripLeadOf } from '@/editor/annotations/stripLead'
|
||||
import {
|
||||
useGestureMessage,
|
||||
type GestureMessage as ShownMessage,
|
||||
type GestureMessages,
|
||||
} from '@/editor/annotations/useGestureMessage'
|
||||
import {
|
||||
initialUi,
|
||||
@@ -49,6 +54,8 @@ import {
|
||||
} from './PointCommands'
|
||||
import CreationPanel from './CreationPanel'
|
||||
import EditStrip, { type StripNotice } from './EditStrip'
|
||||
import TracingCommands from './TracingCommands'
|
||||
import ZoneCommands, { moveTitleKey, useChosenVertex } from './ZoneCommands'
|
||||
import GestureMessage from './GestureMessage'
|
||||
import KeyboardAdd from './KeyboardAdd'
|
||||
import OverviewPanel from './OverviewPanel'
|
||||
@@ -145,59 +152,41 @@ function useCommitContext(): CommitContext {
|
||||
}
|
||||
|
||||
/**
|
||||
* The tab's work: the commit context, the line under the drawing, the placement and the
|
||||
* point editing (handle keys, slider, move mode) on the movable selected circle.
|
||||
* The tab's work: the commit context, the line under the drawing, the placement, the
|
||||
* editing of the movable selection (point handle keys, slider, move mode) and of a
|
||||
* polygon's vertices, the redraw kept in step with its tracing.
|
||||
*/
|
||||
function useWork(scene: Scene) {
|
||||
const context = useCommitContext()
|
||||
const message = useGestureMessage()
|
||||
const placement = usePlacement({ ...scene, context, message })
|
||||
const target = circleTargetOf(scene.selected, scene.model, scene.ui.tool)
|
||||
const edit = usePointEdit({
|
||||
target,
|
||||
frame: scene.frame,
|
||||
onDrop: placement.onDrop,
|
||||
const target = movableOf(scene.selected, scene.model, scene.ui.tool)
|
||||
const onDrop = placement.onDrop
|
||||
const edit = usePointEdit({ target, frame: scene.frame, onDrop })
|
||||
const polygon = target?.geometry?.type === 'polygon' ? target : null
|
||||
const zone = useZoneEdit({
|
||||
...{ target: polygon, frame: scene.frame, onDrop, message },
|
||||
t: context.t,
|
||||
confirmMove: edit.confirmMove,
|
||||
})
|
||||
const [insertsHidden, setInsertsHidden] = useState(false)
|
||||
useModeFollowsSelection(scene.ui.selectedId)
|
||||
useRedrawFollowsTracing(scene.ui.tracing, scene.dispatch)
|
||||
useDroppedNotice(message)
|
||||
return { context, message, placement, edit, target }
|
||||
return {
|
||||
context,
|
||||
message,
|
||||
placement,
|
||||
edit,
|
||||
zone,
|
||||
target,
|
||||
insertsHidden,
|
||||
setInsertsHidden,
|
||||
}
|
||||
}
|
||||
|
||||
type Work = ReturnType<typeof useWork>
|
||||
|
||||
/**
|
||||
* The move mode belongs to the selection: another selection (a list row, a view change)
|
||||
* ends it. A read-only flip keeps the body and its selection, so it suspends the mode
|
||||
* instead (R-14).
|
||||
*/
|
||||
function useModeFollowsSelection(selectedId: string | null) {
|
||||
const modeId = useAnnotationWork((s) => s.mode?.annotationId ?? null)
|
||||
useEffect(() => {
|
||||
if (modeId === null || modeId === selectedId) return
|
||||
const { mode } = useAnnotationWork.getState()
|
||||
endMode()
|
||||
// Never silent (R-13): a started move that ends this way is said.
|
||||
if (mode?.started) useAnnotationWork.setState({ dropped: mode.kind })
|
||||
}, [modeId, selectedId])
|
||||
}
|
||||
|
||||
/**
|
||||
* R-13: a started mode a change made elsewhere dropped is said on the line, once the body
|
||||
* is shown (a drop while read-only is said on the way back).
|
||||
*/
|
||||
function useDroppedNotice(message: GestureMessages) {
|
||||
const t = useT()
|
||||
const visible = usePlaceVisible()
|
||||
const dropped = useAnnotationWork((s) => s.dropped)
|
||||
const show = message.show
|
||||
useEffect(() => {
|
||||
if (dropped === null || !visible) return
|
||||
useAnnotationWork.setState({ dropped: null })
|
||||
const text = t(`editor.annotations.dropped.${dropped}`)
|
||||
show(text, { undo: false, tone: 'info' })
|
||||
}, [dropped, visible, show, t])
|
||||
}
|
||||
|
||||
export default function EditableAnnotations(props: Props) {
|
||||
const scene = useScene(props)
|
||||
const work = useWork(scene)
|
||||
@@ -206,7 +195,10 @@ export default function EditableAnnotations(props: Props) {
|
||||
const { asideRef, byDrawing } = useSelectionEscape(
|
||||
selected?.id ?? null,
|
||||
() => scene.dispatch({ type: 'select', id: null }),
|
||||
visible && selected !== undefined && ui.tracing === null,
|
||||
// A redraw's Escape cancels it at once (R-12); a new tracing keeps its own keys.
|
||||
visible &&
|
||||
selected !== undefined &&
|
||||
(ui.tracing === null || ui.tracing.target === 'replace'),
|
||||
{ editable: true, creating: ui.pending !== null },
|
||||
)
|
||||
return (
|
||||
@@ -222,8 +214,6 @@ export default function EditableAnnotations(props: Props) {
|
||||
)
|
||||
}
|
||||
|
||||
type Placement = ReturnType<typeof usePlacement>
|
||||
|
||||
/** A move that drops the drawing's work asks first (R-12): the action runs once allowed. */
|
||||
const guarded = (action: () => void) => () => {
|
||||
if (guardDrawing(action)) action()
|
||||
@@ -265,7 +255,7 @@ function CanvasColumn({
|
||||
function useHandle(target: Annotation | null, doc: AnatomicalSheetDocument) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
if (!target?.geometry) return null
|
||||
if (target?.geometry?.type !== 'circle') return null
|
||||
const name = annotationName(target, doc.views, locale)
|
||||
const kind = t(`editor.annotations.kind.${target.kind}`)
|
||||
const label = t('editor.annotations.handle.shape')
|
||||
@@ -292,6 +282,8 @@ function CanvasStage({
|
||||
const shown = usePlaceVisible()
|
||||
const { doc, ui, dispatch, model, frame, selected } = scene
|
||||
const handle = useHandle(shown ? work.target : null, doc)
|
||||
const points = shown ? handledPoints(work.target?.geometry ?? null) : null
|
||||
const zone = points && { annotationId: work.target!.id, points }
|
||||
const view = doc.views.find((v) => v.id === scene.viewId)
|
||||
const blocked = !model.measured || doc.categories.length === 0
|
||||
if (!view) return null
|
||||
@@ -307,6 +299,9 @@ function CanvasStage({
|
||||
handle,
|
||||
onKeyDown: work.edit.onHandleKey,
|
||||
onBlur: work.edit.onHandleBlur,
|
||||
zone,
|
||||
zoneKeys: work.zone,
|
||||
onInsertsHidden: work.setInsertsHidden,
|
||||
}}
|
||||
tools={{
|
||||
tool: ui.tool,
|
||||
@@ -318,6 +313,7 @@ function CanvasStage({
|
||||
onSelect={(id) => dispatch({ type: 'select', id })}
|
||||
onPlace={work.placement.onPlace}
|
||||
onDrop={work.placement.onDrop}
|
||||
onHandle={work.zone.onHandle}
|
||||
onDeselect={onDeselect}
|
||||
onTraceKey={work.placement.onTraceKey}
|
||||
/>
|
||||
@@ -379,7 +375,6 @@ function SidePanel({
|
||||
readOnly={false}
|
||||
geometryLocked={!model.measured}
|
||||
dispatch={dispatch}
|
||||
context={context}
|
||||
navigate={scene.navigate}
|
||||
// « Tout afficher » leaves the selection: a started move asks first (R-12).
|
||||
onShowAll={guarded(() => dispatch({ type: 'select', id: null }))}
|
||||
@@ -399,14 +394,13 @@ function moveTargetOf(
|
||||
: null
|
||||
}
|
||||
|
||||
/** The selected point or link circle, when it can be moved now (R-9: its handle). */
|
||||
function circleTargetOf(
|
||||
/** The selection when it can be moved now: a circle (its handle) or a polygon (R-9). */
|
||||
function movableOf(
|
||||
selected: Annotation | undefined,
|
||||
model: ViewModel,
|
||||
tool: ToolId,
|
||||
): Annotation | null {
|
||||
const movable = moveTargetOf(selected, model, tool) !== null
|
||||
return movable && selected?.geometry?.type === 'circle' ? selected : null
|
||||
return moveTargetOf(selected, model, tool) !== null ? selected! : null
|
||||
}
|
||||
|
||||
function overlayOf(
|
||||
@@ -423,6 +417,7 @@ function overlayOf(
|
||||
pending: ui.pending?.geometry ?? null,
|
||||
tracing: ui.tracing?.points ?? null,
|
||||
correction: ui.pending?.correction ?? null,
|
||||
redrawing: ui.tracing?.target === 'replace',
|
||||
}
|
||||
}
|
||||
|
||||
@@ -433,31 +428,47 @@ function overlayOf(
|
||||
function SceneStrip({ scene, work }: { scene: Scene; work: Work }) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const notice = useStripNotice(scene, work.message.message)
|
||||
const moving = useAnnotationWork((s) => s.mode?.kind === 'move')
|
||||
useMoveFocusReturn(moving)
|
||||
const notice = useStripNotice(scene, work)
|
||||
const mode = useAnnotationWork((s) =>
|
||||
s.mode?.kind === 'move' ? s.mode : null,
|
||||
)
|
||||
useMoveFocusReturn(mode !== null)
|
||||
const { ui, selected, doc } = scene
|
||||
const lead = moving
|
||||
const chosenVertex = useChosenVertex(selected?.id)
|
||||
const lead = mode
|
||||
? {
|
||||
title: t('editor.annotations.move.point'),
|
||||
title: t(moveTitleKey(mode, work.target?.kind)),
|
||||
detail: t('editor.annotations.move.hint'),
|
||||
icon: 'move' as const,
|
||||
}
|
||||
: stripLeadOf(ui, selected, doc, { t, locale })
|
||||
: stripLeadOf(ui, selected, doc, { t, locale, chosenVertex })
|
||||
const commandsKey = commandsKeyOf(
|
||||
ui,
|
||||
mode !== null,
|
||||
work.target,
|
||||
chosenVertex,
|
||||
)
|
||||
return (
|
||||
<EditStrip
|
||||
lead={lead}
|
||||
notice={notice}
|
||||
commands={<SceneCommands scene={scene} work={work} />}
|
||||
commandsKey={commandsKeyOf(ui, moving, work.target?.kind === 'point')}
|
||||
commandsKey={commandsKey}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function commandsKeyOf(ui: UiState, moving: boolean, point: boolean) {
|
||||
/** One box per set of commands: a wider set never moves an existing box (A14). */
|
||||
function commandsKeyOf(
|
||||
ui: UiState,
|
||||
moving: boolean,
|
||||
target: Annotation | null,
|
||||
chosen: number | null,
|
||||
) {
|
||||
if (ui.tracing) return 'tracing'
|
||||
if (moving) return 'move'
|
||||
return point ? 'point' : `tool-${ui.tool}`
|
||||
if (target?.geometry?.type === 'polygon') return `zone-${chosen !== null}`
|
||||
return target?.kind === 'point' ? 'point' : `tool-${ui.tool}`
|
||||
}
|
||||
|
||||
/** A tracing's commands, a movable point's, or the Point tool's size (A5). */
|
||||
@@ -467,11 +478,18 @@ function SceneCommands({ scene, work }: { scene: Scene; work: Work }) {
|
||||
return (
|
||||
<TracingCommands
|
||||
count={ui.tracing.points.length}
|
||||
placement={work.placement}
|
||||
onClose={work.placement.closeTracing}
|
||||
onPop={work.placement.popVertex}
|
||||
onCancel={() => dispatch({ type: 'trace_cancel' })}
|
||||
/>
|
||||
)
|
||||
}
|
||||
if (work.target?.geometry?.type === 'polygon') {
|
||||
const edit = work.edit
|
||||
return (
|
||||
<ZoneCommands annotation={work.target} edit={edit} dispatch={dispatch} />
|
||||
)
|
||||
}
|
||||
// A link circle keeps its handle but has no size and no move mode (R-10; no label).
|
||||
if (work.target?.kind === 'point') {
|
||||
const props = { doc, viewId, frame, edit: work.edit }
|
||||
@@ -497,10 +515,12 @@ function SceneCommands({ scene, work }: { scene: Scene; work: Work }) {
|
||||
*/
|
||||
function useStripNotice(
|
||||
scene: Scene,
|
||||
message: ShownMessage | null,
|
||||
work: Pick<Work, 'message' | 'insertsHidden'>,
|
||||
): StripNotice | null {
|
||||
const t = useT()
|
||||
const issues = useGeometryIssues(scene.selected?.id ?? '')
|
||||
const inMode = useAnnotationWork((s) => s.mode !== null)
|
||||
const message: ShownMessage | null = work.message.message
|
||||
const { ui, model, doc } = scene
|
||||
if (ui.hint) return { tone: 'error', text: ui.hint }
|
||||
if (message?.tone === 'error') {
|
||||
@@ -509,6 +529,10 @@ function useStripNotice(
|
||||
if (scene.selected && issues.length > 0) {
|
||||
return { tone: 'error', text: issues[0]!, more: issues.length - 1 }
|
||||
}
|
||||
// R-18: a « + » hidden for its short segment; never in a mode (move, redraw).
|
||||
if (work.insertsHidden && !inMode && !ui.tracing) {
|
||||
return { tone: 'info', text: t('editor.annotations.strip.zoom_hint') }
|
||||
}
|
||||
if (!model.measured) return unmeasuredNotice(scene, t)
|
||||
if (doc.categories.length === 0) {
|
||||
return { tone: 'info', text: t('editor.annotations.no_category') }
|
||||
@@ -534,54 +558,3 @@ function unmeasuredNotice(
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const COMMAND = 'min-h-11 xl:min-h-9'
|
||||
|
||||
/** While a zone or link is traced: close (3 vertices at least), take one back, or drop it. */
|
||||
function TracingCommands({
|
||||
count,
|
||||
placement,
|
||||
onCancel,
|
||||
}: {
|
||||
count: number
|
||||
placement: Placement
|
||||
onCancel: () => void
|
||||
}) {
|
||||
const t = useT()
|
||||
return (
|
||||
<div
|
||||
tabIndex={-1}
|
||||
data-testid="tracing-commands"
|
||||
className="flex items-center gap-2 outline-none"
|
||||
>
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
className={COMMAND}
|
||||
disabled={count < 3}
|
||||
onClick={placement.closeTracing}
|
||||
>
|
||||
{t('editor.annotations.trace_close')}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
variant="secondary"
|
||||
className={COMMAND}
|
||||
disabled={count === 0}
|
||||
onClick={placement.popVertex}
|
||||
>
|
||||
{t('editor.annotations.trace_pop')}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
className={COMMAND}
|
||||
onClick={onCancel}
|
||||
>
|
||||
{t('editor.annotations.trace_cancel')}
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
useEffect,
|
||||
useLayoutEffect,
|
||||
useRef,
|
||||
useState,
|
||||
@@ -13,6 +14,11 @@ import {
|
||||
type HandleSizes,
|
||||
} from '@/editor/annotations/handleSize'
|
||||
import { useAnnotationWork } from '@/editor/annotations/workStore'
|
||||
import ZoneHandles, {
|
||||
shownInserts,
|
||||
type ZoneHandle,
|
||||
type ZoneKeys,
|
||||
} from './ZoneHandles'
|
||||
|
||||
/** The selected point (or link circle) whose handle the layer draws. */
|
||||
export interface PointHandle {
|
||||
@@ -30,6 +36,13 @@ export interface HandleLayerProps {
|
||||
handle: PointHandle | null
|
||||
onKeyDown: (event: KeyboardEvent<SVGGElement>) => void
|
||||
onBlur: (event: FocusEvent<SVGGElement>) => void
|
||||
/** The selected polygon under the cap, and its handles' keys. */
|
||||
zone: ZoneHandle | null
|
||||
zoneKeys: ZoneKeys
|
||||
/** The canvas-local drag preview: the handles follow it. */
|
||||
dragged: Geometry | null
|
||||
/** Whether a « + » is hidden for its short segment (R-18's hint). */
|
||||
onInsertsHidden: (hidden: boolean) => void
|
||||
}
|
||||
|
||||
/** The svg's own box in CSS px before the framing's transform. */
|
||||
@@ -79,6 +92,8 @@ export default function HandleLayer(props: HandleLayerProps) {
|
||||
const svg = useRef<SVGSVGElement | null>(null)
|
||||
const unit = unitsPerPx(props.viewBox, useBoxSize(svg), props.scale)
|
||||
const sizes = useHandleSizes()
|
||||
const zone = useShownZone(props.zone, props.dragged)
|
||||
useReportHidden(zone, { unit, sizes }, props.onInsertsHidden)
|
||||
return (
|
||||
<svg
|
||||
ref={svg}
|
||||
@@ -95,10 +110,40 @@ export default function HandleLayer(props: HandleLayerProps) {
|
||||
unit={unit}
|
||||
/>
|
||||
)}
|
||||
{zone && (
|
||||
<ZoneHandles
|
||||
zone={zone}
|
||||
scale={{ unit, sizes }}
|
||||
keys={props.zoneKeys}
|
||||
/>
|
||||
)}
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
/** The polygon as shown now: the drag in flight, the pending preview, else the stored one. */
|
||||
function useShownZone(
|
||||
zone: ZoneHandle | null,
|
||||
dragged: Geometry | null,
|
||||
): ZoneHandle | null {
|
||||
const id = zone?.annotationId ?? null
|
||||
const preview = useAnnotationWork((s) =>
|
||||
id !== null && s.preview?.annotationId === id ? s.preview.geometry : null,
|
||||
)
|
||||
const shown = dragged ?? preview
|
||||
if (!zone || shown?.type !== 'polygon') return zone
|
||||
return { ...zone, points: shown.points }
|
||||
}
|
||||
|
||||
function useReportHidden(
|
||||
zone: ZoneHandle | null,
|
||||
scale: Parameters<typeof shownInserts>[1],
|
||||
report: (hidden: boolean) => void,
|
||||
) {
|
||||
const hidden = zone ? shownInserts(zone.points, scale).includes(false) : false
|
||||
useEffect(() => report(hidden), [hidden, report])
|
||||
}
|
||||
|
||||
function PointHandleShape({
|
||||
handle,
|
||||
unit,
|
||||
|
||||
@@ -3,19 +3,15 @@ import { flushSync } from 'react-dom'
|
||||
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 { useDeselect } from '@/editor/annotations/deselect'
|
||||
import EditorGlyph from '@/components/editor/EditorGlyph'
|
||||
import { isEditable } from '@/editor/annotations/geometryEdit'
|
||||
import { beginRedraw } from '@/editor/annotations/redraw'
|
||||
import type { UiAction } from '@/editor/annotations/uiState'
|
||||
import { geometryErrorsById } from '@/editor/issues'
|
||||
import { focusRow } from '@/editor/focusRequest'
|
||||
@@ -26,9 +22,7 @@ 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 {
|
||||
@@ -39,7 +33,6 @@ export interface PropertiesPanelProps {
|
||||
/** The view is not measured: geometry is shown as text only, the rest stays editable. */
|
||||
geometryLocked: boolean
|
||||
dispatch: Dispatch<UiAction>
|
||||
context: CommitContext
|
||||
navigate: EditorPlaceProps['navigate']
|
||||
/** Clears the selection: the overview shows again. */
|
||||
onShowAll: () => void
|
||||
@@ -142,25 +135,22 @@ function DeselectButton({
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* The geometry's state: no coordinate field (A8, U2 Task 5: circles and polygons are
|
||||
* edited on the drawing); a path or a missing geometry says so and offers the redraw.
|
||||
*/
|
||||
function GeometrySection(props: PropertiesPanelProps) {
|
||||
const t = useT()
|
||||
const { annotation, readOnly, geometryLocked } = props
|
||||
const locked = readOnly || geometryLocked
|
||||
const { geometry } = annotation
|
||||
const editable = isEditable(geometry)
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
{geometryLocked && !readOnly && (
|
||||
<UnmeasuredHint navigate={props.navigate} viewId={annotation.view_id} />
|
||||
)}
|
||||
{isEditable(geometry) ? (
|
||||
<GeometryGroup
|
||||
key={annotation.id}
|
||||
idPrefix={`${props.idPrefix ?? ''}annotation`}
|
||||
geometry={geometry}
|
||||
locked={locked}
|
||||
onApply={(next) => applyGeometry(annotation, next, props.context)}
|
||||
/>
|
||||
) : (
|
||||
{!editable && (
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t(
|
||||
geometry === null
|
||||
@@ -169,7 +159,7 @@ function GeometrySection(props: PropertiesPanelProps) {
|
||||
)}
|
||||
</p>
|
||||
)}
|
||||
{!locked && !isEditable(geometry) && (
|
||||
{!locked && !editable && (
|
||||
<TraceReplacement id={annotation.id} dispatch={props.dispatch} />
|
||||
)}
|
||||
<GeometryIssues id={annotation.id} />
|
||||
@@ -177,15 +167,6 @@ function GeometrySection(props: PropertiesPanelProps) {
|
||||
)
|
||||
}
|
||||
|
||||
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,
|
||||
@@ -199,9 +180,8 @@ function TraceReplacement({
|
||||
type="button"
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onClick={() =>
|
||||
dispatch({ type: 'trace_start', target: 'replace', annotationId: id })
|
||||
}
|
||||
// The same redraw as « Redessiner »: the drawing guard asks before it is lost.
|
||||
onClick={() => beginRedraw(id, dispatch)}
|
||||
>
|
||||
{t('editor.annotations.replace_polygon')}
|
||||
</Button>
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import Button from '@/components/ui/Button'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
const COMMAND = 'min-h-11 xl:min-h-9'
|
||||
|
||||
export interface TracingCommandsProps {
|
||||
count: number
|
||||
onClose: () => void
|
||||
onPop: () => void
|
||||
onCancel: () => void
|
||||
}
|
||||
|
||||
/** While a zone or link is traced: close (3 vertices at least), take one back, or drop it. */
|
||||
export default function TracingCommands({
|
||||
count,
|
||||
onClose,
|
||||
onPop,
|
||||
onCancel,
|
||||
}: TracingCommandsProps) {
|
||||
const t = useT()
|
||||
return (
|
||||
<div
|
||||
tabIndex={-1}
|
||||
data-testid="tracing-commands"
|
||||
className="flex items-center gap-2 outline-none"
|
||||
>
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
className={COMMAND}
|
||||
disabled={count < 3}
|
||||
onClick={onClose}
|
||||
>
|
||||
{t('editor.annotations.trace_close')}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
variant="secondary"
|
||||
className={COMMAND}
|
||||
disabled={count === 0}
|
||||
onClick={onPop}
|
||||
>
|
||||
{t('editor.annotations.trace_pop')}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
className={COMMAND}
|
||||
onClick={onCancel}
|
||||
>
|
||||
{t('editor.annotations.trace_cancel')}
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
import type { Dispatch } from 'react'
|
||||
import type { Annotation } from '@faunosteo/core-wasm/types'
|
||||
import Button from '@/components/ui/Button'
|
||||
import { beginRedraw } from '@/editor/annotations/redraw'
|
||||
import type { UiAction } from '@/editor/annotations/uiState'
|
||||
import type { PointEdit } from '@/editor/annotations/usePointEdit'
|
||||
import {
|
||||
useAnnotationWork,
|
||||
type ModeWork,
|
||||
} from '@/editor/annotations/workStore'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import EditorGlyph from '../EditorGlyph'
|
||||
import MoveModeBar from './MoveModeBar'
|
||||
|
||||
const COMMAND = 'min-h-11 gap-1.5 xl:min-h-9'
|
||||
|
||||
/** The move mode's title: « Déplacer le sommet », « … la zone » or « … le point ». */
|
||||
export function moveTitleKey(
|
||||
mode: ModeWork,
|
||||
kind: Annotation['kind'] | undefined,
|
||||
) {
|
||||
if (mode.vertex !== null) return 'editor.annotations.move.vertex'
|
||||
return kind === 'zone'
|
||||
? 'editor.annotations.move.zone'
|
||||
: 'editor.annotations.move.point'
|
||||
}
|
||||
|
||||
/** The chosen vertex of `annotationId`, as the work store holds it. */
|
||||
export function useChosenVertex(
|
||||
annotationId: string | undefined,
|
||||
): number | null {
|
||||
return useAnnotationWork((s) =>
|
||||
annotationId !== undefined && s.chosen?.annotationId === annotationId
|
||||
? s.chosen.vertex
|
||||
: null,
|
||||
)
|
||||
}
|
||||
|
||||
export interface ZoneCommandsProps {
|
||||
/** The selected polygon, movable now. */
|
||||
annotation: Annotation
|
||||
edit: PointEdit
|
||||
dispatch: Dispatch<UiAction>
|
||||
}
|
||||
|
||||
/**
|
||||
* The strip's commands for a selected polygon (board 05): « Déplacer la zone » (« Déplacer
|
||||
* le sommet » while a vertex is chosen) and « Redessiner »; during the move mode, its bar
|
||||
* (board 06 B). A link traced as a polygon gets « Redessiner » only (no move label).
|
||||
*/
|
||||
export default function ZoneCommands({
|
||||
annotation,
|
||||
edit,
|
||||
dispatch,
|
||||
}: ZoneCommandsProps) {
|
||||
const t = useT()
|
||||
const chosen = useChosenVertex(annotation.id)
|
||||
const inMode = useAnnotationWork(
|
||||
(s) => s.mode?.kind === 'move' && s.mode.annotationId === annotation.id,
|
||||
)
|
||||
if (inMode) {
|
||||
return (
|
||||
<MoveModeBar
|
||||
onStep={edit.stepMove}
|
||||
onConfirm={edit.confirmMove}
|
||||
onCancel={edit.cancelMove}
|
||||
/>
|
||||
)
|
||||
}
|
||||
const moveKey = chosen === null ? 'move.zone' : 'move.vertex'
|
||||
return (
|
||||
<>
|
||||
{annotation.kind === 'zone' && (
|
||||
<Button
|
||||
// R-12: the focus comes back here when the mode ends (useMoveFocusReturn).
|
||||
data-move-entry=""
|
||||
type="button"
|
||||
size="sm"
|
||||
variant="secondary"
|
||||
className={COMMAND}
|
||||
onClick={edit.startMove}
|
||||
>
|
||||
<EditorGlyph name="move" size={16} />
|
||||
{t(`editor.annotations.${moveKey}`)}
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
variant="secondary"
|
||||
className={COMMAND}
|
||||
onClick={() => beginRedraw(annotation.id, dispatch)}
|
||||
>
|
||||
<EditorGlyph name="zone" size={16} />
|
||||
{t('editor.annotations.redraw')}
|
||||
</Button>
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,325 @@
|
||||
import { useLayoutEffect, useRef, type KeyboardEvent } from 'react'
|
||||
import type { Point } from '@faunosteo/core-wasm/types'
|
||||
import type { HandleSizes } from '@/editor/annotations/handleSize'
|
||||
import { MIN_VERTICES, showsInsert } from '@/editor/annotations/polygonEdit'
|
||||
import type { ZoneEdit } from '@/editor/annotations/useZoneEdit'
|
||||
import { useAnnotationWork } from '@/editor/annotations/workStore'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/** The selected polygon whose vertex, « + » and « − » handles the layer draws. */
|
||||
export interface ZoneHandle {
|
||||
annotationId: string
|
||||
points: Point[]
|
||||
}
|
||||
|
||||
export type ZoneKeys = Pick<
|
||||
ZoneEdit,
|
||||
| 'onVertexKey'
|
||||
| 'onVertexFocus'
|
||||
| 'onVertexBlur'
|
||||
| 'onInsertKey'
|
||||
| 'onRemoveKey'
|
||||
>
|
||||
|
||||
interface Scale {
|
||||
/** Drawing units per CSS px. */
|
||||
unit: number
|
||||
sizes: HandleSizes
|
||||
}
|
||||
|
||||
const segmentPx = (a: Point, b: Point, unit: number) =>
|
||||
Math.hypot(b[0] - a[0], b[1] - a[1]) / unit
|
||||
|
||||
/** R-9: the « + » of segment `i` (vertex i to the next) shows at the current zoom. */
|
||||
export function shownInserts(points: readonly Point[], { unit, sizes }: Scale) {
|
||||
return points.map((p, i) =>
|
||||
showsInsert(segmentPx(p, points[(i + 1) % points.length], unit), sizes.hit),
|
||||
)
|
||||
}
|
||||
|
||||
const RING = 'invisible group-focus-visible:visible'
|
||||
|
||||
function FocusRing({ cx, cy, r }: { cx: number; cy: number; r: number }) {
|
||||
return (
|
||||
<circle
|
||||
cx={cx}
|
||||
cy={cy}
|
||||
r={r}
|
||||
fill="none"
|
||||
stroke="var(--color-accent)"
|
||||
strokeWidth={2}
|
||||
vectorEffect="non-scaling-stroke"
|
||||
className={RING}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
const LINE = { vectorEffect: 'non-scaling-stroke', strokeWidth: 2 } as const
|
||||
|
||||
function Insert({
|
||||
at,
|
||||
after,
|
||||
label,
|
||||
scale,
|
||||
onKeyDown,
|
||||
}: {
|
||||
at: [number, number]
|
||||
after: number
|
||||
label: string
|
||||
scale: Scale
|
||||
onKeyDown: (event: KeyboardEvent) => void
|
||||
}) {
|
||||
const { unit, sizes } = scale
|
||||
const [cx, cy] = at
|
||||
const r = (sizes.insert / 2) * unit
|
||||
const arm = r * 0.5
|
||||
return (
|
||||
<g
|
||||
role="button"
|
||||
tabIndex={0}
|
||||
aria-label={label}
|
||||
className="group outline-none"
|
||||
onKeyDown={onKeyDown}
|
||||
>
|
||||
<circle
|
||||
cx={cx}
|
||||
cy={cy}
|
||||
r={r}
|
||||
fill="white"
|
||||
stroke="var(--color-accent)"
|
||||
{...LINE}
|
||||
/>
|
||||
<path
|
||||
d={`M ${cx - arm} ${cy} H ${cx + arm} M ${cx} ${cy - arm} V ${cy + arm}`}
|
||||
stroke="var(--color-accent)"
|
||||
{...LINE}
|
||||
/>
|
||||
<circle
|
||||
cx={cx}
|
||||
cy={cy}
|
||||
r={Math.max(sizes.hit * unit, r)}
|
||||
data-handle={JSON.stringify({ kind: 'insert', after })}
|
||||
fill="transparent"
|
||||
className="pointer-events-auto cursor-copy"
|
||||
/>
|
||||
<FocusRing cx={cx} cy={cy} r={r + 3 * unit} />
|
||||
</g>
|
||||
)
|
||||
}
|
||||
|
||||
function Vertex({
|
||||
at,
|
||||
index,
|
||||
chosen,
|
||||
label,
|
||||
scale,
|
||||
keys,
|
||||
}: {
|
||||
at: Point
|
||||
index: number
|
||||
chosen: boolean
|
||||
label: string
|
||||
scale: Scale
|
||||
keys: ZoneKeys
|
||||
}) {
|
||||
const { unit, sizes } = scale
|
||||
const [cx, cy] = at
|
||||
// A chosen vertex is bigger, filled and ringed: never told by colour alone.
|
||||
const r = (sizes.vertex / 2 + (chosen ? 2 : 0)) * unit
|
||||
return (
|
||||
<g
|
||||
role="button"
|
||||
tabIndex={0}
|
||||
aria-label={label}
|
||||
aria-pressed={chosen}
|
||||
data-vertex={index}
|
||||
// Its arrow series commits on blur; the drawing guard blurs it before leaving.
|
||||
data-series=""
|
||||
className="group outline-none"
|
||||
onKeyDown={(event) => keys.onVertexKey(index, event)}
|
||||
onFocus={() => keys.onVertexFocus(index)}
|
||||
onBlur={keys.onVertexBlur}
|
||||
>
|
||||
{chosen && (
|
||||
<circle
|
||||
cx={cx}
|
||||
cy={cy}
|
||||
r={r + 3 * unit}
|
||||
fill="none"
|
||||
stroke="var(--color-accent)"
|
||||
{...LINE}
|
||||
/>
|
||||
)}
|
||||
<circle
|
||||
cx={cx}
|
||||
cy={cy}
|
||||
r={r}
|
||||
fill={chosen ? 'var(--color-accent)' : 'white'}
|
||||
stroke="var(--color-ink)"
|
||||
{...LINE}
|
||||
/>
|
||||
<circle
|
||||
cx={cx}
|
||||
cy={cy}
|
||||
r={Math.max(sizes.hit * unit, r)}
|
||||
data-handle={JSON.stringify({ kind: 'vertex', index })}
|
||||
fill="transparent"
|
||||
className="pointer-events-auto cursor-move"
|
||||
/>
|
||||
<FocusRing cx={cx} cy={cy} r={r + 6 * unit} />
|
||||
</g>
|
||||
)
|
||||
}
|
||||
|
||||
/** The « − » bubble over the chosen vertex (board 05), greyed at three vertices (12 B). */
|
||||
function Remove({
|
||||
at,
|
||||
index,
|
||||
disabled,
|
||||
label,
|
||||
scale,
|
||||
onKeyDown,
|
||||
}: {
|
||||
at: Point
|
||||
index: number
|
||||
disabled: boolean
|
||||
label: string
|
||||
scale: Scale
|
||||
onKeyDown: (event: KeyboardEvent) => void
|
||||
}) {
|
||||
const { unit, sizes } = scale
|
||||
const w = sizes.bubble.w * unit
|
||||
const h = sizes.bubble.h * unit
|
||||
const x = at[0] - w / 2
|
||||
const y = at[1] - (sizes.vertex / 2 + 6) * unit - h
|
||||
const fill = disabled
|
||||
? 'var(--color-ink-muted)'
|
||||
: 'var(--color-danger-strong)'
|
||||
return (
|
||||
<g
|
||||
role="button"
|
||||
tabIndex={0}
|
||||
aria-label={label}
|
||||
aria-disabled={disabled}
|
||||
className="group outline-none"
|
||||
onKeyDown={onKeyDown}
|
||||
>
|
||||
<rect
|
||||
x={x}
|
||||
y={y}
|
||||
width={w}
|
||||
height={h}
|
||||
rx={6 * unit}
|
||||
fill={fill}
|
||||
data-handle={JSON.stringify({ kind: 'remove', index })}
|
||||
className={`pointer-events-auto ${disabled ? 'cursor-not-allowed' : 'cursor-pointer'}`}
|
||||
/>
|
||||
<path
|
||||
d={`M ${x + w * 0.3} ${y + h / 2} H ${x + w * 0.7}`}
|
||||
stroke="white"
|
||||
{...LINE}
|
||||
/>
|
||||
<rect
|
||||
x={x - 3 * unit}
|
||||
y={y - 3 * unit}
|
||||
width={w + 6 * unit}
|
||||
height={h + 6 * unit}
|
||||
rx={8 * unit}
|
||||
fill="none"
|
||||
stroke="var(--color-accent)"
|
||||
{...LINE}
|
||||
className={RING}
|
||||
/>
|
||||
</g>
|
||||
)
|
||||
}
|
||||
|
||||
/** The chosen vertex of this polygon, as the store holds it now. */
|
||||
function useChosen(annotationId: string, count: number): number | null {
|
||||
return useAnnotationWork((s) => {
|
||||
const chosen = s.chosen
|
||||
if (chosen?.annotationId !== annotationId) return null
|
||||
return chosen.vertex < count ? chosen.vertex : null
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* A keyboard choice (« + », Insert, Delete) moves the focus to the vertex now chosen, so
|
||||
* the keys keep acting where the user looks; a pointer choice leaves the focus alone.
|
||||
*/
|
||||
function useFocusFollowsChoice(chosen: number | null) {
|
||||
const group = useRef<SVGGElement | null>(null)
|
||||
useLayoutEffect(() => {
|
||||
const active = document.activeElement
|
||||
if (chosen === null || !group.current?.contains(active)) return
|
||||
if (active?.getAttribute('data-vertex') === String(chosen)) return
|
||||
const target = group.current.querySelector(`[data-vertex="${chosen}"]`)
|
||||
if (target instanceof SVGElement) target.focus()
|
||||
}, [chosen])
|
||||
return group
|
||||
}
|
||||
|
||||
const midpoint = (a: Point, b: Point): [number, number] => [
|
||||
(a[0] + b[0]) / 2,
|
||||
(a[1] + b[1]) / 2,
|
||||
]
|
||||
|
||||
/**
|
||||
* The polygon's handles (docs/ANNOTATION-RENDERING.md « Editor overlay »): the « + » of the
|
||||
* segments long enough on screen, every vertex, then the « − » bubble of the chosen one,
|
||||
* in that order (Tab walks them so). No handle covers the interior.
|
||||
*/
|
||||
export default function ZoneHandles({
|
||||
zone,
|
||||
scale,
|
||||
keys,
|
||||
}: {
|
||||
zone: ZoneHandle
|
||||
scale: Scale
|
||||
keys: ZoneKeys
|
||||
}) {
|
||||
const t = useT()
|
||||
const { points } = zone
|
||||
const chosen = useChosen(zone.annotationId, points.length)
|
||||
const group = useFocusFollowsChoice(chosen)
|
||||
const inserts = shownInserts(points, scale)
|
||||
const named = (key: string, n: number) =>
|
||||
t(`editor.annotations.handle.${key}`).replace('{n}', String(n + 1))
|
||||
return (
|
||||
<g ref={group} data-testid="zone-handles">
|
||||
{points.map((p, i) =>
|
||||
inserts[i] ? (
|
||||
<Insert
|
||||
key={`insert-${i}`}
|
||||
at={midpoint(p, points[(i + 1) % points.length])}
|
||||
after={i}
|
||||
label={named('insert', i)}
|
||||
scale={scale}
|
||||
onKeyDown={(event) => keys.onInsertKey(i, event)}
|
||||
/>
|
||||
) : null,
|
||||
)}
|
||||
{points.map((p, i) => (
|
||||
<Vertex
|
||||
key={`vertex-${i}`}
|
||||
at={p}
|
||||
index={i}
|
||||
chosen={chosen === i}
|
||||
label={named('vertex', i)}
|
||||
scale={scale}
|
||||
keys={keys}
|
||||
/>
|
||||
))}
|
||||
{chosen !== null && (
|
||||
<Remove
|
||||
at={points[chosen]}
|
||||
index={chosen}
|
||||
disabled={points.length <= MIN_VERTICES}
|
||||
label={named('remove', chosen)}
|
||||
scale={scale}
|
||||
onKeyDown={(event) => keys.onRemoveKey(chosen, event)}
|
||||
/>
|
||||
)}
|
||||
</g>
|
||||
)
|
||||
}
|
||||
@@ -208,7 +208,6 @@ function ReadOnlyAnnotations({
|
||||
readOnly
|
||||
geometryLocked={!model.measured}
|
||||
dispatch={() => undefined}
|
||||
context={{ t: (key) => key, locale }}
|
||||
navigate={navigate}
|
||||
onShowAll={() => setSelectedId(null)}
|
||||
/>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { Geometry } from '@faunosteo/core-wasm/types'
|
||||
import { round2, type XY } from './frame'
|
||||
import { moveVertex } from './polygonEdit'
|
||||
|
||||
/** A circle or polygon translated as a block by a drawing-unit delta, rounded to 0.01. */
|
||||
export function translatedGeometry(
|
||||
@@ -25,6 +26,21 @@ export function translatedGeometry(
|
||||
return null
|
||||
}
|
||||
|
||||
/** `geometry` moved by `delta`: as a block, or only its vertex `vertex` (a polygon's). */
|
||||
export function movedBy(
|
||||
geometry: Geometry,
|
||||
delta: XY,
|
||||
vertex: number | null,
|
||||
): Geometry | null {
|
||||
if (vertex === null) return translatedGeometry(geometry, delta)
|
||||
if (geometry.type !== 'polygon') return null
|
||||
const at = geometry.points[vertex]
|
||||
if (!at) return null
|
||||
const to = { x: at[0] + delta.x, y: at[1] + delta.y }
|
||||
const points = moveVertex(geometry.points, vertex, to)
|
||||
return points && { type: 'polygon', points }
|
||||
}
|
||||
|
||||
interface Matrix {
|
||||
a: number
|
||||
b: number
|
||||
@@ -50,3 +66,9 @@ export function clientToDrawing(
|
||||
y: inverse.b * client.x + inverse.d * client.y + inverse.f,
|
||||
}
|
||||
}
|
||||
|
||||
/** Drawing units per client px, through the same matrix; null without one. */
|
||||
export function drawingPerClientPx(svg: SVGSVGElement | null): number | null {
|
||||
const inverse: Matrix | undefined = svg?.getScreenCTM?.()?.inverse()
|
||||
return inverse ? Math.hypot(inverse.a, inverse.b) : null
|
||||
}
|
||||
@@ -6,10 +6,24 @@ export interface HandleSizes {
|
||||
insert: number
|
||||
/** The radius around a handle's centre that a press reaches. */
|
||||
hit: number
|
||||
/** The « − » bubble over the chosen vertex. */
|
||||
bubble: { w: number; h: number }
|
||||
}
|
||||
|
||||
const FINE: HandleSizes = { vertex: 12, insert: 16, hit: 10 }
|
||||
const COARSE: HandleSizes = { vertex: 18, insert: 24, hit: 22 }
|
||||
// shortcut: the fine bubble size is read off board 05 (no number given by R-9). Upgrade:
|
||||
// take the owner's value if the trial asks for another.
|
||||
const FINE: HandleSizes = {
|
||||
vertex: 12,
|
||||
insert: 16,
|
||||
hit: 10,
|
||||
bubble: { w: 28, h: 22 },
|
||||
}
|
||||
const COARSE: HandleSizes = {
|
||||
vertex: 18,
|
||||
insert: 24,
|
||||
hit: 22,
|
||||
bubble: { w: 44, h: 36 },
|
||||
}
|
||||
|
||||
function deviceSizes(): HandleSizes {
|
||||
const coarse =
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import { useEffect } from 'react'
|
||||
import { usePlaceVisible } from '@/lib/placeVisibility'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import type { GestureMessages } from './useGestureMessage'
|
||||
import { endMode, useAnnotationWork } from './workStore'
|
||||
|
||||
/**
|
||||
* The move mode belongs to the selection: another selection (a list row, a view change)
|
||||
* ends it. A read-only flip keeps the body and its selection, so it suspends the mode
|
||||
* instead (R-14).
|
||||
*/
|
||||
export function useModeFollowsSelection(selectedId: string | null) {
|
||||
const modeId = useAnnotationWork((s) => s.mode?.annotationId ?? null)
|
||||
useEffect(() => {
|
||||
if (modeId === null || modeId === selectedId) return
|
||||
const { mode } = useAnnotationWork.getState()
|
||||
endMode()
|
||||
// Never silent (R-13): a started move that ends this way is said.
|
||||
if (mode?.started) useAnnotationWork.setState({ dropped: mode.kind })
|
||||
}, [modeId, selectedId])
|
||||
}
|
||||
|
||||
/**
|
||||
* R-13: a started mode a change made elsewhere dropped is said on the line, once the body
|
||||
* is shown (a drop while read-only is said on the way back).
|
||||
*/
|
||||
export function useDroppedNotice(message: GestureMessages) {
|
||||
const t = useT()
|
||||
const visible = usePlaceVisible()
|
||||
const dropped = useAnnotationWork((s) => s.dropped)
|
||||
const show = message.show
|
||||
useEffect(() => {
|
||||
if (dropped === null || !visible) return
|
||||
useAnnotationWork.setState({ dropped: null })
|
||||
const text = t(`editor.annotations.dropped.${dropped}`)
|
||||
show(text, { undo: false, tone: 'info' })
|
||||
}, [dropped, visible, show, t])
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import type { Point } from '@faunosteo/core-wasm/types'
|
||||
import { round2, type XY } from './frame'
|
||||
|
||||
/** T18 (R-7): above this many vertices, outline and message, no handle. */
|
||||
export const HANDLE_CAP = 40
|
||||
export const MIN_VERTICES = 3
|
||||
|
||||
const inRange = (points: readonly Point[], i: number) =>
|
||||
Number.isInteger(i) && i >= 0 && i < points.length
|
||||
|
||||
/** « + » on the segment from vertex `after` to the next (wrapping): its midpoint, 0.01 grid. */
|
||||
export function insertMidpoint(
|
||||
points: readonly Point[],
|
||||
after: number,
|
||||
): Point[] | null {
|
||||
if (!inRange(points, after)) return null
|
||||
const [ax, ay] = points[after]
|
||||
const [bx, by] = points[(after + 1) % points.length]
|
||||
const middle: Point = [round2((ax + bx) / 2), round2((ay + by) / 2)]
|
||||
return [...points.slice(0, after + 1), middle, ...points.slice(after + 1)]
|
||||
}
|
||||
|
||||
/** « − »: null at 3 vertices (board 12 B reason shown by the caller). */
|
||||
export function removeVertex(
|
||||
points: readonly Point[],
|
||||
index: number,
|
||||
): Point[] | null {
|
||||
if (!inRange(points, index) || points.length <= MIN_VERTICES) return null
|
||||
return points.filter((_, i) => i !== index)
|
||||
}
|
||||
|
||||
export function moveVertex(
|
||||
points: readonly Point[],
|
||||
index: number,
|
||||
to: XY,
|
||||
): Point[] | null {
|
||||
if (
|
||||
!inRange(points, index) ||
|
||||
!Number.isFinite(to.x) ||
|
||||
!Number.isFinite(to.y)
|
||||
)
|
||||
return null
|
||||
return points.map(
|
||||
(p, i): Point => (i === index ? [round2(to.x), round2(to.y)] : p),
|
||||
)
|
||||
}
|
||||
|
||||
/** A vertex released on itself or on a neighbour is no change (no commit). */
|
||||
export function isNoOpMove(
|
||||
points: readonly Point[],
|
||||
index: number,
|
||||
moved: readonly Point[],
|
||||
): boolean {
|
||||
const n = points.length
|
||||
const [x, y] = moved[index]
|
||||
return [index, (index + 1) % n, (index + n - 1) % n].some(
|
||||
(j) => points[j][0] === x && points[j][1] === y,
|
||||
)
|
||||
}
|
||||
|
||||
/** R-9: a « + » shows only when its segment is at least two hit radii long on screen. */
|
||||
export function showsInsert(segmentPx: number, hitRadiusPx: number): boolean {
|
||||
return segmentPx >= 2 * hitRadiusPx
|
||||
}
|
||||
@@ -7,6 +7,12 @@ export const SLOP_PX: Readonly<Record<PointerKind, number>> = {
|
||||
pen: 6,
|
||||
touch: 10,
|
||||
}
|
||||
/** R-11: CSS px from the first vertex within which a click closes a tracing. */
|
||||
export const CLOSE_PX: Readonly<Record<PointerKind, number>> = {
|
||||
mouse: 12,
|
||||
pen: 22,
|
||||
touch: 22,
|
||||
}
|
||||
export const DOUBLE_MS = 400
|
||||
export const DOUBLE_PX = 8
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Geometry } from '@faunosteo/core-wasm/types'
|
||||
import { useEditorStore } from '../state/editorStore'
|
||||
import { translatedGeometry } from './gesture'
|
||||
import { movedBy } from './gesture'
|
||||
import {
|
||||
endMode,
|
||||
setModePreview,
|
||||
@@ -42,19 +42,22 @@ export function shownGeometry(
|
||||
}
|
||||
|
||||
/**
|
||||
* One step of the selection by `delta` drawing units: into the move mode when it is on,
|
||||
* else as a keyboard series (`keys`), stamped with the current text.
|
||||
* One step of the selection by `delta` drawing units: into the move mode when it is on
|
||||
* (its vertex, or the shape), else as a keyboard series (`keys`) of the shape or of
|
||||
* `vertex`, stamped with the current text.
|
||||
*/
|
||||
export function stepSelection(
|
||||
annotationId: string,
|
||||
stored: Geometry,
|
||||
delta: { x: number; y: number },
|
||||
vertex: number | null = null,
|
||||
): void {
|
||||
const moved = translatedGeometry(shownGeometry(annotationId, stored), delta)
|
||||
const { mode } = useAnnotationWork.getState()
|
||||
const inMode = mode?.kind === 'move'
|
||||
const shown = shownGeometry(annotationId, stored)
|
||||
const moved = movedBy(shown, delta, inMode ? mode.vertex : vertex)
|
||||
if (!moved) return
|
||||
if (useAnnotationWork.getState().mode?.kind === 'move') {
|
||||
return setModePreview(moved, stored)
|
||||
}
|
||||
if (inMode) return setModePreview(moved, stored)
|
||||
const base = useEditorStore.getState().documentText
|
||||
const preview: Preview = {
|
||||
annotationId,
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { useEffect, useRef, type Dispatch } from 'react'
|
||||
import { useEditorStore } from '../state/editorStore'
|
||||
import type { Tracing, UiAction } from './uiState'
|
||||
import {
|
||||
endMode,
|
||||
markRedraw,
|
||||
startRedraw,
|
||||
useAnnotationWork,
|
||||
} from './workStore'
|
||||
|
||||
/**
|
||||
* « Redessiner » (or « Remplacer par un polygone »): the redraw mode, which the drawing
|
||||
* guard reads (R-12), and the tracing target `replace` that draws the new polygon.
|
||||
*/
|
||||
export function beginRedraw(id: string, dispatch: Dispatch<UiAction>): void {
|
||||
startRedraw(id, useEditorStore.getState().documentText)
|
||||
dispatch({ type: 'trace_start', target: 'replace', annotationId: id })
|
||||
}
|
||||
|
||||
/**
|
||||
* The redraw lives in two places, the tab's tracing and the work store's mode; whichever
|
||||
* ends first ends the other: a tracing gone (closed, « Annuler le tracé », Escape, a
|
||||
* tool, a view, a selection) ends the mode; a mode gone (« Abandonner », an undo, a
|
||||
* document change made elsewhere) ends the tracing. The mode is started once the
|
||||
* tracing holds a vertex, and no longer when it is back to none.
|
||||
*/
|
||||
export function useRedrawFollowsTracing(
|
||||
tracing: Tracing | null,
|
||||
dispatch: Dispatch<UiAction>,
|
||||
): void {
|
||||
const redraw = useAnnotationWork((s) => s.mode?.kind === 'redraw')
|
||||
const count = tracing?.target === 'replace' ? tracing.points.length : null
|
||||
// Only the body whose tracing it was ends the mode: another body kept alive has none.
|
||||
const traced = useRef(false)
|
||||
useEffect(() => {
|
||||
const was = traced.current
|
||||
traced.current = count !== null
|
||||
if (redraw && count === null && was) endMode()
|
||||
else if (!redraw && count !== null) dispatch({ type: 'trace_cancel' })
|
||||
else if (count !== null) markRedraw(count > 0)
|
||||
}, [redraw, count, dispatch])
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import type { GlyphName } from '@/components/editor/EditorGlyph'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import { pluralKey } from '../plural'
|
||||
import type { PendingElement } from './creation'
|
||||
import { HANDLE_CAP } from './polygonEdit'
|
||||
import type { ToolId, Tracing, UiState } from './uiState'
|
||||
|
||||
/** The strip's left part: what is selected or in progress, then its type and detail. */
|
||||
@@ -24,6 +25,8 @@ export interface LeadContext {
|
||||
locale: Locale
|
||||
/** The sheet cannot be edited now: the detail says so (board 33 E). */
|
||||
readOnly?: boolean
|
||||
/** The chosen vertex of the selected polygon, said after its count. */
|
||||
chosenVertex?: number | null
|
||||
}
|
||||
|
||||
const ICON: Record<Annotation['kind'], GlyphName> = {
|
||||
@@ -45,6 +48,23 @@ function counted(
|
||||
.replace('{kind}', () => kind)
|
||||
}
|
||||
|
||||
/**
|
||||
* « Zone · 9 sommets », « … · sommet 7 choisi », or above the cap (T18) « Zone · 41
|
||||
* sommets, trop pour les poignées ».
|
||||
*/
|
||||
function polygonDetail(count: number, kind: string, ctx: LeadContext): string {
|
||||
if (count > HANDLE_CAP && !ctx.readOnly) {
|
||||
return counted(ctx, 'editor.annotations.strip.too_many', count, kind)
|
||||
}
|
||||
const shape = counted(ctx, 'editor.annotations.strip.vertices', count, kind)
|
||||
const chosen = ctx.chosenVertex
|
||||
if (chosen === null || chosen === undefined || ctx.readOnly) return shape
|
||||
return ctx
|
||||
.t('editor.annotations.strip.vertex_chosen')
|
||||
.replace('{detail}', () => shape)
|
||||
.replace('{n}', String(chosen + 1))
|
||||
}
|
||||
|
||||
/** « Point », « Zone · 9 sommets »; « … · fiche en lecture seule » when read-only. */
|
||||
export function selectionLead(
|
||||
annotation: Annotation,
|
||||
@@ -55,12 +75,7 @@ export function selectionLead(
|
||||
const geometry = annotation.geometry
|
||||
const shape =
|
||||
geometry?.type === 'polygon'
|
||||
? counted(
|
||||
ctx,
|
||||
'editor.annotations.strip.vertices',
|
||||
geometry.points.length,
|
||||
kind,
|
||||
)
|
||||
? polygonDetail(geometry.points.length, kind, ctx)
|
||||
: kind
|
||||
return {
|
||||
title: annotationName(annotation, views, ctx.locale),
|
||||
@@ -74,17 +89,22 @@ export function selectionLead(
|
||||
}
|
||||
}
|
||||
|
||||
/** A tracing: « Ajouter une zone » or « Redessiner · {nom} », then the vertices placed. */
|
||||
/**
|
||||
* A tracing: « Ajouter une zone » or « Redessiner · {nom} », then the vertices placed;
|
||||
* « Fermeture refusée » while the core's refusal of its close shows (board 33 A).
|
||||
*/
|
||||
function tracingLead(
|
||||
tracing: Tracing,
|
||||
replaced: string,
|
||||
ctx: LeadContext,
|
||||
ctx: LeadContext & { refused: boolean },
|
||||
): StripLead {
|
||||
const title =
|
||||
tracing.target === 'replace'
|
||||
? ctx
|
||||
.t('editor.annotations.strip.redraw')
|
||||
.replace('{name}', () => replaced)
|
||||
const redraw = ctx
|
||||
.t('editor.annotations.strip.redraw')
|
||||
.replace('{name}', () => replaced)
|
||||
const title = ctx.refused
|
||||
? ctx.t('editor.annotations.strip.close_refused')
|
||||
: tracing.target === 'replace'
|
||||
? redraw
|
||||
: ctx.t(`editor.annotations.add.${tracing.target}`)
|
||||
return {
|
||||
title,
|
||||
@@ -149,7 +169,7 @@ export function stripLeadOf(
|
||||
(a) => a.id === ui.tracing?.annotationId,
|
||||
)
|
||||
const name = replaced ? annotationName(replaced, doc.views, ctx.locale) : ''
|
||||
return tracingLead(ui.tracing, name, ctx)
|
||||
return tracingLead(ui.tracing, name, { ...ctx, refused: ui.hint !== null })
|
||||
}
|
||||
if (selected) return selectionLead(selected, doc.views, ctx)
|
||||
const lead = TOOL_LEAD[ui.tool]
|
||||
|
||||
@@ -60,6 +60,10 @@ const TOOL_OF: Record<Tracing['target'], ToolId> = {
|
||||
replace: 'zone',
|
||||
}
|
||||
|
||||
/** A redraw lends the zone tool to its tracing: whatever ends it gives the select tool back. */
|
||||
const toolAfterTracing = (state: UiState): ToolId =>
|
||||
state.tracing?.target === 'replace' ? 'select' : state.tool
|
||||
|
||||
export function uiReducer(state: UiState, action: UiAction): UiState {
|
||||
switch (action.type) {
|
||||
case 'view':
|
||||
@@ -69,6 +73,7 @@ export function uiReducer(state: UiState, action: UiAction): UiState {
|
||||
case 'select':
|
||||
return {
|
||||
...state,
|
||||
tool: toolAfterTracing(state),
|
||||
selectedId: action.id,
|
||||
pending: null,
|
||||
tracing: null,
|
||||
@@ -124,7 +129,12 @@ export function uiReducer(state: UiState, action: UiAction): UiState {
|
||||
}
|
||||
: state
|
||||
case 'trace_cancel':
|
||||
return { ...state, tracing: null, hint: null }
|
||||
return {
|
||||
...state,
|
||||
tool: toolAfterTracing(state),
|
||||
tracing: null,
|
||||
hint: null,
|
||||
}
|
||||
case 'hint':
|
||||
return { ...state, hint: action.hint }
|
||||
case 'filter':
|
||||
|
||||
@@ -2,9 +2,12 @@ import { useEffect, useRef, useState, type RefObject } from 'react'
|
||||
import type { Annotation, Geometry } from '@faunosteo/core-wasm/types'
|
||||
import { useEditorStore } from '../state/editorStore'
|
||||
import type { XY } from './frame'
|
||||
import { clientToDrawing, translatedGeometry } from './gesture'
|
||||
import { clientToDrawing, movedBy } from './gesture'
|
||||
import { isNoOpMove } from './polygonEdit'
|
||||
import { shownGeometry } from './previewCommit'
|
||||
import { setModePreview } from './workStore'
|
||||
import type { GeometryChange } from './usePlacement'
|
||||
import { chooseVertex } from './zoneEdit'
|
||||
import { setModePreview, useAnnotationWork } from './workStore'
|
||||
import type { EditableGeometry } from './geometryEdit'
|
||||
|
||||
export interface MoveTarget {
|
||||
@@ -18,7 +21,7 @@ export interface BlockDragInput {
|
||||
/** The selected annotation, when it can be moved (editable, measured, circle or polygon). */
|
||||
target: MoveTarget | null
|
||||
/** One `update` on the drop; the verdict and the message are the caller's. */
|
||||
onDrop: (id: string, geometry: Geometry) => void
|
||||
onDrop: (id: string, geometry: Geometry, change?: GeometryChange) => boolean
|
||||
}
|
||||
|
||||
interface Drag {
|
||||
@@ -29,6 +32,8 @@ interface Drag {
|
||||
base: Geometry
|
||||
/** A drag of the move mode (board 06 B): it moves the mode's preview, never commits. */
|
||||
mode: boolean
|
||||
/** The vertex dragged alone (a vertex handle, or the mode's chosen vertex). */
|
||||
vertex: number | null
|
||||
}
|
||||
|
||||
function translatedAt(
|
||||
@@ -38,10 +43,27 @@ function translatedAt(
|
||||
): Geometry | null {
|
||||
const now = clientToDrawing(overlay.current, client)
|
||||
if (!now) return null
|
||||
return translatedGeometry(drag.base, {
|
||||
x: now.x - drag.origin.x,
|
||||
y: now.y - drag.origin.y,
|
||||
})
|
||||
const delta = { x: now.x - drag.origin.x, y: now.y - drag.origin.y }
|
||||
return movedBy(drag.base, delta, drag.vertex)
|
||||
}
|
||||
|
||||
/** A vertex released on itself or on a neighbour moves nothing: no commit. */
|
||||
function isNoOp(drag: Drag, dropped: Geometry): boolean {
|
||||
const { base, vertex } = drag
|
||||
if (vertex === null || base.type !== 'polygon') return false
|
||||
return (
|
||||
dropped.type === 'polygon' &&
|
||||
isNoOpMove(base.points, vertex, dropped.points)
|
||||
)
|
||||
}
|
||||
|
||||
/** A drop outside the mode commits once: the shape moved, or its vertex. */
|
||||
function commitDrop(input: BlockDragInput, drag: Drag, dropped: Geometry) {
|
||||
if (drag.mode || isNoOp(drag, dropped)) return
|
||||
const { id } = drag.target
|
||||
if (drag.vertex === null) return void input.onDrop(id, dropped, 'move')
|
||||
// The dragged vertex stays chosen on the new text (board 05 B).
|
||||
if (input.onDrop(id, dropped, 'vertex')) chooseVertex(id, drag.vertex)
|
||||
}
|
||||
|
||||
/** Losing the target (read-only, another selection) or a document change drops the drag. */
|
||||
@@ -77,14 +99,24 @@ function dragActions(
|
||||
drag.current = null
|
||||
setPreview(null)
|
||||
}
|
||||
const start = (pressedAt: XY, client: XY, mode = false) => {
|
||||
/** `vertex`: a vertex handle's drag; the move mode drags its own vertex, if any. */
|
||||
const start = (
|
||||
pressedAt: XY,
|
||||
client: XY,
|
||||
mode: boolean,
|
||||
vertex: number | null,
|
||||
) => {
|
||||
const origin = clientToDrawing(input.overlay.current, pressedAt)
|
||||
const { target } = input
|
||||
if (!target || !origin) return
|
||||
const base = mode
|
||||
? shownGeometry(target.id, target.geometry)
|
||||
: target.geometry
|
||||
drag.current = { target, origin, base, mode }
|
||||
const own = mode
|
||||
? (useAnnotationWork.getState().mode?.vertex ?? null)
|
||||
: vertex
|
||||
if (!mode && vertex !== null) chooseVertex(target.id, vertex)
|
||||
drag.current = { target, origin, base, mode, vertex: own }
|
||||
show(translatedAt(input.overlay, drag.current, client))
|
||||
}
|
||||
const follow = (client: XY) => {
|
||||
@@ -98,15 +130,15 @@ function dragActions(
|
||||
if (gesture?.mode) show(dropped)
|
||||
drag.current = null
|
||||
setPreview(null)
|
||||
if (gesture && !gesture.mode && dropped)
|
||||
input.onDrop(gesture.target.id, dropped)
|
||||
if (gesture && dropped) commitDrop(input, gesture, dropped)
|
||||
}
|
||||
return { start, follow, drop, cancel }
|
||||
}
|
||||
|
||||
/**
|
||||
* The selection moved as a block (`drag_shape`, `drag_point`): the overlay previews the
|
||||
* translated outline, kept local to the canvas for speed; the drop commits once. A drag
|
||||
* The selection moved as a block (`drag_shape`, `drag_point`) or one of its vertices
|
||||
* (`drag_vertex`): the overlay previews the outline, kept local to the canvas for speed;
|
||||
* the drop commits once. A drag
|
||||
* of the move mode (`drag_mode`) moves the mode's preview instead, and commits nothing.
|
||||
*/
|
||||
export function useBlockDrag(input: BlockDragInput) {
|
||||
|
||||
@@ -10,12 +10,10 @@ import { updateGeometry, type CommitContext } from './annotationCommit'
|
||||
import { pendingForClick, type NewKind } from './creation'
|
||||
import { round2, type Frame, type XY } from './frame'
|
||||
import { newPointSize } from './pointSize'
|
||||
import { endMode, useAnnotationWork } from './workStore'
|
||||
import type { ToolId, Tracing, UiAction, UiState } from './uiState'
|
||||
import type { GestureMessages } from './useGestureMessage'
|
||||
|
||||
/** A click on the first vertex closes the tracing when within this share of F's span. */
|
||||
const CLOSE_SHARE = 0.015
|
||||
|
||||
const TOOL_TARGET: Record<Exclude<ToolId, 'select'>, NewKind> = {
|
||||
point: 'point',
|
||||
zone: 'zone',
|
||||
@@ -35,47 +33,56 @@ export interface PlacementInput {
|
||||
|
||||
const asPoint = (at: XY): Point => [round2(at.x), round2(at.y)]
|
||||
|
||||
function nearFirstVertex(tracing: Tracing, at: XY, frame: Frame): boolean {
|
||||
/** R-11: `reach` is the closing radius of the pointer, in drawing units. */
|
||||
function nearFirstVertex(tracing: Tracing, at: XY, reach: number): boolean {
|
||||
const first = tracing.points[0]
|
||||
if (!first || tracing.points.length < 3) return false
|
||||
const reach = CLOSE_SHARE * Math.max(frame.w, frame.h)
|
||||
return Math.hypot(first[0] - at.x, first[1] - at.y) <= reach
|
||||
}
|
||||
|
||||
/**
|
||||
* What closing a tracing does: a new zone or link becomes the pending element; a polygon
|
||||
* replacing a geometry is committed at once under `accept`, and a refusal drops the
|
||||
* tracing with the core's message (the annotation is unchanged).
|
||||
* A redraw's close commits the polygon at once. The mode leaves first, so the commit is
|
||||
* never said as an abandoned redraw (R-13); a refusal brings it back and keeps the
|
||||
* tracing open with the core's message (board 33 A).
|
||||
*/
|
||||
export function closingActions(
|
||||
tracing: Tracing | null,
|
||||
doc: AnatomicalSheetDocument,
|
||||
context: CommitContext,
|
||||
): UiAction[] {
|
||||
if (!tracing || tracing.points.length < 3) return []
|
||||
const polygon = { type: 'polygon' as const, points: tracing.points }
|
||||
if (tracing.target !== 'replace') {
|
||||
const pending = { kind: tracing.target, geometry: polygon }
|
||||
return [{ type: 'pending_new', pending }]
|
||||
}
|
||||
function closeRedraw(input: PlacementInput, tracing: Tracing): void {
|
||||
const { doc, context, dispatch } = input
|
||||
const annotation = doc.annotations.find((a) => a.id === tracing.annotationId)
|
||||
if (!annotation) return [{ type: 'trace_cancel' }]
|
||||
if (!annotation) return dispatch({ type: 'trace_cancel' })
|
||||
const held = useAnnotationWork.getState().mode
|
||||
endMode()
|
||||
const polygon = { type: 'polygon' as const, points: tracing.points }
|
||||
const result = updateGeometry(annotation, polygon, context)
|
||||
if (result.ok) return [{ type: 'select', id: annotation.id }]
|
||||
return [
|
||||
{ type: 'trace_cancel' },
|
||||
{ type: 'hint', hint: result.messages.join(' ') },
|
||||
]
|
||||
if (!result.ok) {
|
||||
useAnnotationWork.setState({ mode: held })
|
||||
return dispatch({ type: 'hint', hint: result.messages.join(' ') })
|
||||
}
|
||||
dispatch({ type: 'select', id: annotation.id })
|
||||
const text = context.t(
|
||||
`editor.annotations.message.redrawn.${annotation.kind}`,
|
||||
)
|
||||
input.message.show(text, { undo: true, tone: 'info' })
|
||||
}
|
||||
|
||||
/** Tracing with the zone and link tools: vertices, closing, Enter, Backspace and Escape. */
|
||||
function tracingActions(input: PlacementInput) {
|
||||
const { ui, dispatch, frame } = input
|
||||
const closeTracing = () =>
|
||||
closingActions(ui.tracing, input.doc, input.context).forEach(dispatch)
|
||||
const trace = (tool: Exclude<ToolId, 'select' | 'point'>, at: XY) => {
|
||||
if (!frame) return
|
||||
if (ui.tracing && nearFirstVertex(ui.tracing, at, frame)) {
|
||||
const { ui, dispatch } = input
|
||||
const closeTracing = () => {
|
||||
const tracing = ui.tracing
|
||||
if (!tracing || tracing.points.length < 3) return
|
||||
if (tracing.target === 'replace') return closeRedraw(input, tracing)
|
||||
const pending = {
|
||||
kind: tracing.target,
|
||||
geometry: { type: 'polygon' as const, points: tracing.points },
|
||||
}
|
||||
dispatch({ type: 'pending_new', pending })
|
||||
}
|
||||
const trace = (
|
||||
tool: Exclude<ToolId, 'select' | 'point'>,
|
||||
at: XY,
|
||||
reach: number,
|
||||
) => {
|
||||
if (ui.tracing && nearFirstVertex(ui.tracing, at, reach)) {
|
||||
return closeTracing()
|
||||
}
|
||||
if (!ui.tracing)
|
||||
@@ -97,14 +104,25 @@ function tracingActions(input: PlacementInput) {
|
||||
return { closeTracing, trace, onTraceKey }
|
||||
}
|
||||
|
||||
/** A move (drag, move mode, arrow keys) or a size change of the selection. */
|
||||
export type GeometryChange = 'move' | 'size'
|
||||
/**
|
||||
* A move (drag, move mode, arrow keys) or a size change of the selection, a vertex
|
||||
* moved, added or removed.
|
||||
*/
|
||||
export type GeometryChange = 'move' | 'size' | 'vertex' | 'insert' | 'remove'
|
||||
|
||||
const KEPT: Record<Exclude<GeometryChange, 'move'>, string> = {
|
||||
size: 'resized',
|
||||
vertex: 'vertex_moved',
|
||||
insert: 'vertex_added',
|
||||
remove: 'vertex_removed',
|
||||
}
|
||||
|
||||
/** The message keys of a kept and of a refused change. */
|
||||
function changeKeys(change: GeometryChange, kind: Annotation['kind']) {
|
||||
return change === 'size'
|
||||
? { kept: 'resized', refused: 'size_refused' }
|
||||
: { kept: `moved.${kind}`, refused: 'move_refused' }
|
||||
const kept = change === 'move' ? `moved.${kind}` : KEPT[change]
|
||||
if (change === 'size') return { kept, refused: 'size_refused' }
|
||||
const moved = change === 'move' || change === 'vertex'
|
||||
return { kept, refused: moved ? 'move_refused' : 'edit_refused' }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -112,24 +130,32 @@ function changeKeys(change: GeometryChange, kind: Annotation['kind']) {
|
||||
* with the refused place drawn (the shape stays where it was).
|
||||
*/
|
||||
function dropAction({ doc, context, message, dispatch }: PlacementInput) {
|
||||
return (id: string, geometry: Geometry, change: GeometryChange = 'move') => {
|
||||
return (
|
||||
id: string,
|
||||
geometry: Geometry,
|
||||
change: GeometryChange = 'move',
|
||||
): boolean => {
|
||||
const annotation = doc.annotations.find((a) => a.id === id)
|
||||
if (!annotation) return
|
||||
if (!annotation) return false
|
||||
const keys = changeKeys(change, annotation.kind)
|
||||
const result = updateGeometry(annotation, geometry, context)
|
||||
if (result.ok) {
|
||||
// A kept move ends any refusal the strip still shows.
|
||||
dispatch({ type: 'hint', hint: null })
|
||||
const text = context.t(`editor.annotations.message.${keys.kept}`)
|
||||
return message.show(text, { undo: true, tone: 'info' })
|
||||
message.show(text, { undo: true, tone: 'info' })
|
||||
return true
|
||||
}
|
||||
const text = context
|
||||
.t(`editor.annotations.message.${keys.refused}`)
|
||||
.replace('{text}', () => result.messages.join(' '))
|
||||
message.show(text, { undo: false, tone: 'error', place: geometry })
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
export type Drop = ReturnType<typeof dropAction>
|
||||
|
||||
/**
|
||||
* What a click, a key or a drop does with the current tool. Every geometry it proposes is
|
||||
* arithmetic over F; the core decides at « Créer » or at the commit of an existing one.
|
||||
@@ -139,9 +165,10 @@ export function usePlacement(input: PlacementInput) {
|
||||
const t = useT()
|
||||
const tracing = tracingActions(input)
|
||||
const { ui, dispatch, frame } = input
|
||||
const onPlace = (at: XY) => {
|
||||
/** `reach`: the pointer's closing radius in drawing units (R-11). */
|
||||
const onPlace = (at: XY, reach: number) => {
|
||||
if (ui.tool === 'zone' || ui.tool === 'link')
|
||||
return tracing.trace(ui.tool, at)
|
||||
return tracing.trace(ui.tool, at, reach)
|
||||
if (ui.tool !== 'point' || !frame) return
|
||||
const { doc, viewId } = input
|
||||
const size = newPointSize(doc.annotations, viewId, frame, ui.size)
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import type { KeyboardEvent } from 'react'
|
||||
import type { Annotation, Geometry } from '@faunosteo/core-wasm/types'
|
||||
import type { Annotation } from '@faunosteo/core-wasm/types'
|
||||
import { useEditorStore } from '../state/editorStore'
|
||||
import { moveStep, type Frame } from './frame'
|
||||
import type { GeometryChange } from './usePlacement'
|
||||
import type { Drop } from './usePlacement'
|
||||
import { takeMove, takePreview, stepSelection } from './previewCommit'
|
||||
import { chooseVertex, freshChosen } from './zoneEdit'
|
||||
import {
|
||||
endMode,
|
||||
startMove,
|
||||
@@ -11,10 +12,8 @@ import {
|
||||
type Preview,
|
||||
} from './workStore'
|
||||
|
||||
type Drop = (id: string, geometry: Geometry, change?: GeometryChange) => void
|
||||
|
||||
export interface PointEditInput {
|
||||
/** The selection when it can be moved now (a circle: a point or a link). */
|
||||
/** The selection when it can be moved now: a circle (point, link) or a polygon. */
|
||||
target: Annotation | null
|
||||
frame: Frame | null
|
||||
onDrop: Drop
|
||||
@@ -67,8 +66,13 @@ function commits({ onDrop, target }: PointEditInput) {
|
||||
}
|
||||
}
|
||||
const move = () => {
|
||||
const vertex = useAnnotationWork.getState().mode?.vertex ?? null
|
||||
const preview = takeMove()
|
||||
if (preview) onDrop(preview.annotationId, preview.geometry)
|
||||
if (!preview) return
|
||||
const { annotationId: id, geometry } = preview
|
||||
if (vertex === null) return void onDrop(id, geometry)
|
||||
// The moved vertex stays chosen on the new text.
|
||||
if (onDrop(id, geometry, 'vertex')) chooseVertex(id, vertex)
|
||||
}
|
||||
return { keys, size, move }
|
||||
}
|
||||
@@ -102,9 +106,12 @@ export function usePointEdit(input: PointEditInput) {
|
||||
// BDR-050: leaving the handle commits its series; a move mode waits for « Valider ».
|
||||
onHandleBlur: () => commit.keys(),
|
||||
commitSize: commit.size,
|
||||
// « Déplacer le sommet » while a vertex is chosen, else the whole shape.
|
||||
startMove: () => {
|
||||
if (!input.target) return
|
||||
startMove(input.target.id, useEditorStore.getState().documentText)
|
||||
const id = input.target?.id
|
||||
if (!id) return
|
||||
const base = useEditorStore.getState().documentText
|
||||
startMove(id, base, freshChosen(id))
|
||||
},
|
||||
stepMove: (direction: Direction) => step(input, direction, false),
|
||||
confirmMove: commit.move,
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { useEffect, useRef, type PointerEvent, type RefObject } from 'react'
|
||||
import type { XY } from './frame'
|
||||
import { clientToDrawing } from './gesture'
|
||||
import { clientToDrawing, drawingPerClientPx } from './gesture'
|
||||
import { HANDLE_CAP } from './polygonEdit'
|
||||
import {
|
||||
CLOSE_PX,
|
||||
clickAction,
|
||||
dragAction,
|
||||
isDoubleClick,
|
||||
@@ -28,8 +30,10 @@ export interface PressInput extends BlockDragInput {
|
||||
selectedId: string | null
|
||||
viewport: RefObject<HTMLDivElement | null>
|
||||
onSelect: (id: string) => void
|
||||
/** A click with a drawing tool, in drawing units. */
|
||||
onPlace: (at: XY) => void
|
||||
/** A click with a drawing tool, in drawing units, with its closing reach (R-11). */
|
||||
onPlace: (at: XY, reach: number) => void
|
||||
/** A click on a vertex handle, a « + » or the « − » bubble. */
|
||||
onHandle: (action: HandleAction) => void
|
||||
/** A tap on the empty drawing with the select tool. */
|
||||
onDeselect: () => void
|
||||
}
|
||||
@@ -91,16 +95,23 @@ function doubled(
|
||||
return id === null ? action : { type: 'open', id }
|
||||
}
|
||||
|
||||
/**
|
||||
* What a click does with the select tool. `open` (the properties dialog) and the vertex
|
||||
* handles (`choose_vertex`, `insert`, `remove`) have nothing to act on yet.
|
||||
*/
|
||||
export type HandleAction = Extract<
|
||||
ClickAction,
|
||||
{ type: 'choose_vertex' | 'insert' | 'remove' }
|
||||
>
|
||||
|
||||
const isHandleAction = (action: ClickAction): action is HandleAction =>
|
||||
action.type === 'choose_vertex' ||
|
||||
action.type === 'insert' ||
|
||||
action.type === 'remove'
|
||||
|
||||
/** What a click does with the select tool. `open` (the properties dialog) has nothing yet. */
|
||||
function runClick(action: ClickAction, input: PressInput): void {
|
||||
if (action.type === 'select') input.onSelect(action.id)
|
||||
else if (action.type === 'deselect') input.onDeselect()
|
||||
else if (action.type === 'unchoose') {
|
||||
useAnnotationWork.setState({ chosen: null })
|
||||
}
|
||||
} else if (isHandleAction(action)) input.onHandle(action)
|
||||
}
|
||||
|
||||
interface Machine {
|
||||
@@ -123,8 +134,11 @@ interface Feed {
|
||||
|
||||
/** A click with a drawing tool places at the pointer-up, in drawing units. */
|
||||
function place(input: PressInput, event: PointerEvent): void {
|
||||
const drawn = clientToDrawing(input.overlay.current, clientOf(event))
|
||||
if (drawn) input.onPlace(drawn)
|
||||
const svg = input.overlay.current
|
||||
const drawn = clientToDrawing(svg, clientOf(event))
|
||||
const perPx = drawingPerClientPx(svg)
|
||||
if (!drawn || perPx === null) return
|
||||
input.onPlace(drawn, CLOSE_PX[pointerKindOf(event.pointerType)] * perPx)
|
||||
}
|
||||
|
||||
/** A click with the select tool: its action, or `open` as the second of a double click. */
|
||||
@@ -144,7 +158,12 @@ function onClick(f: Feed, target: PressTarget, event: PointerEvent): void {
|
||||
if (!inMoveMode()) selectClick(f, target, event)
|
||||
}
|
||||
|
||||
const BLOCK_DRAGS = new Set(['drag_shape', 'drag_point', 'drag_mode'])
|
||||
const BLOCK_DRAGS = new Set([
|
||||
'drag_shape',
|
||||
'drag_point',
|
||||
'drag_mode',
|
||||
'drag_vertex',
|
||||
])
|
||||
|
||||
function applyEffect(f: Feed, effect: PressEffect, event: PointerEvent | null) {
|
||||
if (effect?.type === 'drag_cancel') f.drag.cancel()
|
||||
@@ -152,13 +171,22 @@ function applyEffect(f: Feed, effect: PressEffect, event: PointerEvent | null) {
|
||||
if (effect.type === 'click') onClick(f, effect.target, event)
|
||||
if (effect.type === 'drag_end') f.drag.drop(clientOf(event))
|
||||
if (effect.type === 'drag_start' && BLOCK_DRAGS.has(effect.action.type)) {
|
||||
const mode = effect.action.type === 'drag_mode'
|
||||
f.drag.start(f.machine.current.pressedAt, clientOf(event), mode)
|
||||
const { action } = effect
|
||||
const vertex = action.type === 'drag_vertex' ? action.index : null
|
||||
const mode = action.type === 'drag_mode'
|
||||
f.drag.start(f.machine.current.pressedAt, clientOf(event), mode, vertex)
|
||||
}
|
||||
}
|
||||
|
||||
/** R-7: above the handle cap a polygon keeps the move mode and the redraw only. */
|
||||
function movable(input: PressInput): boolean {
|
||||
const geometry = input.target?.geometry
|
||||
if (!geometry) return false
|
||||
return geometry.type !== 'polygon' || geometry.points.length <= HANDLE_CAP
|
||||
}
|
||||
|
||||
const contextOf = (input: PressInput) => ({
|
||||
movable: input.target !== null,
|
||||
movable: movable(input),
|
||||
moveMode: inMoveMode(),
|
||||
})
|
||||
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
import type { KeyboardEvent } from 'react'
|
||||
import type { Annotation, Point } from '@faunosteo/core-wasm/types'
|
||||
import { moveStep, type Frame } from './frame'
|
||||
import { insertMidpoint, removeVertex } from './polygonEdit'
|
||||
import { stepSelection, takePreview } from './previewCommit'
|
||||
import type { Drop } from './usePlacement'
|
||||
import type { GestureMessages } from './useGestureMessage'
|
||||
import type { HandleAction } from './usePressGesture'
|
||||
import { chooseVertex, freshChosen } from './zoneEdit'
|
||||
import { useAnnotationWork } from './workStore'
|
||||
|
||||
export interface ZoneEditInput {
|
||||
/** The selected polygon when its vertices can be edited now (editable, measured). */
|
||||
target: Annotation | null
|
||||
frame: Frame | null
|
||||
onDrop: Drop
|
||||
message: Pick<GestureMessages, 'show'>
|
||||
t: (key: string) => string
|
||||
/** The move mode's own Enter: « Valider la position ». */
|
||||
confirmMove: () => void
|
||||
}
|
||||
|
||||
const STEP: Record<string, [number, number]> = {
|
||||
ArrowLeft: [-1, 0],
|
||||
ArrowUp: [0, -1],
|
||||
ArrowDown: [0, 1],
|
||||
ArrowRight: [1, 0],
|
||||
}
|
||||
|
||||
const pointsOf = (target: Annotation | null): Point[] | null =>
|
||||
target?.geometry?.type === 'polygon' ? target.geometry.points : null
|
||||
|
||||
/** « + »: the midpoint commits once, and the new vertex is chosen on the new text. */
|
||||
function insertAfter(input: ZoneEditInput, after: number): void {
|
||||
const { target, onDrop } = input
|
||||
const next = insertMidpoint(pointsOf(target) ?? [], after)
|
||||
if (!target || !next) return
|
||||
const polygon = { type: 'polygon' as const, points: next }
|
||||
if (onDrop(target.id, polygon, 'insert')) chooseVertex(target.id, after + 1)
|
||||
}
|
||||
|
||||
/**
|
||||
* « − »: only the vertex chosen on the current text (Review Focus 2: an undo drops it, so a
|
||||
* stale bubble or key removes nothing). At three vertices the reason is said instead.
|
||||
*/
|
||||
function removeChosen(input: ZoneEditInput, index: number): boolean {
|
||||
const { target, onDrop } = input
|
||||
const points = pointsOf(target)
|
||||
if (!target || !points || freshChosen(target.id) !== index) return false
|
||||
const next = removeVertex(points, index)
|
||||
if (!next) {
|
||||
const text = input.t('editor.annotations.message.vertex_min')
|
||||
input.message.show(text, { undo: false, tone: 'error' })
|
||||
return false
|
||||
}
|
||||
return onDrop(target.id, { type: 'polygon', points: next }, 'remove')
|
||||
}
|
||||
|
||||
/** The keyboard series of a vertex commits once, the vertex still chosen after. */
|
||||
function commitKeys(input: ZoneEditInput): void {
|
||||
const { target, onDrop } = input
|
||||
const preview = takePreview('keys')
|
||||
if (!target || !preview || preview.annotationId !== target.id) return
|
||||
const same =
|
||||
JSON.stringify(preview.geometry) === JSON.stringify(target.geometry)
|
||||
if (same) return
|
||||
const vertex = freshChosen(target.id)
|
||||
const kept = onDrop(target.id, preview.geometry, 'vertex')
|
||||
if (kept && vertex !== null) chooseVertex(target.id, vertex)
|
||||
}
|
||||
|
||||
function stepVertex(input: ZoneEditInput, index: number, event: KeyboardEvent) {
|
||||
const { target, frame } = input
|
||||
const [x, y] = STEP[event.key]
|
||||
if (!target?.geometry || !frame) return
|
||||
const size = moveStep(frame, event.shiftKey)
|
||||
stepSelection(target.id, target.geometry, { x: x * size, y: y * size }, index)
|
||||
}
|
||||
|
||||
const inMoveMode = () => useAnnotationWork.getState().mode?.kind === 'move'
|
||||
|
||||
/** Delete removes the chosen vertex; the next one is chosen so the focus stays on a vertex. */
|
||||
function deleteChosen(input: ZoneEditInput): void {
|
||||
const id = input.target?.id
|
||||
const chosen = id ? freshChosen(id) : null
|
||||
if (id === undefined || chosen === null || !removeChosen(input, chosen))
|
||||
return
|
||||
const left = pointsOf(input.target)!.length - 2
|
||||
chooseVertex(id, Math.min(chosen, left))
|
||||
}
|
||||
|
||||
/** A vertex handle's keys (board 32): arrows step it, Enter commits, Insert adds, Delete. */
|
||||
function vertexKey(input: ZoneEditInput, index: number, event: KeyboardEvent) {
|
||||
const id = input.target?.id
|
||||
if (STEP[event.key]) {
|
||||
event.preventDefault()
|
||||
return stepVertex(input, index, event)
|
||||
}
|
||||
if (event.key === 'Enter') {
|
||||
event.preventDefault()
|
||||
return inMoveMode() ? input.confirmMove() : commitKeys(input)
|
||||
}
|
||||
const chosen = id ? freshChosen(id) : null
|
||||
if (event.key === 'Insert' && chosen !== null) insertAfter(input, chosen)
|
||||
if (event.key === 'Delete') deleteChosen(input)
|
||||
}
|
||||
|
||||
/** Enter or Space on a « + » or on the « − » bubble is its click. */
|
||||
const isPress = (event: KeyboardEvent) =>
|
||||
event.key === 'Enter' || event.key === ' '
|
||||
|
||||
/**
|
||||
* Zone editing (R-7 to R-13): a click on a vertex chooses it, « + » inserts at a segment's
|
||||
* midpoint, « − » removes the chosen vertex, and the vertex handles' keys. Every change
|
||||
* commits once through `onDrop`, the core deciding.
|
||||
*/
|
||||
export function useZoneEdit(input: ZoneEditInput) {
|
||||
const onHandle = (action: HandleAction) => {
|
||||
const id = input.target?.id
|
||||
if (!id) return
|
||||
if (action.type === 'choose_vertex') chooseVertex(id, action.index)
|
||||
if (action.type === 'insert') insertAfter(input, action.after)
|
||||
if (action.type === 'remove') removeChosen(input, action.index)
|
||||
}
|
||||
return {
|
||||
onHandle,
|
||||
onVertexKey: (index: number, event: KeyboardEvent) =>
|
||||
vertexKey(input, index, event),
|
||||
// Focus chooses (board 32: Insert and Delete act on the chosen vertex).
|
||||
onVertexFocus: (index: number) => {
|
||||
const id = input.target?.id
|
||||
if (id && freshChosen(id) !== index) chooseVertex(id, index)
|
||||
},
|
||||
// BDR-050: leaving the vertex commits its series; a move mode waits for « Valider ».
|
||||
onVertexBlur: () => commitKeys(input),
|
||||
onInsertKey: (after: number, event: KeyboardEvent) => {
|
||||
if (!isPress(event)) return
|
||||
event.preventDefault()
|
||||
insertAfter(input, after)
|
||||
},
|
||||
onRemoveKey: (index: number, event: KeyboardEvent) => {
|
||||
if (!isPress(event)) return
|
||||
event.preventDefault()
|
||||
removeChosen(input, index)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export type ZoneEdit = ReturnType<typeof useZoneEdit>
|
||||
@@ -31,6 +31,8 @@ export interface ModeWork {
|
||||
annotationId: string
|
||||
started: boolean
|
||||
base: string
|
||||
/** The chosen vertex a move mode moves alone (« Déplacer le sommet »); null: the shape. */
|
||||
vertex: number | null
|
||||
}
|
||||
export type GuardWork = 'creation' | 'move' | 'redraw' | null
|
||||
/** A navigation held by the drawing guard (R-12): what it asks about, and the move itself. */
|
||||
@@ -68,15 +70,34 @@ export function endMode(): void {
|
||||
useAnnotationWork.setState({ mode: null, preview: null })
|
||||
}
|
||||
|
||||
/** « Déplacer … »: the mode on `annotationId`, nothing moved yet. */
|
||||
export function startMove(annotationId: string, base: string): void {
|
||||
/** « Déplacer … »: the mode on `annotationId` (or its vertex `vertex`), nothing moved yet. */
|
||||
export function startMove(
|
||||
annotationId: string,
|
||||
base: string,
|
||||
vertex: number | null = null,
|
||||
): void {
|
||||
useAnnotationWork.setState({
|
||||
mode: { kind: 'move', annotationId, started: false, base },
|
||||
mode: { kind: 'move', annotationId, started: false, base, vertex },
|
||||
preview: null,
|
||||
})
|
||||
}
|
||||
|
||||
/** « Redessiner »: the redraw of `annotationId`, no vertex placed yet (R-12). */
|
||||
export function startRedraw(annotationId: string, base: string): void {
|
||||
useAnnotationWork.setState({
|
||||
mode: { kind: 'redraw', annotationId, started: false, base, vertex: null },
|
||||
preview: null,
|
||||
chosen: null,
|
||||
})
|
||||
}
|
||||
|
||||
/** The redraw is started once its tracing holds a vertex, and no longer at none. */
|
||||
export function markRedraw(started: boolean): void {
|
||||
const { mode } = useAnnotationWork.getState()
|
||||
if (mode?.kind !== 'redraw' || mode.started === started) return
|
||||
useAnnotationWork.setState({ mode: { ...mode, started } })
|
||||
}
|
||||
|
||||
const sameGeometry = (a: Geometry, b: Geometry) =>
|
||||
JSON.stringify(a) === JSON.stringify(b)
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import type { Geometry, Point } from '@faunosteo/core-wasm/types'
|
||||
import { useEditorStore } from '../state/editorStore'
|
||||
import { HANDLE_CAP } from './polygonEdit'
|
||||
import { useAnnotationWork } from './workStore'
|
||||
|
||||
/** A polygon's vertices when it gets handles (R-7: at most HANDLE_CAP), else null. */
|
||||
export function handledPoints(geometry: Geometry | null): Point[] | null {
|
||||
if (geometry?.type !== 'polygon') return null
|
||||
return geometry.points.length > HANDLE_CAP ? null : geometry.points
|
||||
}
|
||||
|
||||
/** The vertex `vertex` of `annotationId` is chosen, stamped with the current text (R-13). */
|
||||
export function chooseVertex(annotationId: string, vertex: number): void {
|
||||
const base = useEditorStore.getState().documentText
|
||||
useAnnotationWork.setState({ chosen: { annotationId, vertex, base } })
|
||||
}
|
||||
|
||||
/** The chosen vertex of `annotationId`, only when it was chosen on the current text. */
|
||||
export function freshChosen(annotationId: string): number | null {
|
||||
const { chosen } = useAnnotationWork.getState()
|
||||
if (chosen?.annotationId !== annotationId) return null
|
||||
const current = useEditorStore.getState().documentText
|
||||
return chosen.base === current ? chosen.vertex : null
|
||||
}
|
||||
+480
-264
File diff suppressed because it is too large.
Load diff
+550
-288
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user