From 03e85a7c67f1779f8cb5aa2c0a187d7382a12f9f Mon Sep 17 00:00:00 2001 From: bchanot Date: Thu, 19 Nov 2026 11:26:37 +0100 Subject: [PATCH] fix(editor): focus follows the action after a touch commit --- .../editor/annotations/TracingCommands.tsx | 11 ++++ .../src/editor/annotations/drawingGuard.ts | 9 ++- .../src/editor/annotations/usePressGesture.ts | 19 ++++++- .../app/tests/unit/editor-point-edit.test.tsx | 45 +++++++++++++++ apps/app/tests/unit/editor-zone-edit.test.tsx | 55 +++++++++++++++++++ 5 files changed, 135 insertions(+), 4 deletions(-) diff --git a/apps/app/src/components/editor/annotations/TracingCommands.tsx b/apps/app/src/components/editor/annotations/TracingCommands.tsx index 4773311e..445708c4 100644 --- a/apps/app/src/components/editor/annotations/TracingCommands.tsx +++ b/apps/app/src/components/editor/annotations/TracingCommands.tsx @@ -1,3 +1,4 @@ +import { useEffect, useRef } from 'react' import Button from '@/components/ui/Button' import { useT } from '@/i18n/useT' @@ -18,8 +19,18 @@ export default function TracingCommands({ onCancel, }: TracingCommandsProps) { const t = useT() + const box = useRef(null) + // The command that started the tracing (« Redessiner ») left with the strip's swap: the + // focus goes here, as long as nothing else holds it (never over a dialog or a field). + useEffect(() => { + const active = document.activeElement + if (active === null || active === document.body) { + box.current?.focus({ preventScroll: true }) + } + }, []) return (
{ + 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 }) + } + + it('a press on the point handle commits the slider change and focuses the handle', async () => { + await mount(withPoints(point('a', 50, 50, 5), point('b', 20, 20, 8))) + await select() + act(() => slider().focus()) + fireEvent.keyDown(slider(), { key: 'PageUp' }) + const before = depth() + tapDrag(handle().querySelector('[data-handle]')!) + expect(depth()).toBe(before + 1) + expect(document.activeElement).toBe(handle()) + }) + + it('a press on the handle commits its arrow series and keeps the handle focused', async () => { + await mount(withPoints(point('a', 50, 50, 5))) + await select() + act(() => handle().focus()) + fireEvent.keyDown(handle(), { key: 'ArrowRight' }) + const before = depth() + tapDrag(handle().querySelector('[data-handle]')!) + expect(depth()).toBe(before + 1) + 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))) + 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) + }) +}) diff --git a/apps/app/tests/unit/editor-zone-edit.test.tsx b/apps/app/tests/unit/editor-zone-edit.test.tsx index 7dccf5aa..c3e4d27e 100644 --- a/apps/app/tests/unit/editor-zone-edit.test.tsx +++ b/apps/app/tests/unit/editor-zone-edit.test.tsx @@ -1292,3 +1292,58 @@ describe('Safari: a strip command clicked with the focus left on a keyed vertex' expect(depth()).toBe(before + 1) }) }) + +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()) + await select() + clickHandle('Sommet 1 de N', [20, 20]) + act(() => vertex(1).focus()) + fireEvent.keyDown(vertex(1), { key: 'ArrowRight' }) + expect(useAnnotationWork.getState().preview?.source).toBe('keys') + } + + it('a press on another vertex commits the series and focuses that vertex', async () => { + await keyedVertex() + const before = depth() + clickHandle('Sommet 2 de N', [80, 20]) + expect(depth()).toBe(before + 1) + expect(pointsOf('z')![0]).toEqual([20.5, 20]) + 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() + pressAt(marker('z'), 50, 50) + expect(depth()).toBe(before + 1) + expect(document.activeElement).toBe(vertex(1)) + }) + + it('a press on « + » commits the series and keeps a vertex focused', async () => { + await keyedVertex() + clickHandle('Ajouter un sommet après le sommet 1 de N', [50, 20]) + expect(document.activeElement).not.toBe(document.body) + expect(document.activeElement?.getAttribute('data-vertex')).not.toBeNull() + }) + + it('« Redessiner » and « Déplacer le sommet » never leave it on body', 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) + }) + + it('« Désélectionner » focuses the row, as its key does', async () => { + await keyedVertex() + fireEvent.click(screen.getByRole('button', { name: 'Désélectionner' })) + expect(document.activeElement).not.toBe(document.body) + }) +})