fix(editor): a guarded press that hides the handle keeps the focus off body

This commit is contained in:
bchanot committed 2026-11-19 17:16:56 +01:00
1 parent 8df52fd1be
commit fa00e57652
4 files changed
+66 -7

No files matched your search

+42 -2
View File
@@ -1,4 +1,5 @@
/** 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,
@@ -12,12 +13,15 @@ import {
import userEvent from '@testing-library/user-event'
import type { Annotation, Point } from '@faunosteo/core-wasm/types'
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 { 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 { stripLeadOf } from '@/editor/annotations/stripLead'
import { initialUi } from '@/editor/annotations/uiState'
import { drawnText, insert, point } from '../helpers/editorFixtures'
@@ -101,9 +105,9 @@ const tab = (readOnly = false) => (
</div>
)
async function mount(text: string) {
async function mount(text: string, ui = tab()) {
await openReady({ text })
const view = render(tab())
const view = render(ui)
await waitFor(() =>
expect(
screen.getByTestId('annotation-canvas-drawing').querySelector('svg'),
@@ -1454,6 +1458,42 @@ 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. */
function TabsOverPanel() {
const [active, setActive] = useState<EditorTabId>('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
}
return (
<>
<EditorTabs active={active} tier="wide" onSelect={select} />
<KeptPlace visible={active === 'annotations'}>{tab()}</KeptPlace>
</>
)
}
it('a press on an editor tab commits with the focus kept, then focuses the tab', async () => {
await mount(squareText(), <TabsOverPanel />)
await select()
clickHandle('Sommet 1 de N', [20, 20])
act(() => vertex(1).focus())
fireEvent.keyDown(vertex(1), { key: 'ArrowRight' })
const held = vertex(1)
const seen = watchCommitFocus()
const target = screen.getByRole('tab', { name: 'Général' })
fireEvent.click(target)
expect(pointsOf('z')![0]).toEqual([20.5, 20])
expect(seen[0]).toBe(held)
expect(target).toHaveAttribute('aria-selected', 'true')
expect(document.activeElement).toBe(target)
})
it('« Déplacer le sommet » focuses the first arrow', async () => {
await keyedVertex()
fireEvent.click(screen.getByRole('button', { name: 'Déplacer le sommet' }))