From 922bd50a5b748052055dd26356bcfdda21297cd2 Mon Sep 17 00:00:00 2001 From: bchanot Date: Sat, 21 Nov 2026 13:02:59 +0100 Subject: [PATCH] =?UTF-8?q?feat(editor):=20=C2=AB=20Placer=20au=20centre?= =?UTF-8?q?=20=C2=BB=20in=20the=20add=20row=20while=20a=20tool=20is=20arme?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A screen-reader user can create an annotation without the drawing: the command places the armed kind at the centre through the one placement function shared with Enter and the add buttons' keyboard path. --- apps/app/e2e/shell/editor-annotations.spec.ts | 45 ++++ .../annotations/EditableAnnotations.tsx | 35 ++- .../editor/annotations/KeyboardAdd.tsx | 21 +- .../src/editor/annotations/usePlacement.ts | 37 +++- apps/app/src/i18n/en.ts | 7 +- apps/app/src/i18n/fr.ts | 7 +- .../tests/unit/editor-add-buttons.test.tsx | 206 +++++++++++++++++- 7 files changed, 323 insertions(+), 35 deletions(-) diff --git a/apps/app/e2e/shell/editor-annotations.spec.ts b/apps/app/e2e/shell/editor-annotations.spec.ts index edf7b40e..d42c54e1 100644 --- a/apps/app/e2e/shell/editor-annotations.spec.ts +++ b/apps/app/e2e/shell/editor-annotations.spec.ts @@ -1166,5 +1166,50 @@ test.describe('editor annotations (A3)', () => { await expect(tool(page, 'zone')).toHaveAttribute('aria-pressed', 'true') }) } + + // « Placer au centre »: the fourth slot of the add row, reserved at all times. + const centre = (page: Page) => page.locator('[data-place-centre]') + const addRow = (page: Page) => + page.locator('[data-add-kind], [data-place-centre]') + + for (const width of [1024, 1180, 1280]) { + test(`at ${width} px: arming Point shows « Placer au centre » on the add row's line, nothing moves`, async ({ + page, + requests, + }) => { + await page.setViewportSize({ width, height: 800 }) + const { actor } = await openAnnotations(page) + requests.forbid(actor.account.email, actor.account.password) + await add(page, 'point').scrollIntoViewIfNeeded() + await expect(centre(page)).toBeHidden() + await expectNoShift(page, async () => { + await add(page, 'point').click() + await expect(centre(page)).toBeVisible() + }) + const tops = await addRow(page).evaluateAll((els) => + els.map((el) => Math.round(el.getBoundingClientRect().top)), + ) + expect(tops, 'four buttons on one line').toHaveLength(4) + expect(new Set(tops).size, `tops ${tops.join(',')}`).toBe(1) + await centre(page).click() + await expect(creationDialog(page, 'point')).toBeVisible() + }) + } + + test('at 1280 px: a double click on « Placer au centre » keeps its dialog, asks nothing', async ({ + page, + requests, + }) => { + await page.setViewportSize({ width: 1280, height: 800 }) + const { actor } = await openAnnotations(page) + requests.forbid(actor.account.email, actor.account.password) + await add(page, 'point').scrollIntoViewIfNeeded() + await add(page, 'point').click() + await centre(page).dblclick() + await expect(creationDialog(page, 'point')).toBeVisible() + await expect( + page.getByText(dict['editor.annotations.guard.creation']), + ).toHaveCount(0) + }) }) }) diff --git a/apps/app/src/components/editor/annotations/EditableAnnotations.tsx b/apps/app/src/components/editor/annotations/EditableAnnotations.tsx index 3517e64a..0b005a7d 100644 --- a/apps/app/src/components/editor/annotations/EditableAnnotations.tsx +++ b/apps/app/src/components/editor/annotations/EditableAnnotations.tsx @@ -7,11 +7,8 @@ import type { } from '@faunosteo/core-wasm/types' import type { CommitContext } from '@/editor/annotations/annotationCommit' import { useAnnotationRequest } from '@/editor/annotations/focusTarget' -import { keyboardPending, type NewKind } from '@/editor/annotations/creation' -import { - creationTracing, - placePending, -} from '@/editor/annotations/creationWork' +import type { NewKind } from '@/editor/annotations/creation' +import { creationTracing } from '@/editor/annotations/creationWork' import { useCreationFollow } from '@/editor/annotations/useCreationFollow' import { PENDING_ID, @@ -38,7 +35,11 @@ import { useLayerFollow, type OpenDialog, } from '@/editor/annotations/dialogOpening' -import { creationStart, usePlacement } from '@/editor/annotations/usePlacement' +import { + placeAtCentre, + TOOL_TARGET, + usePlacement, +} from '@/editor/annotations/usePlacement' import { useGestureMessage, type GestureMessages, @@ -319,7 +320,7 @@ function CanvasColumn({ disabled={!model.measured || noCategory || scene.frame === null} linkDisabled={model.children.length === 0} armed={armedTool(scene.ui)} - announce={scene.ui.armedFrom === 'add'} + centre={centreOf(scene)} onAdd={(kind) => guarded(() => addAtCentre(kind, scene))()} onArm={(kind, button) => armFromAdd(kind, button, scene)} /> @@ -469,11 +470,21 @@ function armFromAdd( /** « Ajouter … » (keyboard): the shape at the centre at the A5 size, then its dialog. */ function addAtCentre(kind: NewKind, scene: Scene) { - const { frame, doc, viewId, ui } = scene - if (!frame) return - const size = newPointSize(doc.annotations, viewId, frame, ui.size) - const start = creationStart(scene, 'add') - placePending(start, keyboardPending(kind, frame, size)) + placeAtCentre(scene, kind, 'add') +} + +/** + * « Placer au centre »: available while a creation tool is armed and nothing is started + * (a redraw's lent tool is not an armed tool); null otherwise, for the reserved slot. + */ +function centreOf(scene: Scene): (() => void) | null { + const { ui, doc, model, frame, creation } = scene + const tool = armedTool(ui) + if (tool === 'select' || creation !== null || ui.tracing !== null) return null + if (frame === null || !model.measured || doc.categories.length === 0) + return null + if (tool === 'link' && model.children.length === 0) return null + return () => placeAtCentre(scene, TOOL_TARGET[tool], ui.armedFrom) } /** Board 02 A: the next point's size under a mouse pointer, while the point tool places. */ diff --git a/apps/app/src/components/editor/annotations/KeyboardAdd.tsx b/apps/app/src/components/editor/annotations/KeyboardAdd.tsx index c96e48fe..7e53f83e 100644 --- a/apps/app/src/components/editor/annotations/KeyboardAdd.tsx +++ b/apps/app/src/components/editor/annotations/KeyboardAdd.tsx @@ -14,13 +14,14 @@ const ARMED = * tool, or disarms it when armed, so the shape is placed where the drawing is pressed and its * dialog opens itself; the second click of a double-click (`detail` 2) changes nothing; Enter * or Space (`detail` 0) keeps the C7 keyboard path: the shape at the centre of the frame at - * the A5 size, its dialog at once. + * the A5 size, its dialog at once. « Placer au centre » does the same for the armed tool, + * from a fourth slot reserved at all times so nothing moves when it appears. */ export default function KeyboardAdd({ disabled, linkDisabled, armed, - announce, + centre, onAdd, onArm, }: { @@ -30,8 +31,8 @@ export default function KeyboardAdd({ linkDisabled: boolean /** The armed tool (`armedTool`): its button shows pressed. */ armed: ToolId - /** An add button armed the tool: the region says so to a screen reader. */ - announce: boolean + /** « Placer au centre » when it can be used now, else null: its slot stays reserved. */ + centre: (() => void) | null onAdd: (kind: NewKind) => void onArm: (kind: NewKind, button: HTMLElement) => void }) { @@ -40,7 +41,7 @@ export default function KeyboardAdd({ if (event.detail === 0) onAdd(kind) else if (event.detail === 1) onArm(kind, event.currentTarget) } - const armedKind = announce + const armedKind = centre ? KINDS.find((k) => TOOL_OF[k] === armed) : undefined return ( @@ -60,6 +61,16 @@ export default function KeyboardAdd({ {t(`editor.annotations.add.${kind}`)} ))} + {/* Always mounted: the region exists before its text (M-3). */}

{armedKind ? t(`editor.annotations.armed.${armedKind}`) : ''} diff --git a/apps/app/src/editor/annotations/usePlacement.ts b/apps/app/src/editor/annotations/usePlacement.ts index 9bbb1838..87207c8c 100644 --- a/apps/app/src/editor/annotations/usePlacement.ts +++ b/apps/app/src/editor/annotations/usePlacement.ts @@ -28,7 +28,7 @@ import { endMode, PENDING_ID, useAnnotationWork } from './workStore' import type { ToolId, Tracing, UiAction, UiState } from './uiState' import type { GestureMessages } from './useGestureMessage' -const TOOL_TARGET: Record, NewKind> = { +export const TOOL_TARGET: Record, NewKind> = { point: 'point', zone: 'zone', link: 'detail_trigger', @@ -81,7 +81,7 @@ function closeRedraw(input: PlacementInput, tracing: Tracing): void { } /** Where a creation on this view starts: F, the origin, a fresh form (A4). */ -export const creationStart = ( +const creationStart = ( { viewId, frame, doc }: Pick, origin: CreationStart['origin'], ): CreationStart => ({ viewId, frame, origin, form: emptyForm(doc) }) @@ -95,21 +95,36 @@ function popTrace({ tracing, dispatch }: PlacementInput): void { } /** - * The screen-reader path: Enter on the drawing while an add tool is armed and nothing is - * started places the kind's shape at the centre, as the C7 keyboard path does. + * The one placement at the centre: the kind's shape at the centre of the frame at the A5 + * size, its dialog at once. « Placer au centre », Enter on the drawing and the add buttons' + * keyboard path all end here; a stale strip hint goes first. + */ +export function placeAtCentre( + scene: Pick, + kind: NewKind, + origin: CreationStart['origin'], +): void { + const { ui, frame, doc, viewId, dispatch } = scene + if (!frame || useAnnotationWork.getState().creation) return + dispatch({ type: 'hint', hint: null }) + const size = newPointSize(doc.annotations, viewId, frame, ui.size) + placePending( + creationStart(scene, origin), + keyboardPending(kind, frame, size), + ) +} + +/** + * Enter on the drawing while an add tool is armed and nothing is started places the kind's + * shape at the centre, as the C7 keyboard path does. */ function placeAtCentreOnEnter(input: PlacementInput, event: KeyboardEvent) { - const { ui, frame, doc, viewId } = input + const { ui, frame } = input if (event.key !== 'Enter' || ui.tool === 'select' || !frame) return if (event.ctrlKey || event.metaKey || event.altKey || event.shiftKey) return if (useAnnotationWork.getState().creation) return event.preventDefault() - const kind = TOOL_TARGET[ui.tool] - const size = newPointSize(doc.annotations, viewId, frame, ui.size) - placePending( - creationStart(input, ui.armedFrom), - keyboardPending(kind, frame, size), - ) + placeAtCentre(input, TOOL_TARGET[ui.tool], ui.armedFrom) } /** diff --git a/apps/app/src/i18n/en.ts b/apps/app/src/i18n/en.ts index b46e9449..4294c736 100644 --- a/apps/app/src/i18n/en.ts +++ b/apps/app/src/i18n/en.ts @@ -1767,12 +1767,13 @@ export const en: Dict = { 'editor.annotations.add.point': 'Add a point', 'editor.annotations.add.zone': 'Add a zone', 'editor.annotations.add.detail_trigger': 'Add a link', + 'editor.annotations.place_centre': 'Place at the centre', 'editor.annotations.armed.point': - 'Point tool on: tap or click the drawing where you want it, or press Enter on the drawing to place the point at the centre.', + 'Point tool on: tap or click the drawing where you want it, or choose “Place at the centre”, below the drawing (Enter on the drawing does the same).', 'editor.annotations.armed.zone': - 'Zone tool on: place the vertices on the drawing, or press Enter on the drawing to place a zone at the centre.', + 'Zone tool on: place the vertices on the drawing, or choose “Place at the centre”, below the drawing (Enter on the drawing does the same).', 'editor.annotations.armed.detail_trigger': - 'Link tool on: place the vertices on the drawing, or press Enter on the drawing to place a link at the centre.', + 'Link tool on: place the vertices on the drawing, or choose “Place at the centre”, below the drawing (Enter on the drawing does the same).', 'editor.annotations.new.point': 'New point', 'editor.annotations.new.zone': 'New zone', 'editor.annotations.new.detail_trigger': 'New link', diff --git a/apps/app/src/i18n/fr.ts b/apps/app/src/i18n/fr.ts index 2a37b131..a45cec43 100644 --- a/apps/app/src/i18n/fr.ts +++ b/apps/app/src/i18n/fr.ts @@ -1833,12 +1833,13 @@ export const fr: Dict = { 'editor.annotations.add.point': 'Ajouter un point', 'editor.annotations.add.zone': 'Ajouter une zone', 'editor.annotations.add.detail_trigger': 'Ajouter un accès', + 'editor.annotations.place_centre': 'Placer au centre', 'editor.annotations.armed.point': - "Outil Point actif : touchez ou cliquez sur le dessin à l'endroit voulu, ou appuyez sur Entrée dans le dessin pour placer le point au centre.", + "Outil Point actif : touchez ou cliquez sur le dessin à l'endroit voulu, ou choisissez « Placer au centre », sous le dessin (Entrée dans le dessin fait de même).", 'editor.annotations.armed.zone': - 'Outil Zone actif : posez les sommets sur le dessin, ou appuyez sur Entrée dans le dessin pour placer une zone au centre.', + 'Outil Zone actif : posez les sommets sur le dessin, ou choisissez « Placer au centre », sous le dessin (Entrée dans le dessin fait de même).', 'editor.annotations.armed.detail_trigger': - 'Outil Accès actif : posez les sommets sur le dessin, ou appuyez sur Entrée dans le dessin pour placer un accès au centre.', + 'Outil Accès actif : posez les sommets sur le dessin, ou choisissez « Placer au centre », sous le dessin (Entrée dans le dessin fait de même).', 'editor.annotations.new.point': 'Nouveau point', 'editor.annotations.new.zone': 'Nouvelle zone', 'editor.annotations.new.detail_trigger': 'Nouvel accès', diff --git a/apps/app/tests/unit/editor-add-buttons.test.tsx b/apps/app/tests/unit/editor-add-buttons.test.tsx index d69845d8..6a20d97f 100644 --- a/apps/app/tests/unit/editor-add-buttons.test.tsx +++ b/apps/app/tests/unit/editor-add-buttons.test.tsx @@ -1,7 +1,9 @@ /** U2 trial adjustments, Task 6: « Ajouter … » arms its tool on a pointer press. */ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { + act, cleanup, + fireEvent, render, screen, waitFor, @@ -16,7 +18,7 @@ import { useLocaleStore } from '@/state/localeStore' import { drawnText, insert, point } from '../helpers/editorFixtures' import { LayeredTab } from '../helpers/annotationLayer' import { addByKeyboard } from '../helpers/addByKeyboard' -import { openReady, pressAt, resetStore } from '../helpers/editorHarness' +import { openReady, pressAt, resetStore, store } from '../helpers/editorHarness' vi.mock('@/lib/supabase', () => ({ getSupabaseClient: () => ({}) })) vi.mock('@faunosteo/supabase', async (orig) => ({ @@ -313,6 +315,185 @@ describe('the screen-reader path', () => { }) }) +const centreButton = () => + screen.getByRole('button', { name: 'Placer au centre' }) +/** No stylesheet in happy-dom: the hidden slot shows as its `invisible` class (CSS visibility). */ +const centreSlot = () => document.querySelector('[data-place-centre]')! +const noCentre = () => expect(centreSlot()).toHaveClass('invisible') + +describe('« Placer au centre »', () => { + it('is reachable with a creation tool armed, hidden with Select', async () => { + const user = await mount() + noCentre() + await user.click(tool('Point')) + expect(centreSlot()).not.toHaveClass('invisible') + expect(centreButton()).toBeEnabled() + await user.click(tool('Sélectionner et déplacer')) + noCentre() + }) + + it('opens the dialog at the centre; Abandon returns to its opener', async () => { + const user = await mount() + await user.click(tool('Point')) + await user.click(centreButton()) + await dialog('Nouveau point') + expect(ghost()).toHaveAttribute('cx', '50') + await abandon(user) + await waitFor(() => expect(tool('Point')).toHaveFocus()) + }) + + it('armed from « Ajouter une zone »: the zone dialog, focus back on it', async () => { + const user = await mount() + await user.click(add('une zone')) + await user.click(centreButton()) + await dialog('Nouvelle zone') + expect(ghost().tagName).toBe('polygon') + await abandon(user) + await waitFor(() => expect(add('une zone')).toHaveFocus()) + }) + + it('answers Enter, Space and a click with detail 0', async () => { + for (const press of ['{Enter}', ' ', 'click'] as const) { + const user = await mount() + await user.click(tool('Point')) + if (press === 'click') fireEvent.click(centreButton()) + else { + centreButton().focus() + await user.keyboard(press) + } + await dialog('Nouveau point') + cleanup() + resetStore() + resetWork() + } + }) + + it('is not available during a creation nor a tracing', async () => { + const user = await mount() + await user.click(tool('Point')) + await user.click(centreButton()) + const shown = await dialog('Nouveau point') + noCentre() + await user.click( + within(shown).getByRole('button', { name: 'Replacer le point' }), + ) + await waitFor(noDialog) + noCentre() + await user.click(tool('Zone')) + pressAt(canvas(), 30, 30) + await waitFor(() => + expect(useAnnotationWork.getState().creation).not.toBeNull(), + ) + noCentre() + }) + + it('is not available when nothing can be created', async () => { + const bare = JSON.parse(drawnText()) + bare.categories = [] + await openReady({ text: JSON.stringify(bare) }) + render() + noCentre() + expect(tool('Point')).toBeDisabled() + cleanup() + resetStore() + resetWork() + await mount() + expect(tool('Accès à une vue détaillée')).toBeDisabled() + noCentre() + }) + + it('goes away when the last category is removed while Point is armed', async () => { + const user = await mount() + await user.click(tool('Point')) + expect(centreSlot()).not.toHaveClass('invisible') + act(() => { + store + .getState() + .commit({ op: 'remove', key: { kind: 'category', id: 'cat-a' } }) + }) + await waitFor(noCentre) + expect(tool('Point')).toHaveAttribute('aria-pressed', 'true') + }) + + it('goes away when the last link child disappears while Link is armed', async () => { + const user = await mount(withDetail()) + await user.click(tool('Accès à une vue détaillée')) + expect(centreSlot()).not.toHaveClass('invisible') + act(() => { + store + .getState() + .commit({ op: 'remove', key: { kind: 'view', id: 'view-det' } }) + }) + await waitFor(noCentre) + expect(tool('Accès à une vue détaillée')).toHaveAttribute( + 'aria-pressed', + 'true', + ) + }) + + it('is unavailable while the drawing could not be built, the Point tool armed', async () => { + await openReady() + const { preview } = store.getState() + vi.spyOn(preview!, 'canvas').mockReturnValue({ + ok: false, + kind: 'svg_rejected', + }) + render() + await waitFor(() => expect(tool('Point')).toBeEnabled()) + const user = userEvent.setup() + await user.click(tool('Point')) + expect(tool('Point')).toHaveAttribute('aria-pressed', 'true') + expect(canvas().querySelector('[data-anno-id]')).toBeNull() + noCentre() + }) + + it('clears a stale hint', async () => { + const user = await mount() + await user.click(tool('Point')) + pressAt(canvas(), -5, 50) + expect( + await screen.findByText("Cliquez à l'intérieur du dessin."), + ).toBeInTheDocument() + await user.click(centreButton()) + await dialog('Nouveau point') + expect(screen.queryByText("Cliquez à l'intérieur du dessin.")).toBeNull() + }) + + it('Enter on « Ajouter un point » places a point while Zone is armed', async () => { + const user = await mount() + await user.click(tool('Zone')) + await addByKeyboard(user, add('un point')) + await dialog('Nouveau point') + }) + + it('the status reads the armed text from the column, not during a creation', async () => { + const user = await mount() + await user.click(tool('Point')) + expect(status()).toHaveTextContent('Placer au centre') + await user.click(tool('Sélectionner et déplacer')) + await addByKeyboard(user, add('un point')) + const shown = await dialog('Nouveau point') + expect(status()).toBeEmptyDOMElement() + await user.click( + within(shown).getByRole('button', { name: 'Replacer le point' }), + ) + await waitFor(noDialog) + expect(status()).toBeEmptyDOMElement() + }) + + it('stays available with a selection, the strip unchanged', async () => { + const user = await mount(insert(drawnText(), point('pt-a', 50, 50, 5))) + const list = await screen.findByRole('list', { + name: 'Annotations de la vue', + }) + await user.click(within(list).getAllByRole('button')[0]) + const before = lead().textContent + await user.click(tool('Point')) + expect(centreButton()).toBeEnabled() + expect(lead().textContent).toBe(before) + }) +}) + describe('a redraw lends the zone tool', () => { it('« Ajouter une zone » is not armed, and arms a NEW zone when pressed', async () => { const user = await mount(withZone()) @@ -335,4 +516,27 @@ describe('the dictionaries', () => { for (const kind of ['point', 'zone', 'detail_trigger']) expect(dict[`editor.annotations.armed.${kind}`]).toBeTruthy() }) + + it('word « Placer au centre » and its hint exactly, below the drawing', () => { + expect(fr['editor.annotations.place_centre']).toBe('Placer au centre') + expect(en['editor.annotations.place_centre']).toBe('Place at the centre') + expect(fr['editor.annotations.armed.point']).toBe( + "Outil Point actif : touchez ou cliquez sur le dessin à l'endroit voulu, ou choisissez « Placer au centre », sous le dessin (Entrée dans le dessin fait de même).", + ) + expect(fr['editor.annotations.armed.zone']).toBe( + 'Outil Zone actif : posez les sommets sur le dessin, ou choisissez « Placer au centre », sous le dessin (Entrée dans le dessin fait de même).', + ) + expect(fr['editor.annotations.armed.detail_trigger']).toBe( + 'Outil Accès actif : posez les sommets sur le dessin, ou choisissez « Placer au centre », sous le dessin (Entrée dans le dessin fait de même).', + ) + expect(en['editor.annotations.armed.point']).toBe( + 'Point tool on: tap or click the drawing where you want it, or choose “Place at the centre”, below the drawing (Enter on the drawing does the same).', + ) + expect(en['editor.annotations.armed.zone']).toBe( + 'Zone tool on: place the vertices on the drawing, or choose “Place at the centre”, below the drawing (Enter on the drawing does the same).', + ) + expect(en['editor.annotations.armed.detail_trigger']).toBe( + 'Link tool on: place the vertices on the drawing, or choose “Place at the centre”, below the drawing (Enter on the drawing does the same).', + ) + }) })