From 62b400e87615aef5202f63c4341196be9f1901b4 Mon Sep 17 00:00:00 2001 From: bchanot Date: Thu, 19 Nov 2026 12:00:48 +0100 Subject: [PATCH] =?UTF-8?q?fix(editor):=20=C2=AB=20=E2=88=92=20=C2=BB=20pr?= =?UTF-8?q?ess=20follows=20Delete,=20discriminating=20focus=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/app/e2e/shell/editor-touch.spec.ts | 5 ++ .../editor/annotations/HandleLayer.tsx | 2 +- .../editor/annotations/MoveModeBar.tsx | 2 +- .../editor/annotations/ZoneHandles.tsx | 4 +- .../src/editor/annotations/drawingGuard.ts | 2 + .../app/src/editor/annotations/useZoneEdit.ts | 3 +- .../unit/editor-annotation-dialog.test.tsx | 19 +++++ .../app/tests/unit/editor-point-edit.test.tsx | 62 +++++++++++---- apps/app/tests/unit/editor-zone-edit.test.tsx | 77 +++++++++++++------ 9 files changed, 131 insertions(+), 45 deletions(-) diff --git a/apps/app/e2e/shell/editor-touch.spec.ts b/apps/app/e2e/shell/editor-touch.spec.ts index 9582f8af..f18bb935 100644 --- a/apps/app/e2e/shell/editor-touch.spec.ts +++ b/apps/app/e2e/shell/editor-touch.spec.ts @@ -226,6 +226,8 @@ for (const width of WIDTHS) { }) const [, moved] = await shownPoints(page) expect(moved).not.toEqual(SQUARE[1]) + // The finger's commit leaves the focus on the dragged vertex, never on the page body. + await expect(page.locator('[data-vertex="1"]:focus')).toHaveCount(1) // The chosen vertex shows its « − » bubble: finger-sized too. await expect( page.getByRole('button', { name: /^Retirer le sommet/ }), @@ -242,6 +244,9 @@ for (const width of WIDTHS) { dict['editor.annotations.message.moved.point'], ) expect(await shownNumber(page, 'cx')).toBeLessThan(750) + await expect( + page.getByTestId('point-handle').and(page.locator(':focus')), + ).toHaveCount(1) // Zoomed in, the handles keep their CSS px size. await pinchOut(cdp, await clientOf(page, EMPTY.x, EMPTY.y)) expect((await framing(page)).scale).toBeGreaterThan(1) diff --git a/apps/app/src/components/editor/annotations/HandleLayer.tsx b/apps/app/src/components/editor/annotations/HandleLayer.tsx index 0c3848d8..5b1f6e02 100644 --- a/apps/app/src/components/editor/annotations/HandleLayer.tsx +++ b/apps/app/src/components/editor/annotations/HandleLayer.tsx @@ -167,7 +167,7 @@ function PointHandleShape({ tabIndex={0} aria-label={handle.label} data-testid="point-handle" - // Its arrow series commits on blur; the drawing guard blurs it before leaving. + // Its arrow series commits on blur; the drawing guard sends it that blur (focus kept). data-series="" className="group outline-none" onKeyDown={onKeyDown} diff --git a/apps/app/src/components/editor/annotations/MoveModeBar.tsx b/apps/app/src/components/editor/annotations/MoveModeBar.tsx index 5949ccbd..bf6f327d 100644 --- a/apps/app/src/components/editor/annotations/MoveModeBar.tsx +++ b/apps/app/src/components/editor/annotations/MoveModeBar.tsx @@ -30,7 +30,7 @@ export default function MoveModeBar({ }: MoveModeBarProps) { const t = useT() const first = useRef(null) - useEffect(() => first.current?.focus(), []) + useEffect(() => first.current?.focus({ preventScroll: true }), []) return (
{ARROWS.map(({ direction, glyph }, index) => ( diff --git a/apps/app/src/components/editor/annotations/ZoneHandles.tsx b/apps/app/src/components/editor/annotations/ZoneHandles.tsx index fb159582..465eaaaa 100644 --- a/apps/app/src/components/editor/annotations/ZoneHandles.tsx +++ b/apps/app/src/components/editor/annotations/ZoneHandles.tsx @@ -136,7 +136,7 @@ function Vertex({ aria-pressed={chosen} aria-keyshortcuts="Insert Plus Delete Backspace" data-vertex={index} - // Its arrow series commits on blur; the drawing guard blurs it before leaving. + // Its arrow series commits on blur; the drawing guard sends it that blur (focus kept). data-series="" className="group outline-none" onKeyDown={(event) => keys.onVertexKey(index, event)} @@ -272,7 +272,7 @@ function useFocusFollowsChoice(chosen: number | null) { const active = document.activeElement if (active?.getAttribute('data-vertex') === String(chosen)) return const target = element.querySelector(`[data-vertex="${chosen}"]`) - if (target instanceof SVGElement) target.focus() + if (target instanceof SVGElement) target.focus({ preventScroll: true }) }, [chosen, choice]) // Read after every commit: where the focus was before the next change unmounts it. useLayoutEffect(() => { diff --git a/apps/app/src/editor/annotations/drawingGuard.ts b/apps/app/src/editor/annotations/drawingGuard.ts index 280c6c5c..be13b3db 100644 --- a/apps/app/src/editor/annotations/drawingGuard.ts +++ b/apps/app/src/editor/annotations/drawingGuard.ts @@ -17,6 +17,8 @@ export function commitFocusedSeries(): void { const active = document.activeElement const series = active instanceof HTMLElement || active instanceof SVGElement if (!series || !active.closest('[data-series]')) return + // Commits through the element's own blur handler without moving the focus; nothing above + // a `[data-series]` element listens to blur. active.dispatchEvent(new FocusEvent('focusout', { bubbles: true })) } diff --git a/apps/app/src/editor/annotations/useZoneEdit.ts b/apps/app/src/editor/annotations/useZoneEdit.ts index edf4f828..2e92eb09 100644 --- a/apps/app/src/editor/annotations/useZoneEdit.ts +++ b/apps/app/src/editor/annotations/useZoneEdit.ts @@ -168,7 +168,8 @@ export function useZoneEdit(input: ZoneEditInput) { if (!id) return if (action.type === 'choose_vertex') chooseVertex(id, action.index) if (action.type === 'insert') insertAfter(input, action.after) - if (action.type === 'remove') removeChosen(input, action.index) + // As Delete does: the next vertex is chosen, so the focus lands on it. + if (action.type === 'remove') deleteChosen(input) } return { onHandle, diff --git a/apps/app/tests/unit/editor-annotation-dialog.test.tsx b/apps/app/tests/unit/editor-annotation-dialog.test.tsx index e5c9303b..1ff83560 100644 --- a/apps/app/tests/unit/editor-annotation-dialog.test.tsx +++ b/apps/app/tests/unit/editor-annotation-dialog.test.tsx @@ -178,6 +178,25 @@ describe('opening (A1)', () => { expect(await screen.findByRole('dialog')).toBeVisible() }) + it('a tap on the point handle with a size change pending: the dialog takes the focus, the row gets it back', async () => { + await mount( + insert(insert(drawnText(), point('a', 50, 50, 5)), point('b', 20, 20, 8)), + ) + await selectRow('a') + const slider = within(screen.getByTestId('size-slider')).getByRole('slider') + act(() => slider.focus()) + fireEvent.keyDown(slider, { key: 'PageUp' }) + const handle = screen.getByRole('button', { name: 'Point N' }) + pressAt(handle.querySelector('[data-handle]')!, 50, 50) + const opened = await screen.findByRole('dialog') + expect(opened.contains(document.activeElement)).toBe(true) + await userEvent + .setup() + .click(within(opened).getByRole('button', { name: 'Fermer' })) + await waitFor(() => expect(dialog()).toBeNull()) + expect(document.activeElement).toBe(rowOf('a')) + }) + it('closed, the focus goes to the drawing when a filter hides the row (board 32)', async () => { await mount(insert(drawnText(), point('a', 50, 50, 5))) await selectRow('a') diff --git a/apps/app/tests/unit/editor-point-edit.test.tsx b/apps/app/tests/unit/editor-point-edit.test.tsx index 4abced07..b2341eff 100644 --- a/apps/app/tests/unit/editor-point-edit.test.tsx +++ b/apps/app/tests/unit/editor-point-edit.test.tsx @@ -750,16 +750,30 @@ describe('Safari: « Déplacer le point » clicked with the focus left on the sl }) describe('Safari: the focus after a press that commits a pending change', () => { - const tapDrag = (el: Element) => { - const init = { - pointerId: 1, - isPrimary: true, - button: 0, - buttons: 1, - pointerType: 'mouse', - } - fireEvent.pointerDown(el, { ...init, clientX: 50, clientY: 50 }) - fireEvent.pointerMove(el, { ...init, clientX: 80, clientY: 80 }) + const init = { + pointerId: 1, + isPrimary: true, + button: 0, + buttons: 1, + pointerType: 'mouse', + } + const hit = () => handle().querySelector('[data-handle]')! + /** A finger on the handle that moves past the slop and lets go: a drag and its drop. */ + const dragHandle = () => { + fireEvent.pointerDown(hit(), { ...init, clientX: 50, clientY: 50 }) + fireEvent.pointerMove(hit(), { ...init, clientX: 80, clientY: 80 }) + fireEvent.pointerUp(hit(), { + ...init, + buttons: 0, + clientX: 80, + clientY: 80, + }) + } + /** The focus seen by every document change: at the commit, not after a later move. */ + function watchCommitFocus(): Array { + const seen: Array = [] + store.subscribe(() => seen.push(document.activeElement)) + return seen } it('a press on the point handle commits the slider change and focuses the handle', async () => { @@ -768,8 +782,10 @@ describe('Safari: the focus after a press that commits a pending change', () => act(() => slider().focus()) fireEvent.keyDown(slider(), { key: 'PageUp' }) const before = depth() - tapDrag(handle().querySelector('[data-handle]')!) - expect(depth()).toBe(before + 1) + const seen = watchCommitFocus() + dragHandle() + expect(depth()).toBe(before + 2) + expect(seen[0]).toBe(slider()) expect(document.activeElement).toBe(handle()) }) @@ -779,17 +795,29 @@ describe('Safari: the focus after a press that commits a pending change', () => act(() => handle().focus()) fireEvent.keyDown(handle(), { key: 'ArrowRight' }) const before = depth() - tapDrag(handle().querySelector('[data-handle]')!) - expect(depth()).toBe(before + 1) + const seen = watchCommitFocus() + dragHandle() + expect(depth()).toBe(before + 2) + expect(seen[0]).toBe(handle()) expect(document.activeElement).toBe(handle()) }) - it('« Redessiner » and « Désélectionner » never leave the focus on body', async () => { - await mount(withPoints(point('a', 50, 50, 5))) + it('« Déplacer le point » focuses the first arrow', async () => { + await mount(withPoints(point('a', 50, 50, 5), point('b', 20, 20, 8))) + await select() + act(() => slider().focus()) + fireEvent.keyDown(slider(), { key: 'PageUp' }) + fireEvent.click(moveButton()) + expect(document.activeElement).toBe(within(bar()).getAllByRole('button')[0]) + }) + + it('« Désélectionner » focuses the row', async () => { + await mount(withPoints(point('a', 50, 50, 5), point('b', 20, 20, 8))) await select() act(() => slider().focus()) fireEvent.keyDown(slider(), { key: 'PageUp' }) fireEvent.click(screen.getByRole('button', { name: 'Désélectionner' })) - expect(document.activeElement).not.toBe(document.body) + const list = screen.getByRole('list', { name: 'Annotations de la vue' }) + expect(document.activeElement).toBe(within(list).getAllByRole('button')[0]) }) }) diff --git a/apps/app/tests/unit/editor-zone-edit.test.tsx b/apps/app/tests/unit/editor-zone-edit.test.tsx index c3e4d27e..d0f633bd 100644 --- a/apps/app/tests/unit/editor-zone-edit.test.tsx +++ b/apps/app/tests/unit/editor-zone-edit.test.tsx @@ -237,7 +237,8 @@ describe('a vertex (R-9)', () => { clickHandle('Sommet 3 de N', [80, 80]) clickHandle('Retirer le sommet 3 de N', [80, 70]) expect(pointsOf('z')).toEqual([SQUARE[0], SQUARE[1], SQUARE[3]]) - expect(chosen()).toBeNull() + // As Delete: the next vertex is chosen (and focused). + expect(chosen()).toMatchObject({ vertex: 2 }) expect(message()).toHaveTextContent('Sommet retiré') }) @@ -1296,54 +1297,84 @@ describe('Safari: a strip command clicked with the focus left on a keyed vertex' describe('Safari: the focus after a press that commits a pending series', () => { // A tap that moves no focus (jsdom moves none either): the commit must not send the focus // to body, and the press lands it where the same action sends it from the keyboard. - async function keyedVertex() { - await mount(squareText()) + const PENTAGON: Point[] = [...SQUARE, [50, 95]] + + async function keyedVertex(n = 1, points: Point[] = SQUARE) { + await mount(squareText(points)) await select() - clickHandle('Sommet 1 de N', [20, 20]) - act(() => vertex(1).focus()) - fireEvent.keyDown(vertex(1), { key: 'ArrowRight' }) + clickHandle(`Sommet ${n} de N`, [20, 20]) + act(() => vertex(n).focus()) + fireEvent.keyDown(vertex(n), { key: 'ArrowRight' }) expect(useAnnotationWork.getState().preview?.source).toBe('keys') } - it('a press on another vertex commits the series and focuses that vertex', async () => { + /** The focus seen by every document change: at the commit, not after a refocus effect. */ + function watchCommitFocus(): Array { + const seen: Array = [] + store.subscribe(() => seen.push(document.activeElement)) + return seen + } + + it('a press on another vertex commits with the focus kept, then focuses it', async () => { await keyedVertex() - const before = depth() + const seen = watchCommitFocus() clickHandle('Sommet 2 de N', [80, 20]) - expect(depth()).toBe(before + 1) expect(pointsOf('z')![0]).toEqual([20.5, 20]) + expect(seen[0]).toBe(vertex(1)) expect(document.activeElement).toBe(vertex(2)) }) it('a press on the interior keeps the focus on the handle that held the series', async () => { await keyedVertex() - const before = depth() + const seen = watchCommitFocus() pressAt(marker('z'), 50, 50) - expect(depth()).toBe(before + 1) + expect(seen[0]).toBe(vertex(1)) expect(document.activeElement).toBe(vertex(1)) }) - it('a press on « + » commits the series and keeps a vertex focused', async () => { + it('a press on « + » commits with the focus kept', async () => { await keyedVertex() + const seen = watchCommitFocus() clickHandle('Ajouter un sommet après le sommet 1 de N', [50, 20]) - expect(document.activeElement).not.toBe(document.body) + expect(seen[0]).toBe(vertex(1)) expect(document.activeElement?.getAttribute('data-vertex')).not.toBeNull() }) - it('« Redessiner » and « Déplacer le sommet » never leave it on body', async () => { + it.each([ + ['a middle vertex', 2, 2], + ['the last vertex', 5, 4], + ])( + 'a press on « − » of %s focuses and chooses the next vertex', + async (_n, n, next) => { + await keyedVertex(n, PENTAGON) + const held = vertex(n) + const seen = watchCommitFocus() + clickHandle(`Retirer le sommet ${n} de N`) + expect(pointsOf('z')).toHaveLength(4) + expect(seen[0]).toBe(held) + expect(document.activeElement).toBe(vertex(next)) + expect(useAnnotationWork.getState().chosen?.vertex).toBe(next - 1) + }, + ) + + it('« Déplacer le sommet » focuses the first arrow', async () => { await keyedVertex() fireEvent.click(screen.getByRole('button', { name: 'Déplacer le sommet' })) - expect(document.activeElement).not.toBe(document.body) - cleanup() - resetStore() - resetWork() - await keyedVertex() - fireEvent.click(screen.getByRole('button', { name: 'Redessiner' })) - expect(document.activeElement).not.toBe(document.body) + expect(document.activeElement).toBe( + within(screen.getByTestId('move-mode-bar')).getAllByRole('button')[0], + ) }) - it('« Désélectionner » focuses the row, as its key does', async () => { + it('« Redessiner » focuses the tracing commands', async () => { + await keyedVertex() + fireEvent.click(screen.getByRole('button', { name: 'Redessiner' })) + expect(document.activeElement).toBe(screen.getByTestId('tracing-commands')) + }) + + it('« Désélectionner » focuses the row', async () => { await keyedVertex() fireEvent.click(screen.getByRole('button', { name: 'Désélectionner' })) - expect(document.activeElement).not.toBe(document.body) + const list = screen.getByRole('list', { name: 'Annotations de la vue' }) + expect(document.activeElement).toBe(within(list).getAllByRole('button')[0]) }) })