Final review M-3, M-4, M-5, M-8. - M-3: the line and the strip render a refusal's role="alert" beside their always-mounted role="status" region, never inside it; every shown message carries a serial that keys its pill, so a repeated identical refusal is a new pill and is announced again. - M-4: after the line's « Défaire » the focus goes to the canvas box. - M-5: « Défaire » gets a transparent ::before, 44 px tall below xl (36 px from xl), within the row's reserved 44 px: the pill keeps its 36 px. The A14 e2e checks the hit area at 1024, 1180, 1280 px. - M-8: the « swallows the click on a marker » test fired a click the canvas ignores since T2; the inert click is dropped, the title says what the taps prove. Tests rewritten: « the message sits on its own row »: role="status" on the row → a role="status" region inside the row, empty at rest. « a mouse tap deselects … swallows the click »: the final fireEvent.click on the marker removed (inert), its isDeselected assertion kept after the second tap.
147 lines
4.3 KiB
TypeScript
147 lines
4.3 KiB
TypeScript
import {
|
|
useCallback,
|
|
useEffect,
|
|
useRef,
|
|
useState,
|
|
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). */
|
|
const MESSAGE_MS = 6_000
|
|
const HINT_MS = 4_000
|
|
|
|
/** The document and the history a message was shown on: any change closes it (R-2). */
|
|
interface Stamp {
|
|
text: string
|
|
undo: number
|
|
redo: number
|
|
}
|
|
|
|
export interface GestureMessage {
|
|
text: string
|
|
tone: 'info' | 'error'
|
|
/** Offers « Défaire » (a kept gesture); never on a refusal. */
|
|
undo: boolean
|
|
hint: boolean
|
|
stamp: Stamp
|
|
/**
|
|
* Counts every message shown: a repeated identical refusal changes no document, yet it
|
|
* is a new pill and is announced again (M-3).
|
|
*/
|
|
serial: number
|
|
/** 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 {
|
|
const { documentText, undo, redo } = useEditorStore.getState()
|
|
return { text: documentText, undo: undo.length, redo: redo.length }
|
|
}
|
|
|
|
const sameStamp = (a: Stamp, b: Stamp) =>
|
|
a.text === b.text && a.undo === b.undo && a.redo === b.redo
|
|
|
|
/**
|
|
* Any change of the document or of either history stack closes the shown message for
|
|
* good: an undo then a redo restores the same stamp, which must not bring it back (R-2).
|
|
*/
|
|
function useCloseOnChange(close: (now: Stamp) => void): void {
|
|
useEffect(
|
|
() =>
|
|
useEditorStore.subscribe((state, before) => {
|
|
const changed =
|
|
state.documentText !== before.documentText ||
|
|
state.undo.length !== before.undo.length ||
|
|
state.redo.length !== before.redo.length
|
|
if (changed) close(currentStamp())
|
|
}),
|
|
[close],
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Hover with a mouse and focus inside hold the message; a touch never does (R-2). The hold
|
|
* belongs to the message it was taken on: a message that closes (« Défaire » pressed, the
|
|
* pill unmounted without pointerleave or blur) leaves no hold on the next one.
|
|
*/
|
|
function useHold(message: GestureMessage | null) {
|
|
const [hovered, setHovered] = useState<GestureMessage | null>(null)
|
|
const [focused, setFocused] = useState<GestureMessage | null>(null)
|
|
const handlers = {
|
|
onPointerEnter: (event: PointerEvent) => {
|
|
if (event.pointerType === 'mouse') setHovered(message)
|
|
},
|
|
onPointerLeave: () => setHovered(null),
|
|
onFocus: () => setFocused(message),
|
|
onBlur: (event: FocusEvent<HTMLElement>) => {
|
|
if (!event.currentTarget.contains(event.relatedTarget as Node | null)) {
|
|
setFocused(null)
|
|
}
|
|
},
|
|
}
|
|
const held = message !== null && (hovered === message || focused === message)
|
|
return { held, handlers }
|
|
}
|
|
|
|
/**
|
|
* The line under the drawing (A14, R-2): « Point déplacé · Défaire » after a gesture, a
|
|
* refusal in red, or a hint while nothing else is shown. A message lasts 6 s, held while a
|
|
* mouse hovers it or the focus is inside; any commit, undo or redo closes it.
|
|
*/
|
|
export function useGestureMessage() {
|
|
const [message, setShown] = useState<GestureMessage | null>(null)
|
|
const { held, handlers } = useHold(message)
|
|
// A message shown after its own commit carries that commit's stamp: it stays.
|
|
const close = useCallback(
|
|
(now: Stamp) =>
|
|
setShown((shown) =>
|
|
shown && sameStamp(shown.stamp, now) ? shown : null,
|
|
),
|
|
[],
|
|
)
|
|
useCloseOnChange(close)
|
|
const serial = useRef(0)
|
|
useEffect(() => {
|
|
if (!message || held) return
|
|
const timer = setTimeout(
|
|
() => setShown(null),
|
|
message.hint ? HINT_MS : MESSAGE_MS,
|
|
)
|
|
return () => clearTimeout(timer)
|
|
}, [message, held])
|
|
const show = (text: string, { undo, tone, place }: ShowOptions) =>
|
|
setShown({
|
|
text,
|
|
tone,
|
|
undo,
|
|
hint: false,
|
|
stamp: currentStamp(),
|
|
serial: ++serial.current,
|
|
place: place ?? null,
|
|
})
|
|
const hint = (text: string) => {
|
|
if (message) return
|
|
setShown({
|
|
text,
|
|
tone: 'info',
|
|
undo: false,
|
|
hint: true,
|
|
stamp: currentStamp(),
|
|
serial: ++serial.current,
|
|
place: null,
|
|
})
|
|
}
|
|
return { message, show, hint, hold: handlers }
|
|
}
|
|
|
|
export type GestureMessages = ReturnType<typeof useGestureMessage>
|