From e299c3d4f63f5c8fb7ac9c7b490893ff7bfb3e85 Mon Sep 17 00:00:00 2001 From: bchanot Date: Wed, 18 Nov 2026 13:00:12 +0100 Subject: [PATCH] =?UTF-8?q?fix(editor):=20give=20the=20focus=20back=20to?= =?UTF-8?q?=20=C2=AB=20Redessiner=20=C2=BB=20after=20a=20redraw?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Final review M-6. useMoveFocusReturn becomes useModeFocusReturn: the mode that ended picks its entry, « Déplacer … » (data-move-entry) for a move, « Redessiner » or « Remplacer par un polygone » (data-redraw-entry) for a redraw, the drawing when neither is shown. A redraw counts as running while its mode or its tracing lives, so a link polygon (no « Déplacer ») never leaves the focus on body. Tests: no assertion removed. --- .../editor/annotations/PointCommands.tsx | 34 +++++--- .../editor/annotations/SceneStrip.tsx | 17 +++- .../editor/annotations/ZoneCommands.tsx | 4 +- apps/app/tests/unit/editor-zone-edit.test.tsx | 80 +++++++++++++++++++ 4 files changed, 121 insertions(+), 14 deletions(-) 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) { <>