fix(editor): give the focus back to « Redessiner » after a redraw

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.
This commit is contained in:
bchanot committed 2026-11-18 13:00:12 +01:00
1 parent 323a342586
commit e299c3d4f6
4 files changed
+121 -14

No files matched your search

@@ -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) {
<>
<SelectionSize {...props} />
<Button
// R-12: the focus comes back here when the mode ends (useMoveFocusReturn).
// R-12: the focus comes back here when the mode ends (useModeFocusReturn).
data-move-entry=""
type="button"
size="sm"
@@ -17,7 +17,8 @@ import EditStrip, { type StripNotice } from './EditStrip'
import {
SelectionPointCommands,
ToolSize,
useMoveFocusReturn,
type FocusMode,
useModeFocusReturn,
} from './PointCommands'
import SelectionCommands from './SelectionCommands'
import TracingCommands from './TracingCommands'
@@ -40,8 +41,8 @@ export default function SceneStrip({
const mode = useAnnotationWork((s) =>
s.mode?.kind === 'move' ? s.mode : null,
)
useMoveFocusReturn(mode !== null)
const { ui, selected, doc } = scene
useModeFocusReturn(useFocusMode(mode !== null, scene.tracing))
const chosenVertex = useChosenVertex(selected?.id)
const lead = mode
? {
@@ -73,6 +74,16 @@ export default function SceneStrip({
)
}
/**
* The mode whose end returns the focus: a move, or a redraw while its mode or its tracing
* lives (they end one after the other, in either order: the focus waits for both).
*/
function useFocusMode(moving: boolean, tracing: Scene['tracing']): FocusMode {
const redraw = useAnnotationWork((s) => s.mode?.kind === 'redraw')
if (moving) return 'move'
return redraw || tracing?.target === 'replace' ? 'redraw' : null
}
/** One box per set of commands: a wider set never moves an existing box (A14). */
function commandsKeyOf(
{ ui, tracing, creation }: Scene,
@@ -109,6 +120,8 @@ function ShapeCommands({ scene, work }: { scene: Scene; work: Work }) {
if (!traced || !model.measured) return null
return (
<Button
// M-6: the redraw's return target, as « Redessiner » (useModeFocusReturn).
data-redraw-entry=""
type="button"
size="sm"
variant="secondary"
@@ -72,7 +72,7 @@ export default function ZoneCommands({
<>
{annotation.kind === 'zone' && (
<Button
// R-12: the focus comes back here when the mode ends (useMoveFocusReturn).
// R-12: the focus comes back here when the mode ends (useModeFocusReturn).
data-move-entry=""
type="button"
size="sm"
@@ -85,6 +85,8 @@ export default function ZoneCommands({
</Button>
)}
<Button
// M-6: the focus comes back here when the redraw ends (useModeFocusReturn).
data-redraw-entry=""
type="button"
size="sm"
variant="secondary"