diff --git a/apps/app/src/components/editor/annotations/PointCommands.tsx b/apps/app/src/components/editor/annotations/PointCommands.tsx index cb295d59..cb078edd 100644 --- a/apps/app/src/components/editor/annotations/PointCommands.tsx +++ b/apps/app/src/components/editor/annotations/PointCommands.tsx @@ -1,5 +1,6 @@ import { useEffect, useRef, type Dispatch } from 'react' import { visibleElement } from '@/editor/focusRequest' +import { CANVAS_BOX } from '@/editor/useEditorNavigation' import type { Annotation, AnatomicalSheetDocument, @@ -64,21 +65,32 @@ function previewSize(annotation: Annotation, radius: number): void { }) } +/** What a mode ending gives the focus back to: the command that started it (R-12, M-6). */ +const ENTRY = { + move: '[data-testid="edit-strip"] [data-move-entry]', + redraw: '[data-testid="edit-strip"] [data-redraw-entry]', +} as const + +export type FocusMode = keyof typeof ENTRY | null + /** - * Focus back on « Déplacer le point » when the move mode ends with the focus lost (its bar - * unmounted under it: « Valider », « Annuler », Escape). Mounted by the strip, which - * outlives the commands it swaps. + * Focus back on the command that started the mode when it ends with the focus lost (its + * bar or the tracing's commands unmounted under it: « Valider », « Annuler », « Annuler le + * tracé », a closed trace): « Déplacer … » for a move, « Redessiner » (or « Remplacer par + * un polygone ») for a redraw; the drawing when that command is gone, never `body`. + * Mounted by the strip, which outlives the commands it swaps. */ -export function useMoveFocusReturn(inMode: boolean) { - const was = useRef(inMode) +export function useModeFocusReturn(mode: FocusMode) { + const was = useRef(mode) useEffect(() => { const active = document.activeElement const lost = active === null || active === document.body - if (was.current && !inMode && lost) { - visibleElement('[data-testid="edit-strip"] [data-move-entry]')?.focus() - } - was.current = inMode - }, [inMode]) + const ended = was.current + was.current = mode + if (ended === null || mode !== null || !lost) return + const entry = visibleElement(ENTRY[ended]) ?? visibleElement(CANVAS_BOX) + entry?.focus() + }, [mode]) } export interface SelectionPointProps { @@ -112,7 +124,7 @@ export function SelectionPointCommands(props: SelectionPointProps) { <>