feat(editor): point handle, size slider with core minimum, move mode

HandleLayer: its own svg over the overlay, not aria-hidden; the selected
point (or link circle) gets a focusable « Point {nom} » handle whose hit
circle (data-handle) the press machine reads, sized in CSS px under zoom
(R-9, read once from any-pointer: coarse). The Task 2 stand-in (the
selected marker as its own handle) is removed.

Keys on the handle: arrows step 0.5 % of the frame (Shift x10) as a
stamped series, Enter or blur commit once, Escape drops it (R-11, R-13).
SizeSlider in the strip for a selected point and for the Point tool (A5):
the view's sizes as ticks, the core minimum as a red mark, snap 8 px,
arrows 1 %, Page Up/Down next size, one commit on release, Enter or blur;
a refusal restores the size and draws the refused place in red dashes.
« Déplacer le point » opens the move mode (board 06 B): drag anywhere or
four arrows move a preview only, clicks select nothing, « Valider la
position » commits once, « Annuler » and Escape restore; the focus comes
back to the button. Escape follows board 32 (escapeAction). The overlay
reads the preview from the work store and draws the old place in grey
dashes. Ctrl+Z now also works from a slider (not a text field).

Rewritten assertions:
- editor-annotations-tab « a finger drags the selected point »:
  drag(marker('anno-a')) -> drag(pointHandle()), same expected geometry.
- editor-geometry.spec « a point inherited out of its frame is repaired
  through the geometry group » (cx/cy typed 200) -> « ... repaired with
  the move mode »: a partial move refused and restored (cx 110, issue
  kept), then a move inside kept (issue gone, fewer errors).
This commit is contained in:
bchanot committed 2026-11-17 18:50:29 +01:00
1 parent 2882821661
commit 91e94952f0
23 files changed
+1679 -149

No files matched your search

+42 -6
View File
@@ -9,6 +9,8 @@ import {
} from '@/editor/focusRequest'
import { CANVAS_BOX } from '@/editor/useEditorNavigation'
import { useT } from '@/i18n/useT'
import { escapeAction, type EscapeAction } from './escape'
import { endMode, useAnnotationWork } from './workStore'
/** The drawing's focusable box of the shown body, when there is one. */
export function focusDrawing(): boolean {
@@ -89,20 +91,54 @@ export function useEscapeDeselect(
}, [active])
}
/** The editable body's pending work, as board 32 orders Escape (rows 3 to 6). */
function escapeOf(id: string | null, creating: boolean): EscapeAction {
const work = useAnnotationWork.getState()
return escapeAction({
previewPending: work.preview !== null && work.preview.source !== 'move',
creating,
inMode: work.mode !== null,
chosenVertex: work.chosen?.annotationId === id ? work.chosen.vertex : null,
selectedId: id,
})
}
/** What one Escape does; `deselect` when nothing pending is left. */
function runEscape(action: EscapeAction, deselect: () => boolean): boolean {
if (action === 'drop_preview') useAnnotationWork.setState({ preview: null })
else if (action === 'drop_mode') endMode()
else if (action === 'unchoose') useAnnotationWork.setState({ chosen: null })
else if (action === 'deselect') return deselect()
// `ask_creation`: the creation dialog asks (U2 Task 7); nothing here yet.
return action !== null && action !== 'ask_creation'
}
/** Where Escape applies: the editable body runs the work order; the read-only twin deselects. */
export interface EscapeScope {
editable: boolean
/** A creation in progress (the editable body only). */
creating?: boolean
}
/**
* A body's Escape: deselects `id` while `active`. Returns the ref of the panel column, which
* tells a key pressed inside the panel (focus follows the row) from one pressed elsewhere,
* and the tap-on-the-drawing deselect.
* A body's Escape while `active`: one situation per press (board 32): a pending preview,
* then a mode, a chosen vertex, then the selection `id`. Returns the ref of the panel
* column, which tells a key pressed inside the panel (focus follows the row) from one
* pressed elsewhere, and the tap-on-the-drawing deselect.
*/
export function useSelectionEscape(
id: string | null,
clear: () => void,
active: boolean,
scope: EscapeScope = { editable: false },
) {
const asideRef = useRef<HTMLElement>(null)
const deselect = useDeselect(id, clear)
useEscapeDeselect(active, (event) =>
deselect.byKey(asideRef.current?.contains(event.target as Node) ?? false),
)
useEscapeDeselect(active, (event) => {
const inPanel = asideRef.current?.contains(event.target as Node) ?? false
const byKey = () => deselect.byKey(inPanel)
if (!scope.editable) return byKey()
return runEscape(escapeOf(id, scope.creating ?? false), byKey)
})
return { asideRef, byDrawing: deselect.byDrawing }
}
@@ -0,0 +1,26 @@
/** R-9: handle sizes in CSS px, constant under zoom. */
export interface HandleSizes {
vertex: number
insert: number
/** The radius around a handle's centre that a press reaches. */
hit: number
}
const FINE: HandleSizes = { vertex: 12, insert: 16, hit: 10 }
const COARSE: HandleSizes = { vertex: 18, insert: 24, hit: 22 }
let device: HandleSizes | null = null
/**
* The device's sizes, read ONCE from `(any-pointer: coarse)` (R-9): a hybrid device keeps
* the size it started with, so no handle is re-created under the finger.
*/
export function handleSizes(): HandleSizes {
if (device === null) {
const coarse =
typeof window.matchMedia === 'function' &&
window.matchMedia('(any-pointer: coarse)').matches
device = coarse ? COARSE : FINE
}
return device
}
@@ -0,0 +1,66 @@
import type { Geometry } from '@faunosteo/core-wasm/types'
import { useEditorStore } from '../state/editorStore'
import { translatedGeometry } from './gesture'
import {
endMode,
setModePreview,
useAnnotationWork,
type Preview,
} from './workStore'
const fresh = (preview: Preview | null): Preview | null =>
preview && preview.base === useEditorStore.getState().documentText
? preview
: null
/**
* A keyboard series or a size change ends (Enter, blur, release): the preview of `source`
* leaves the store and is returned when it was computed on the current text (R-13); a
* stale one is dropped, never committed.
*/
export function takePreview(source: Preview['source']): Preview | null {
const { preview } = useAnnotationWork.getState()
if (preview?.source !== source) return null
useAnnotationWork.setState({ preview: null })
return fresh(preview)
}
/** « Valider la position »: the mode ends first; a started, fresh move is returned. */
export function takeMove(): Preview | null {
const { mode, preview } = useAnnotationWork.getState()
endMode()
return mode?.started ? fresh(preview) : null
}
/** What `annotationId` shows now: its pending preview, else the stored shape. */
export function shownGeometry(
annotationId: string,
stored: Geometry,
): Geometry {
const { preview } = useAnnotationWork.getState()
return preview?.annotationId === annotationId ? preview.geometry : stored
}
/**
* 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.
*/
export function stepSelection(
annotationId: string,
stored: Geometry,
delta: { x: number; y: number },
): void {
const moved = translatedGeometry(shownGeometry(annotationId, stored), delta)
if (!moved) return
if (useAnnotationWork.getState().mode?.kind === 'move') {
return setModePreview(moved, stored)
}
const base = useEditorStore.getState().documentText
const preview: Preview = {
annotationId,
geometry: moved,
base,
source: 'keys',
}
useAnnotationWork.setState({ preview })
}
+35 -10
View File
@@ -3,6 +3,8 @@ 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 { shownGeometry } from './previewCommit'
import { setModePreview } from './workStore'
import type { EditableGeometry } from './geometryEdit'
export interface MoveTarget {
@@ -23,6 +25,10 @@ interface Drag {
target: MoveTarget
/** The press point in drawing units: the block moves by the pointer's delta from it. */
origin: XY
/** The shape the drag starts from: the stored one, or the move mode's last place. */
base: Geometry
/** A drag of the move mode (board 06 B): it moves the mode's preview, never commits. */
mode: boolean
}
function translatedAt(
@@ -32,7 +38,7 @@ function translatedAt(
): Geometry | null {
const now = clientToDrawing(overlay.current, client)
if (!now) return null
return translatedGeometry(drag.target.geometry, {
return translatedGeometry(drag.base, {
x: now.x - drag.origin.x,
y: now.y - drag.origin.y,
})
@@ -49,40 +55,59 @@ function useDropOnChange(target: MoveTarget | null, drop: () => void) {
type SetPreview = (geometry: Geometry | null) => void
/** The drag's shape at `geometry`: local to the canvas, or the move mode's preview. */
function shower(drag: RefObject<Drag | null>, setPreview: SetPreview) {
return (geometry: Geometry | null) => {
const gesture = drag.current
if (!gesture?.mode) return setPreview(geometry)
if (geometry) setModePreview(geometry, gesture.target.geometry)
}
}
/** Start, follow, drop and cancel of the block drag held in `drag`. */
function dragActions(
input: BlockDragInput,
drag: RefObject<Drag | null>,
setPreview: SetPreview,
) {
const show = shower(drag, setPreview)
const cancel = () => {
// A cancelled drag of the mode puts the shape back where that drag found it.
show(drag.current?.mode ? drag.current.base : null)
drag.current = null
setPreview(null)
}
const start = (pressedAt: XY, client: XY) => {
const start = (pressedAt: XY, client: XY, mode = false) => {
const origin = clientToDrawing(input.overlay.current, pressedAt)
if (!input.target || !origin) return
drag.current = { target: input.target, origin }
setPreview(translatedAt(input.overlay, drag.current, client))
const { target } = input
if (!target || !origin) return
const base = mode
? shownGeometry(target.id, target.geometry)
: target.geometry
drag.current = { target, origin, base, mode }
show(translatedAt(input.overlay, drag.current, client))
}
const follow = (client: XY) => {
if (drag.current)
setPreview(translatedAt(input.overlay, drag.current, client))
if (drag.current) show(translatedAt(input.overlay, drag.current, client))
}
const drop = (client: XY) => {
const gesture = drag.current
const dropped = gesture
? translatedAt(input.overlay, gesture, client)
: null
cancel()
if (gesture && dropped) input.onDrop(gesture.target.id, dropped)
if (gesture?.mode) show(dropped)
drag.current = null
setPreview(null)
if (gesture && !gesture.mode && dropped)
input.onDrop(gesture.target.id, 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.
* translated 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) {
const drag = useRef<Drag | null>(null)
@@ -5,6 +5,7 @@ import {
type FocusEvent,
type PointerEvent,
} from 'react'
import type { Geometry } from '@faunosteo/core-wasm/types'
import { useEditorStore } from '../state/editorStore'
/** A6/A14: how long a message stays (R-2) and how long a hint does (R-1). */
@@ -25,6 +26,14 @@ export interface GestureMessage {
undo: boolean
hint: boolean
stamp: Stamp
/** The place the core refused, drawn in red dashes while the refusal shows. */
place: Geometry | null
}
export interface ShowOptions {
undo: boolean
tone: GestureMessage['tone']
place?: Geometry
}
function currentStamp(): Stamp {
@@ -102,10 +111,15 @@ export function useGestureMessage() {
)
return () => clearTimeout(timer)
}, [message, held])
const show = (
text: string,
{ undo, tone }: { undo: boolean; tone: GestureMessage['tone'] },
) => setShown({ text, tone, undo, hint: false, stamp: currentStamp() })
const show = (text: string, { undo, tone, place }: ShowOptions) =>
setShown({
text,
tone,
undo,
hint: false,
stamp: currentStamp(),
place: place ?? null,
})
const hint = (text: string) => {
if (message) return
setShown({
@@ -114,6 +128,7 @@ export function useGestureMessage() {
undo: false,
hint: true,
stamp: currentStamp(),
place: null,
})
}
return { message, show, hint, hold: handlers }
@@ -1,5 +1,6 @@
import type { Dispatch, KeyboardEvent } from 'react'
import type {
Annotation,
AnatomicalSheetDocument,
Geometry,
Point,
@@ -96,24 +97,36 @@ function tracingActions(input: PlacementInput) {
return { closeTracing, trace, onTraceKey }
}
/** A drop commits one update; the line under the drawing says it, or the core's refusal. */
/** A move (drag, move mode, arrow keys) or a size change of the selection. */
export type GeometryChange = 'move' | 'size'
/** 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' }
}
/**
* A drop commits one update; the line under the drawing says it, or the core's refusal
* with the refused place drawn (the shape stays where it was).
*/
function dropAction({ doc, context, message, dispatch }: PlacementInput) {
return (id: string, geometry: Geometry) => {
return (id: string, geometry: Geometry, change: GeometryChange = 'move') => {
const annotation = doc.annotations.find((a) => a.id === id)
if (!annotation) return
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.moved.${annotation.kind}`,
)
const text = context.t(`editor.annotations.message.${keys.kept}`)
return message.show(text, { undo: true, tone: 'info' })
}
const text = context
.t('editor.annotations.message.move_refused')
.t(`editor.annotations.message.${keys.refused}`)
.replace('{text}', () => result.messages.join(' '))
message.show(text, { undo: false, tone: 'error' })
message.show(text, { undo: false, tone: 'error', place: geometry })
}
}
@@ -0,0 +1,99 @@
import type { KeyboardEvent } from 'react'
import type { Annotation, Geometry } from '@faunosteo/core-wasm/types'
import { useEditorStore } from '../state/editorStore'
import { moveStep, type Frame } from './frame'
import type { GeometryChange } from './usePlacement'
import { takeMove, takePreview, stepSelection } from './previewCommit'
import { endMode, startMove, useAnnotationWork } 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). */
target: Annotation | null
frame: Frame | null
onDrop: Drop
}
export type Direction = 'left' | 'up' | 'down' | 'right'
const DELTA: Record<Direction, [number, number]> = {
left: [-1, 0],
up: [0, -1],
down: [0, 1],
right: [1, 0],
}
const KEY_DIRECTION: Record<string, Direction> = {
ArrowLeft: 'left',
ArrowUp: 'up',
ArrowDown: 'down',
ArrowRight: 'right',
}
/** One step (R-11) of the target, into the move mode or a keyboard series. */
function step(input: PointEditInput, direction: Direction, big: boolean) {
const { target, frame } = input
if (!target?.geometry || !frame) return
const size = moveStep(frame, big)
const [x, y] = DELTA[direction]
stepSelection(target.id, target.geometry, { x: x * size, y: y * size })
}
/** The commits of the pending work: each takes its preview, fresh only (R-13). */
function commits(onDrop: Drop) {
const keys = () => {
const preview = takePreview('keys')
if (preview) onDrop(preview.annotationId, preview.geometry)
}
const size = () => {
const preview = takePreview('size')
if (preview) onDrop(preview.annotationId, preview.geometry, 'size')
}
const move = () => {
const preview = takeMove()
if (preview) onDrop(preview.annotationId, preview.geometry)
}
return { keys, size, move }
}
const inMoveMode = () => useAnnotationWork.getState().mode?.kind === 'move'
/** The point handle's keys: arrows step (Shift ×10), Enter commits the series or the mode. */
function handleKeys(input: PointEditInput, commit: ReturnType<typeof commits>) {
return (event: KeyboardEvent) => {
const direction = KEY_DIRECTION[event.key]
if (direction) {
event.preventDefault()
return step(input, direction, event.shiftKey)
}
if (event.key !== 'Enter') return
event.preventDefault()
if (inMoveMode()) commit.move()
else commit.keys()
}
}
/**
* Point editing (R-10 to R-13): the handle's keyboard series (one commit on Enter or
* blur), the size slider's commit, and the move mode (« Déplacer le point », its arrows,
* « Valider la position » committing once, « Annuler »).
*/
export function usePointEdit(input: PointEditInput) {
const commit = commits(input.onDrop)
return {
onHandleKey: handleKeys(input, commit),
// BDR-050: leaving the handle commits its series; a move mode waits for « Valider ».
onHandleBlur: () => commit.keys(),
commitSize: commit.size,
startMove: () => {
if (!input.target) return
startMove(input.target.id, useEditorStore.getState().documentText)
},
stepMove: (direction: Direction) => step(input, direction, false),
confirmMove: commit.move,
cancelMove: endMode,
}
}
export type PointEdit = ReturnType<typeof usePointEdit>
@@ -20,11 +20,7 @@ import {
} from './press'
import type { ToolId } from './uiState'
import { annotationIdOf } from './useCanvasBuild'
import {
useBlockDrag,
type BlockDragInput,
type MoveTarget,
} from './useBlockDrag'
import { useBlockDrag, type BlockDragInput } from './useBlockDrag'
import { useAnnotationWork } from './workStore'
export interface PressInput extends BlockDragInput {
@@ -48,46 +44,35 @@ function centreOf(element: Element): XY {
return { x: box.left + box.width / 2, y: box.top + box.height / 2 }
}
/**
* One element under the press as a hit: a handle by its `data-handle`, else an annotation.
* Until the handle layer exists, the selected point's own marker stands for its handle.
*/
function hitOf(element: Element, pointId: string | null): Hit | null {
/** One element under the press as a hit: a handle of the handle layer, else an annotation. */
function hitOf(element: Element): Hit | null {
const handle = element.getAttribute('data-handle')
if (handle !== null) {
const ref = JSON.parse(handle) as HandleRef
return { kind: 'handle', handle: ref, centre: centreOf(element) }
}
const id = annotationIdOf(element)
if (id === null) return null
if (id !== pointId) return { kind: 'annotation', id }
const marker = element.closest('[data-anno-id]') ?? element
const centre = centreOf(marker)
return { kind: 'handle', handle: { kind: 'point' }, centre }
return id === null ? null : { kind: 'annotation', id }
}
/** Everything under the client point inside the canvas box, top first. */
function hitsAt(
viewport: Element | null,
at: XY,
pointId: string | null,
): Hit[] {
function hitsAt(viewport: Element | null, at: XY): Hit[] {
if (!viewport) return []
return document
.elementsFromPoint(at.x, at.y)
.filter((element) => viewport.contains(element))
.flatMap((element) => hitOf(element, pointId) ?? [])
.flatMap((element) => hitOf(element) ?? [])
}
/** « Déplacer … » is on: a drag anywhere moves its target and a click selects nothing. */
const inMoveMode = () => useAnnotationWork.getState().mode?.kind === 'move'
function selectionOf(selectedId: string | null): Selection {
const chosen = useAnnotationWork.getState().chosen
const own = selectedId !== null && chosen?.annotationId === selectedId
return { selectedId, chosenVertex: own ? chosen.vertex : null }
}
const pointIdOf = (target: MoveTarget | null) =>
target?.kind === 'point' ? target.id : null
const HANDLE_ACTIONS = new Set<ClickAction['type']>([
'choose_vertex',
'insert',
@@ -153,12 +138,13 @@ function selectClick(f: Feed, target: PressTarget, event: PointerEvent): void {
runClick(double ? doubled(action, target, sel) : action, f.input)
}
/** A click with the select tool (none during the move mode, R-12), or with a drawing tool. */
function onClick(f: Feed, target: PressTarget, event: PointerEvent): void {
if (f.input.tool === 'select') selectClick(f, target, event)
else place(f.input, event)
if (f.input.tool !== 'select') return place(f.input, event)
if (!inMoveMode()) selectClick(f, target, event)
}
const BLOCK_DRAGS = new Set(['drag_shape', 'drag_point'])
const BLOCK_DRAGS = new Set(['drag_shape', 'drag_point', 'drag_mode'])
function applyEffect(f: Feed, effect: PressEffect, event: PointerEvent | null) {
if (effect?.type === 'drag_cancel') f.drag.cancel()
@@ -166,14 +152,20 @@ 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)) {
f.drag.start(f.machine.current.pressedAt, clientOf(event))
const mode = effect.action.type === 'drag_mode'
f.drag.start(f.machine.current.pressedAt, clientOf(event), mode)
}
}
const contextOf = (input: PressInput) => ({
movable: input.target !== null,
moveMode: inMoveMode(),
})
/** One event through the machine and its effect; at idle the press is over. */
function feed(f: Feed, event: PressEvent, pointer: PointerEvent | null) {
const m = f.machine.current
const context = { movable: f.input.target !== null, moveMode: false }
const context = contextOf(f.input)
const step = stepPress(m.press, event, context)
m.press = step.press
applyEffect(f, step.effect, pointer)
@@ -231,7 +223,7 @@ function startPress(latest: RefObject<Feed>, event: PointerEvent): void {
if (m.press.phase !== 'idle') feed(f, { type: 'cancel' }, null)
m.pressedAt = clientOf(event)
const kind = pointerKindOf(event.pointerType)
const context = { kind, movable: f.input.target !== null, moveMode: false }
const context = { kind, ...contextOf(f.input) }
// The drag of the selection keeps its pointer even past the box's edge.
if (BLOCK_DRAGS.has(dragAction(m.target, context).type)) {
event.currentTarget.setPointerCapture?.(event.pointerId)
@@ -241,7 +233,7 @@ function startPress(latest: RefObject<Feed>, event: PointerEvent): void {
/** The press's target: the selection's handles, then the topmost annotation. */
function targetAt(input: PressInput, at: XY): PressTarget {
const hits = hitsAt(input.viewport.current, at, pointIdOf(input.target))
const hits = hitsAt(input.viewport.current, at)
return pressTarget(hits, at, selectionOf(input.selectedId))
}