fix(editor): guard keeps pending series, mode end said, R-9 hook

- guardDrawing commits a keyboard or size series the focus still holds
  (its blur) and ends only the mode: a pending series is never dropped.
- A started move ended by a selection change is said (R-13).
- handleSize: no module-level state; useHandleSizes reads the device
  once per handle layer. The layer reads ResizeObserver contentRect.
- Dead kind check removed in SelectionPointCommands; long lines wrapped
  (code, doc).
- e2e: the zoom check waits for the transform to change; the move-mode
  arrow clicks are under expectNoShift.
This commit is contained in:
bchanot committed 2026-11-17 19:25:04 +01:00
1 parent e01d8519ea
commit 463e0a2621
11 files changed
+157 -40

No files matched your search

@@ -173,7 +173,11 @@ type Work = ReturnType<typeof useWork>
function useModeFollowsSelection(selectedId: string | null) {
const modeId = useAnnotationWork((s) => s.mode?.annotationId ?? null)
useEffect(() => {
if (modeId !== null && modeId !== selectedId) endMode()
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])
}
@@ -8,7 +8,10 @@ import {
} from 'react'
import type { Geometry } from '@faunosteo/core-wasm/types'
import { parseFrame } from '@/editor/annotations/frame'
import { handleSizes } from '@/editor/annotations/handleSize'
import {
useHandleSizes,
type HandleSizes,
} from '@/editor/annotations/handleSize'
import { useAnnotationWork } from '@/editor/annotations/workStore'
/** The selected point (or link circle) whose handle the layer draws. */
@@ -40,7 +43,10 @@ function useBoxSize(svg: RefObject<SVGSVGElement | null>) {
setSize({ w: element.clientWidth || 0, h: element.clientHeight || 0 })
read()
if (typeof ResizeObserver === 'undefined') return
const observer = new ResizeObserver(read)
const observer = new ResizeObserver(([entry]) => {
const box = entry?.contentRect
if (box) setSize({ w: box.width, h: box.height })
})
observer.observe(element)
return () => observer.disconnect()
}, [svg])
@@ -72,6 +78,7 @@ function unitsPerPx(
export default function HandleLayer(props: HandleLayerProps) {
const svg = useRef<SVGSVGElement | null>(null)
const unit = unitsPerPx(props.viewBox, useBoxSize(svg), props.scale)
const sizes = useHandleSizes()
return (
<svg
ref={svg}
@@ -81,7 +88,12 @@ export default function HandleLayer(props: HandleLayerProps) {
className="pointer-events-none absolute inset-0 h-full w-full overflow-visible"
>
{props.handle && (
<PointHandleShape {...props} handle={props.handle} unit={unit} />
<PointHandleShape
{...props}
handle={props.handle}
sizes={sizes}
unit={unit}
/>
)}
</svg>
)
@@ -92,19 +104,26 @@ function PointHandleShape({
unit,
onKeyDown,
onBlur,
}: HandleLayerProps & { handle: PointHandle; unit: number }) {
sizes,
}: HandleLayerProps & {
handle: PointHandle
unit: number
sizes: HandleSizes
}) {
const preview = useAnnotationWork((s) =>
s.preview?.annotationId === handle.annotationId ? s.preview.geometry : null,
)
const shown = preview ?? handle.stored
if (shown.type !== 'circle') return null
const r = Math.max(shown.r, handleSizes().hit * unit)
const r = Math.max(shown.r, sizes.hit * unit)
return (
<g
role="button"
tabIndex={0}
aria-label={handle.label}
data-testid="point-handle"
// Its arrow series commits on blur; the drawing guard blurs it before leaving.
data-series=""
className="group outline-none"
onKeyDown={onKeyDown}
onBlur={onBlur}
@@ -110,7 +110,7 @@ export function SelectionPointCommands(props: SelectionPointProps) {
}
return (
<>
{annotation.kind === 'point' && <SelectionSize {...props} />}
<SelectionSize {...props} />
<Button
// R-12: the focus comes back here when the mode ends (useMoveFocusReturn).
data-move-entry=""
@@ -82,6 +82,7 @@ export default function SizeSlider(props: SizeSliderProps) {
ref={input}
id={id}
type="range"
data-series=""
min={range.low}
max={range.high}
step={0.01}
+2 -1
View File
@@ -39,7 +39,8 @@ function drawingFocus(): void {
* Deselecting: `id` is the RESOLVED selection (never the raw id, which can name a removed
* annotation). The button always moves the focus to the row; Escape does when it was
* pressed inside the properties panel, or when the focused element left with the selection
* (the handle, the slider, « Déplacer le point »). Anywhere else the focus stays where it is. Both answer whether something was deselected.
* (the handle, the slider, « Déplacer le point »). Anywhere else the focus stays where it
* is. Both answer whether something was deselected.
*/
export function useDeselect(id: string | null, clear: () => void) {
const t = useT()
@@ -1,5 +1,25 @@
import { endMode, guardWork, useAnnotationWork } from './workStore'
/**
* A keyboard series or a size change still pending (no blur yet: a mouse Back button)
* commits now, through its own blur (BDR-050): leaving never drops it.
*/
function commitFocusedSeries(): void {
const { preview } = useAnnotationWork.getState()
if (!preview || preview.source === 'move') return
const active = document.activeElement
const series = active instanceof HTMLElement || active instanceof SVGElement
if (series && active.closest('[data-series]')) active.blur()
}
/** An unstarted mode ends; a pending series that is not the mode's stays. */
function endModeOnly(): void {
useAnnotationWork.setState((work) => ({
mode: null,
preview: work.preview?.source === 'move' ? null : work.preview,
}))
}
/**
* The drawing guard (R-12), asked by every navigation that would drop the drawing's work:
* a tab, a section, « Aller à », a view chip, a tool, Back, leaving the editor. Nothing
@@ -10,7 +30,8 @@ import { endMode, guardWork, useAnnotationWork } from './workStore'
export function guardDrawing(proceed: () => void): boolean {
const work = guardWork(useAnnotationWork.getState())
if (work === null) {
endMode()
commitFocusedSeries()
endModeOnly()
return true
}
useAnnotationWork.setState({ question: { work, proceed } })
+3 -3
View File
@@ -87,9 +87,9 @@ function clampAxis(value: number, low: number, high: number): number {
/**
* Candidate C: radius r0 (the wanted `radius`, fitted to F), the centre clamped per axis
* into [x + r0, x + w − r0] and [y + r0, y + h − r0], rounded inward to 0.01. When that rounding leaves an empty
* interval (r0 = min(w, h) / 2 with an origin off the grid) the exact clamped value is
* used: the core accepts any decimals.
* into [x + r0, x + w − r0] and [y + r0, y + h − r0], rounded inward to 0.01. When that
* rounding leaves an empty interval (r0 = min(w, h) / 2 with an origin off the grid) the
* exact clamped value is used: the core accepts any decimals.
*/
export function correctedCircle(
frame: Frame,
+14 -11
View File
@@ -1,3 +1,5 @@
import { useState } from 'react'
/** R-9: handle sizes in CSS px, constant under zoom. */
export interface HandleSizes {
vertex: number
@@ -9,18 +11,19 @@ export interface HandleSizes {
const FINE: HandleSizes = { vertex: 12, insert: 16, hit: 10 }
const COARSE: HandleSizes = { vertex: 18, insert: 24, hit: 22 }
let device: HandleSizes | null = null
function deviceSizes(): HandleSizes {
const coarse =
typeof window.matchMedia === 'function' &&
window.matchMedia('(any-pointer: coarse)').matches
return coarse ? COARSE : FINE
}
/**
* 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.
* The device's sizes, read ONCE per handle layer from `(any-pointer: coarse)` (R-9): a
* hybrid device keeps the size the layer 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
export function useHandleSizes(): HandleSizes {
const [sizes] = useState(deviceSizes)
return sizes
}