diff --git a/apps/app/src/editor/annotations/useAccessTargetClear.ts b/apps/app/src/editor/annotations/useAccessTargetClear.ts index 97b93d85..cf68e13b 100644 --- a/apps/app/src/editor/annotations/useAccessTargetClear.ts +++ b/apps/app/src/editor/annotations/useAccessTargetClear.ts @@ -2,18 +2,13 @@ import { useEffect, type Dispatch } from 'react' import { useEditorStore } from '../state/editorStore' import type { UiAction } from './uiState' -/** - * The preset « Vue ouverte » ends with an undo or a redo: either one takes an entry off a - * history stack, which a commit (one more undo entry) never does while a redo stays empty. - */ +/** The preset « Vue ouverte » ends with an undo or a redo, never with another edit. */ export function useAccessTargetClear(dispatch: Dispatch): void { useEffect( () => useEditorStore.subscribe((state, before) => { - const stepped = - state.undo.length < before.undo.length || - state.redo.length < before.redo.length - if (stepped) dispatch({ type: 'access_target', viewId: null }) + if (state.historySteps !== before.historySteps) + dispatch({ type: 'access_target', viewId: null }) }), [dispatch], ) diff --git a/apps/app/src/editor/annotations/usePressGesture.ts b/apps/app/src/editor/annotations/usePressGesture.ts index bb57b885..4a75af61 100644 --- a/apps/app/src/editor/annotations/usePressGesture.ts +++ b/apps/app/src/editor/annotations/usePressGesture.ts @@ -1,8 +1,8 @@ import { useEffect, useRef, type PointerEvent, type RefObject } from 'react' import type { Circle, Polygon } from '@faunosteo/core-wasm/types' import { + defaultAccess, isDrawn, - rectanglePolygon, type AccessShape, type DrawnShape, } from './accessShape' @@ -34,11 +34,10 @@ import { annotationIdOf } from './useCanvasBuild' import { useBlockDrag, type BlockDragInput } from './useBlockDrag' import { commitFocusedSeries } from './drawingGuard' import { - defaultCircleAt, - defaultAt, framePhase, parentFrame, placeDefault, + visibleDrawing, } from './frameDraw' import { useFrameDrag, @@ -247,11 +246,8 @@ function accessClick( const at = clientToDrawing(input.overlay.current, clientOf(event)) const bounds = parentFrame(input.overlay.current) if (!at || !bounds) return - input.onShape( - shape === 'circle' - ? defaultCircleAt(input, at, bounds) - : rectanglePolygon(defaultAt(input, at, bounds)), - ) + const visible = visibleDrawing(input, bounds) + input.onShape(defaultAccess(shape, at, visible, bounds)) } /** A click with the select tool (none during the move mode, R-12), or with a drawing tool. */ diff --git a/apps/app/src/editor/state/editorBase.ts b/apps/app/src/editor/state/editorBase.ts index 3374198e..09515708 100644 --- a/apps/app/src/editor/state/editorBase.ts +++ b/apps/app/src/editor/state/editorBase.ts @@ -23,6 +23,7 @@ export function initialData(): EditorData { drawings: [], drawingFailures: {}, ...emptyHistory(), + historySteps: 0, saving: false, submitting: false, reloading: false, diff --git a/apps/app/src/editor/state/editorEdit.ts b/apps/app/src/editor/state/editorEdit.ts index d3b8f229..b43f702c 100644 --- a/apps/app/src/editor/state/editorEdit.ts +++ b/apps/app/src/editor/state/editorEdit.ts @@ -94,7 +94,8 @@ export function stepHistory( const replaced = preview.replaceDocument(step.text) if (!replaced.ok) return replaced const patch = replacedPatch(state, step.text, replaced.state, step.history) - api.set(withReveal(patch, state.documentText, step.text)) + const revealed = withReveal(patch, state.documentText, step.text) + api.set({ ...revealed, historySteps: state.historySteps + 1 }) return { ok: true } } diff --git a/apps/app/src/editor/state/editorTypes.ts b/apps/app/src/editor/state/editorTypes.ts index 52beb2f1..2110163d 100644 --- a/apps/app/src/editor/state/editorTypes.ts +++ b/apps/app/src/editor/state/editorTypes.ts @@ -136,6 +136,8 @@ export interface EditorData { drawingFailures: Record undo: HistoryEntry[] redo: HistoryEntry[] + /** Counts the undo and redo steps taken, so a listener tells them from an edit. */ + historySteps: number saving: boolean submitting: boolean reloading: boolean diff --git a/apps/app/tests/unit/editor-creation-dialog.test.tsx b/apps/app/tests/unit/editor-creation-dialog.test.tsx index ec442ac0..d2b11a07 100644 --- a/apps/app/tests/unit/editor-creation-dialog.test.tsx +++ b/apps/app/tests/unit/editor-creation-dialog.test.tsx @@ -279,7 +279,7 @@ describe('placing and the form (A4, A5)', () => { expect(place([])).toBe('') }) - it('an undo or a redo clears the access target, a commit does not', async () => { + it('an undo or a redo clears the access target, an unrelated edit does not', async () => { const user = await mount(withDetail()) const dispatch = vi.fn() renderHook(() => useAccessTargetClear(dispatch)) @@ -296,14 +296,24 @@ describe('placing and the form (A4, A5)', () => { await user.click(within(shown).getByRole('button', { name: 'Créer' })) await waitFor(() => expect(creation()).toBeNull()) expect(dispatch).not.toHaveBeenCalled() + const cleared = { type: 'access_target', viewId: null } act(() => store.getState().undoStep()) - expect(dispatch).toHaveBeenCalledWith({ - type: 'access_target', - viewId: null, + expect(dispatch).toHaveBeenCalledTimes(1) + expect(dispatch).toHaveBeenLastCalledWith(cleared) + // An edit while the redo stack is not empty drops it: it is no redo. + dispatch.mockClear() + expect(store.getState().redo.length).toBeGreaterThan(0) + act(() => { + store + .getState() + .commit({ op: 'remove', key: { kind: 'annotation', id: 'anno-a' } }) }) + expect(dispatch).not.toHaveBeenCalled() + act(() => store.getState().undoStep()) dispatch.mockClear() act(() => store.getState().redoStep()) expect(dispatch).toHaveBeenCalledTimes(1) + expect(dispatch).toHaveBeenLastCalledWith(cleared) }) it('the committed access lands in the internal category', async () => {