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"
@@ -1005,3 +1005,83 @@ describe('final review I-1: a redo or a handle key never drops pending work', ()
expect(screen.queryByText(ABANDONED)).toBeNull()
})
})
describe('final review M-6: a redraw gives the focus back to « Redessiner »', () => {
const canvas = () => screen.getByTestId('annotation-canvas')
const TRACE: [number, number][] = [
[30, 30],
[70, 30],
[70, 70],
[30, 70],
]
const redrawButton = () => screen.getByRole('button', { name: 'Redessiner' })
async function redrawing(text = squareText()) {
await mount(text)
await select()
await userEvent.setup().click(redrawButton())
}
const press = (name: string) =>
userEvent.setup().click(screen.getByRole('button', { name }))
it('after « Annuler le tracé »', async () => {
await redrawing()
pressAt(canvas(), 30, 30)
await press('Annuler le tracé')
expect(useAnnotationWork.getState().mode).toBeNull()
expect(document.activeElement).toBe(redrawButton())
})
it('after a closed redraw', async () => {
await redrawing()
TRACE.forEach(([x, y]) => pressAt(canvas(), x, y))
await press('Fermer le tracé')
expect(pointsOf('z')).toEqual(TRACE)
expect(document.activeElement).toBe(redrawButton())
})
it('after a refused close, then « Annuler le tracé »', async () => {
await redrawing()
for (const [x, y] of [
[20, 50],
[80, 50],
[80, 50.5],
])
pressAt(canvas(), x, y)
await press('Fermer le tracé')
expect(useAnnotationWork.getState().mode?.started).toBe(true)
await press('Annuler le tracé')
expect(document.activeElement).toBe(redrawButton())
})
it('on a link polygon (no « Déplacer »), never on body', async () => {
const head = insert(drawnText(), {
kind: 'view',
value: {
kind: 'detail',
id: 'view-det',
name: { fr: 'Tête', en: 'Head' },
svg_asset_id: null,
view_box: { min_x: 10, min_y: 10, width: 80, height: 80 },
parent_view_id: 'view-main',
},
})
const link = {
id: 'l',
view_id: 'view-main',
category_id: 'cat-a',
laterality: 'unpaired',
geometry: { type: 'polygon', points: SQUARE },
review_status: 'to_confirm',
color: null,
kind: 'detail_trigger',
name: null,
opens_view_id: 'view-det',
} as Annotation
await redrawing(insert(head, { kind: 'annotation', value: link }))
expect(screen.queryByRole('button', { name: /^Déplacer/ })).toBeNull()
pressAt(canvas(), 30, 30)
await press('Annuler le tracé')
expect(document.activeElement).not.toBe(document.body)
expect(document.activeElement).toBe(redrawButton())
})
})