From 6df66f9bb9da6ef2c65223bfb34de12a5dc17cea Mon Sep 17 00:00:00 2001 From: bchanot Date: Thu, 19 Nov 2026 11:16:55 +0100 Subject: [PATCH] =?UTF-8?q?feat(editor):=20Backspace=20and=20=C2=AB=20+=20?= =?UTF-8?q?=C2=BB=20on=20a=20vertex=20handle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/app/e2e/shell/editor-zones.spec.ts | 45 +++++++++++++++++++ .../editor/annotations/ZoneHandles.tsx | 1 + .../app/src/editor/annotations/useZoneEdit.ts | 33 +++++++++----- .../unit/editor-creation-dialog.test.tsx | 15 +++++++ apps/app/tests/unit/editor-zone-edit.test.tsx | 24 ++++++++++ 5 files changed, 108 insertions(+), 10 deletions(-) diff --git a/apps/app/e2e/shell/editor-zones.spec.ts b/apps/app/e2e/shell/editor-zones.spec.ts index ce039fd1..db51a9fc 100644 --- a/apps/app/e2e/shell/editor-zones.spec.ts +++ b/apps/app/e2e/shell/editor-zones.spec.ts @@ -169,6 +169,51 @@ test.describe('editor zones on the drawing (U2 Task 5)', () => { await undoTo(page, () => shownPoints(page), stored) }) + test('Thoraciques: Backspace and « + » on a vertex, each undone, the group named in full', async ({ + page, + requests, + }) => { + const actor = await openThoracic(page) + requests.forbid(actor.account.email, actor.account.password) + await pickAnnotation(page, dict, THORACIC) + await scrollToTop(page) + // Point 5: the group and a vertex carry the annotation's full name. + await expect( + page.getByRole('group', { + name: 'Sommets de Vertèbres thoraciques', + exact: true, + }), + ).toBeAttached() + const stored = await shownPoints(page) + const third = handle(page, 'Sommet 3 de Vertèbres thoraciques') + await expect(third).toBeAttached() + + // Backspace removes the focused vertex, as Delete; undone. + await third.focus() + await page.keyboard.press('Backspace') + await expect(line(page)).toContainText( + dict['editor.annotations.message.vertex_removed'], + ) + expect(await count(page)).toBe(stored.length - 1) + await undoTo(page, () => shownPoints(page), stored) + + // « + » adds after the focused vertex, as Insert, the canvas zoom untouched; undone. + // The undo cleared the choice but left the focus: moving it chooses again. + await handle(page, 'Sommet 2 de Vertèbres thoraciques').focus() + await handle(page, 'Sommet 3 de Vertèbres thoraciques').focus() + const drawing = page.getByTestId('annotation-canvas-drawing').locator('..') + const framed = () => + drawing.evaluate((el) => getComputedStyle(el).transform) + const zoomed = await framed() + await page.keyboard.press('+') + await expect(line(page)).toContainText( + dict['editor.annotations.message.vertex_added'], + ) + expect(await count(page)).toBe(stored.length + 1) + expect(await framed()).toBe(zoomed) + await undoTo(page, () => count(page), stored.length) + }) + test('Thoraciques: « Désélectionner » commits the arrow steps pending on a vertex', async ({ page, requests, diff --git a/apps/app/src/components/editor/annotations/ZoneHandles.tsx b/apps/app/src/components/editor/annotations/ZoneHandles.tsx index 9b1a194f..fb159582 100644 --- a/apps/app/src/components/editor/annotations/ZoneHandles.tsx +++ b/apps/app/src/components/editor/annotations/ZoneHandles.tsx @@ -134,6 +134,7 @@ function Vertex({ tabIndex={0} aria-label={label} 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. data-series="" diff --git a/apps/app/src/editor/annotations/useZoneEdit.ts b/apps/app/src/editor/annotations/useZoneEdit.ts index dd0ddfda..31600ce7 100644 --- a/apps/app/src/editor/annotations/useZoneEdit.ts +++ b/apps/app/src/editor/annotations/useZoneEdit.ts @@ -113,9 +113,28 @@ function deleteChosen(input: ZoneEditInput): void { chooseVertex(id, Math.min(chosen, left)) } -/** A vertex handle's keys (board 32): arrows step it, Enter commits, Insert adds, Delete. */ -function vertexKey(input: ZoneEditInput, index: number, event: KeyboardEvent) { +const INSERT_KEYS = ['Insert', '+'] +const REMOVE_KEYS = ['Delete', 'Backspace'] + +/** + * Insert or « + » adds after the chosen vertex, Delete or Backspace removes it (Mac keyboards + * have no Insert and Delete is Backspace; « + » is the drawn « + », whatever the layout). + */ +function editKey(input: ZoneEditInput, event: KeyboardEvent): void { + event.preventDefault() + // Final review I-1: a move mode changes the position only, as the press machine does. + if (inMoveMode()) return + // I-1: arrow steps still pending are committed first; the change applies to that place. + commitKeys(input) const id = input.target?.id + const chosen = id ? freshChosen(id) : null + if (INSERT_KEYS.includes(event.key) && chosen !== null) + insertAfter(input, chosen) + if (REMOVE_KEYS.includes(event.key)) deleteChosen(input) +} + +/** A vertex handle's keys (board 32): arrows step it, Enter commits, the edit keys above. */ +function vertexKey(input: ZoneEditInput, index: number, event: KeyboardEvent) { if (STEP[event.key]) { event.preventDefault() return stepVertex(input, index, event) @@ -124,14 +143,8 @@ function vertexKey(input: ZoneEditInput, index: number, event: KeyboardEvent) { event.preventDefault() return inMoveMode() ? input.confirmMove() : commitKeys(input) } - if (event.key !== 'Insert' && event.key !== 'Delete') return - // Final review I-1: a move mode changes the position only, as the press machine does. - if (inMoveMode()) return - // I-1: arrow steps still pending are committed first; the change applies to that place. - commitKeys(input) - const chosen = id ? freshChosen(id) : null - if (event.key === 'Insert' && chosen !== null) insertAfter(input, chosen) - if (event.key === 'Delete') deleteChosen(input) + if (INSERT_KEYS.includes(event.key) || REMOVE_KEYS.includes(event.key)) + editKey(input, event) } /** Enter or Space on a « + » or on the « − » bubble is its click. */ diff --git a/apps/app/tests/unit/editor-creation-dialog.test.tsx b/apps/app/tests/unit/editor-creation-dialog.test.tsx index 51eb6817..b37b13c9 100644 --- a/apps/app/tests/unit/editor-creation-dialog.test.tsx +++ b/apps/app/tests/unit/editor-creation-dialog.test.tsx @@ -352,6 +352,21 @@ describe('« Reprendre le tracé » edits the closed shape with its handles (boa expect(untouched()).toEqual(before) }) + it('Backspace and « + » on a pending vertex act as Delete and Insert, nothing held', async () => { + await resumed() + const before = untouched() + const vertex = (n: number) => + screen.getByRole('button', { name: `Sommet ${n} de Tibia` }) + vertex(1).focus() + expect(fireEvent.keyDown(vertex(1), { key: '+' })).toBe(false) + expect(shape()).toHaveLength(5) + expect(shape()![1]).toEqual([50, 20]) + expect(useAnnotationWork.getState().chosen).toMatchObject({ vertex: 1 }) + expect(fireEvent.keyDown(vertex(2), { key: 'Backspace' })).toBe(false) + expect(shape()).toEqual(SQUARE) + expect(untouched()).toEqual(before) + }) + it('« − » at three vertices is refused with its reason', async () => { await resumed('Tibia', SQUARE.slice(0, 3)) clickHandle('Sommet 1 de Tibia', [20, 20]) diff --git a/apps/app/tests/unit/editor-zone-edit.test.tsx b/apps/app/tests/unit/editor-zone-edit.test.tsx index 91ed6351..f4b85adc 100644 --- a/apps/app/tests/unit/editor-zone-edit.test.tsx +++ b/apps/app/tests/unit/editor-zone-edit.test.tsx @@ -241,6 +241,30 @@ describe('a vertex (R-9)', () => { expect(message()).toHaveTextContent('Sommet retiré') }) + it('Backspace removes and « + » inserts like Delete and Insert, one commit each, the default prevented', async () => { + await mount(squareText()) + await select() + const before = depth() + vertex(1).focus() + expect(fireEvent.keyDown(vertex(1), { key: '+' })).toBe(false) + expect(pointsOf('z')).toHaveLength(5) + expect(chosen()).toMatchObject({ vertex: 1 }) + expect(fireEvent.keyDown(vertex(2), { key: 'Backspace' })).toBe(false) + expect(pointsOf('z')).toEqual(SQUARE) + expect(depth()).toBe(before + 2) + }) + + it('Backspace at three vertices keeps them with the reason; « + » needs no drawn « + »', async () => { + await mount(squareText(SQUARE.slice(0, 3))) + await select() + vertex(1).focus() + fireEvent.keyDown(vertex(1), { key: 'Backspace' }) + expect(pointsOf('z')).toHaveLength(3) + expect(message()).toHaveTextContent('Une zone garde au moins 3 sommets') + fireEvent.keyDown(vertex(1), { key: '+' }) + expect(pointsOf('z')).toHaveLength(4) + }) + it('« − » is disabled at 3 with its reason; nothing is removed', async () => { const triangle = SQUARE.slice(0, 3) await mount(squareText(triangle))