diff --git a/apps/app/src/editor/annotations/drawingGuard.ts b/apps/app/src/editor/annotations/drawingGuard.ts index fdebba49..620cfc03 100644 --- a/apps/app/src/editor/annotations/drawingGuard.ts +++ b/apps/app/src/editor/annotations/drawingGuard.ts @@ -25,11 +25,14 @@ export function commitFocusedSeries(): void { /** * After a press that hid or removed the focused handle (Safari focuses no clicked button): * the pressed control takes the focus, where the keyboard's press leaves it, never `body`. + * A removed handle drops the focus to `body` at once; a handle in a `hidden` panel keeps it + * until the browser's later fix-up, so a focus inside `[hidden]` counts as lost too. */ export function focusPressedIfLost(control: HTMLElement): void { const active = document.activeElement - if (active === null || active === document.body) - control.focus({ preventScroll: true }) + const lost = + active === null || active === document.body || !!active.closest('[hidden]') + if (lost) control.focus({ preventScroll: true }) } /** An unstarted mode ends; a pending series that is not the mode's stays. */ diff --git a/apps/app/tests/unit/editor-zone-edit.test.tsx b/apps/app/tests/unit/editor-zone-edit.test.tsx index d209d7d3..809d6de2 100644 --- a/apps/app/tests/unit/editor-zone-edit.test.tsx +++ b/apps/app/tests/unit/editor-zone-edit.test.tsx @@ -1,5 +1,4 @@ /** U2 Task 5: vertices, « + », « − », zone move, redraw, paths and the handle cap. */ -import { useLayoutEffect, useState } from 'react' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { act, @@ -16,12 +15,16 @@ import AnnotationsTab from '@/components/editor/tabs/AnnotationsTab' import EditorTabs from '@/components/editor/EditorTabs' import HistoryButtons from '@/components/editor/HistoryButtons' import KeptPlace from '@/components/editor/KeptPlace' -import { answerDrawing, guardDrawing } from '@/editor/annotations/drawingGuard' +import { + answerDrawing, + focusPressedIfLost, + guardDrawing, +} from '@/editor/annotations/drawingGuard' import { useAnnotationWork, resetWork } from '@/editor/annotations/workStore' import { useUndoShortcuts } from '@/editor/useUndoShortcuts' import { useLocaleStore } from '@/state/localeStore' import { fr } from '@/i18n/fr' -import type { EditorTabId } from '@/editor/tabs' +import { useEditorNavigation } from '@/editor/useEditorNavigation' import { stripLeadOf } from '@/editor/annotations/stripLead' import { initialUi } from '@/editor/annotations/uiState' import { drawnText, insert, point } from '../helpers/editorFixtures' @@ -1458,22 +1461,18 @@ describe('Safari: the focus after a press that commits a pending series', () => expect(document.activeElement).toBe(row) }) - /** The tab row over the annotations panel, which a browser drops the focus out of when hidden. */ + /** The tab row over the annotations panel, hidden with its focus left in place (a browser + * clears the focus of a hidden subtree later, never during the press). */ function TabsOverPanel() { - const [active, setActive] = useState('annotations') - useLayoutEffect(() => { - const held = document.activeElement - if (held instanceof HTMLElement && held.closest('[hidden]')) held.blur() - }) - const select = (next: EditorTabId) => { - if (!guardDrawing(() => undefined)) return false - setActive(next) - return true - } + const nav = useEditorNavigation('annotations') return ( <> - - {tab()} + + {tab()} ) } @@ -1494,6 +1493,35 @@ describe('Safari: the focus after a press that commits a pending series', () => expect(document.activeElement).toBe(target) }) + describe('focusPressedIfLost', () => { + afterEach(() => document.body.replaceChildren()) + + const pressed = () => + document.body.appendChild(document.createElement('button')) + + function held(hidden: boolean) { + const host = document.body.appendChild(document.createElement('div')) + host.hidden = hidden + const field = host.appendChild(document.createElement('input')) + field.focus() + return field + } + + it('takes the focus left inside a hidden panel (the browser clears it later)', () => { + const field = held(true) + expect(document.activeElement).toBe(field) + const control = pressed() + focusPressedIfLost(control) + expect(document.activeElement).toBe(control) + }) + + it('never steals the focus from a visible field or dialog', () => { + const field = held(false) + focusPressedIfLost(pressed()) + expect(document.activeElement).toBe(field) + }) + }) + it('« Déplacer le sommet » focuses the first arrow', async () => { await keyedVertex() fireEvent.click(screen.getByRole('button', { name: 'Déplacer le sommet' })) diff --git a/docs/development/V1-2-6_UX_PLAN.md b/docs/development/V1-2-6_UX_PLAN.md index f14bb88b..d71e62e7 100644 --- a/docs/development/V1-2-6_UX_PLAN.md +++ b/docs/development/V1-2-6_UX_PLAN.md @@ -479,8 +479,8 @@ Status: implemented on `feature/v1-2-anatomical-sheets` (lots A to H; Lot I, gat ### U2 · Direct manipulation of points and polygons - Status: implemented on `feature/v1-2-anatomical-sheets` (plan `.claude/tasks/plans/2026-11-17-v1-2-6-u2.md`, rev 4, - Tasks 1 to 16 and the final fix rounds done; gates run on the final head, figures in `V1-2-6_UX_TESTS.md`; owner - trial pending). The boards are `docs/design/v1-2-6-u2/` (A1 to A15 and the three + Tasks 1 to 16 and the final fix rounds done; gates ran at f5a6eafc, figures in `V1-2-6_UX_TESTS.md`, and the + controller reruns them on the final head; owner trial pending). The boards are `docs/design/v1-2-6-u2/` (A1 to A15 and the three adjustments, validated 2026-11-17). - Objective: boards 02 to 06, 13 and 31 to 33 of `v1-2-6-u2/` for points and polygons (curve handles excluded, curves lot): point move and size slider snapping to the radii of the view with the red minimum mark; vertex drag; « + » per