From 876960ccc7db150aef4ea9b2e7bbbbe85d4b54cc Mon Sep 17 00:00:00 2001 From: bchanot Date: Mon, 23 Nov 2026 13:30:45 +0100 Subject: [PATCH] fix(editor): an unstarted frame ends silently on Escape and leave --- .../src/components/editor/HistoryButtons.tsx | 4 +- .../src/editor/annotations/drawingGuard.ts | 4 +- apps/app/src/editor/annotations/workStore.ts | 4 +- apps/app/tests/unit/editor-escape.test.tsx | 53 ++++++++++++++++++- 4 files changed, 60 insertions(+), 5 deletions(-) diff --git a/apps/app/src/components/editor/HistoryButtons.tsx b/apps/app/src/components/editor/HistoryButtons.tsx index a1576cbf..fd0b7d09 100644 --- a/apps/app/src/components/editor/HistoryButtons.tsx +++ b/apps/app/src/components/editor/HistoryButtons.tsx @@ -44,8 +44,8 @@ function asField(element: Element | EventTarget | null): HTMLElement | null { } /** - * A12: both are inactive during a creation or an open frame (R-13); I-1: redo also while work it would drop - * is pending (a started mode, a keyboard series, a slider change). + * A12: both are inactive during a creation or an open frame (R-13); I-1: redo also while + * work it would drop is pending (a started mode, a keyboard series, a slider change). */ const inactive = ( direction: Direction, diff --git a/apps/app/src/editor/annotations/drawingGuard.ts b/apps/app/src/editor/annotations/drawingGuard.ts index 26954d26..5c8cddd5 100644 --- a/apps/app/src/editor/annotations/drawingGuard.ts +++ b/apps/app/src/editor/annotations/drawingGuard.ts @@ -46,7 +46,7 @@ function endModeOnly(): void { /** * The drawing guard (R-12), asked by every navigation that would drop the drawing's work: * a tab, a section, « Aller à », a view chip, a tool, Back, leaving the editor. Nothing - * started: an unstarted mode ends at once and the caller goes on (true). Work started: + * started: an unstarted mode or frame ends at once and the caller goes on (true). Work started: * the question is shown (`DrawingGuardDialog`) and `proceed` waits for « Abandonner » * (false). */ @@ -56,6 +56,8 @@ export function guardDrawing(proceed: () => void): boolean { if (work === null) { commitFocusedSeries() endModeOnly() + // Like an unstarted mode, a frame with nothing drawn or changed loses nothing. + useAnnotationWork.setState({ frame: null }) return true } // A question already open keeps its own move (a Back over it is not replayed). diff --git a/apps/app/src/editor/annotations/workStore.ts b/apps/app/src/editor/annotations/workStore.ts index 637a7194..7c19ba78 100644 --- a/apps/app/src/editor/annotations/workStore.ts +++ b/apps/app/src/editor/annotations/workStore.ts @@ -130,7 +130,9 @@ export const useAnnotationWork = create(() => NO_WORK) export type HistoryHold = 'creation' | 'creation_redo' | 'frame' | 'pending' const sameBox = (a: Frame | null, b: Frame | null) => - JSON.stringify(a) === JSON.stringify(b) + a === null || b === null + ? a === b + : a.x === b.x && a.y === b.y && a.w === b.w && a.h === b.h /** A frame asks only once a box is drawn (create) or differs from its start (recrop). */ const frameChanged = (frame: FrameWork | null) => diff --git a/apps/app/tests/unit/editor-escape.test.tsx b/apps/app/tests/unit/editor-escape.test.tsx index f262335e..f7485163 100644 --- a/apps/app/tests/unit/editor-escape.test.tsx +++ b/apps/app/tests/unit/editor-escape.test.tsx @@ -15,7 +15,7 @@ import ModalDialog from '@/components/ui/ModalDialog' import DrawingGuardDialog from '@/components/editor/annotations/DrawingGuardDialog' import { guardDrawing } from '@/editor/annotations/drawingGuard' import { setFrameBox, startFrame } from '@/editor/annotations/frameWork' -import { resetWork } from '@/editor/annotations/workStore' +import { resetWork, useAnnotationWork } from '@/editor/annotations/workStore' import { useLocaleStore } from '@/state/localeStore' import { drawnText, edit, insert, point } from '../helpers/editorFixtures' import { pressAt, resetStore, serverHas, store } from '../helpers/editorHarness' @@ -416,3 +416,54 @@ describe('the frame guard dialog titles', () => { expect(await screen.findByRole('dialog', { name: title })).toBeVisible() }) }) + +describe('Escape over a frame', () => { + const open = () => + act(() => { + startFrame({ + purpose: 'create', + viewId: 'v', + drawnOn: 'v', + box: null, + returnTo: 'v', + }) + }) + afterEach(() => act(() => resetWork())) + + it('an unstarted frame ends silently: no dialog, undo no longer held', async () => { + const user = await mount() + await select(user) + open() + expect(useAnnotationWork.getState().frame).not.toBeNull() + canvasBox().focus() + escape(canvasBox()) + expect(useAnnotationWork.getState().frame).toBeNull() + expect(screen.queryByRole('dialog')).toBeNull() + expect( + screen.getByRole('button', { name: 'Défaire la dernière modification' }), + ).not.toHaveAttribute('aria-describedby') + }) + + it('a started frame asks; « Abandonner » drops it', async () => { + const user = await mount() + await select(user) + open() + act(() => setFrameBox({ x: 1, y: 1, w: 9, h: 9 })) + canvasBox().focus() + escape(canvasBox()) + const dialog = await screen.findByRole('dialog', { + name: 'Abandonner la création ?', + }) + expect(useAnnotationWork.getState().frame).not.toBeNull() + await user.click(within(dialog).getByRole('button', { name: 'Abandonner' })) + expect(useAnnotationWork.getState().frame).toBeNull() + }) + + it('a navigation guard passes over an unstarted frame and clears it', () => { + open() + const proceed = vi.fn() + expect(guardDrawing(proceed)).toBe(true) + expect(useAnnotationWork.getState().frame).toBeNull() + expect(useAnnotationWork.getState().question).toBeNull() + }) +})