fix(editor): access target cleared by undo and redo only

This commit is contained in:
bchanot committed 2026-11-23 14:03:41 +01:00
1 parent 09f0187181
commit 0fbdbce0fd
6 files changed
+26 -21

No files matched your search

@@ -2,18 +2,13 @@ import { useEffect, type Dispatch } from 'react'
import { useEditorStore } from '../state/editorStore' import { useEditorStore } from '../state/editorStore'
import type { UiAction } from './uiState' import type { UiAction } from './uiState'
/** /** The preset « Vue ouverte » ends with an undo or a redo, never with another edit. */
* 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.
*/
export function useAccessTargetClear(dispatch: Dispatch<UiAction>): void { export function useAccessTargetClear(dispatch: Dispatch<UiAction>): void {
useEffect( useEffect(
() => () =>
useEditorStore.subscribe((state, before) => { useEditorStore.subscribe((state, before) => {
const stepped = if (state.historySteps !== before.historySteps)
state.undo.length < before.undo.length || dispatch({ type: 'access_target', viewId: null })
state.redo.length < before.redo.length
if (stepped) dispatch({ type: 'access_target', viewId: null })
}), }),
[dispatch], [dispatch],
) )
@@ -1,8 +1,8 @@
import { useEffect, useRef, type PointerEvent, type RefObject } from 'react' import { useEffect, useRef, type PointerEvent, type RefObject } from 'react'
import type { Circle, Polygon } from '@faunosteo/core-wasm/types' import type { Circle, Polygon } from '@faunosteo/core-wasm/types'
import { import {
defaultAccess,
isDrawn, isDrawn,
rectanglePolygon,
type AccessShape, type AccessShape,
type DrawnShape, type DrawnShape,
} from './accessShape' } from './accessShape'
@@ -34,11 +34,10 @@ import { annotationIdOf } from './useCanvasBuild'
import { useBlockDrag, type BlockDragInput } from './useBlockDrag' import { useBlockDrag, type BlockDragInput } from './useBlockDrag'
import { commitFocusedSeries } from './drawingGuard' import { commitFocusedSeries } from './drawingGuard'
import { import {
defaultCircleAt,
defaultAt,
framePhase, framePhase,
parentFrame, parentFrame,
placeDefault, placeDefault,
visibleDrawing,
} from './frameDraw' } from './frameDraw'
import { import {
useFrameDrag, useFrameDrag,
@@ -247,11 +246,8 @@ function accessClick(
const at = clientToDrawing(input.overlay.current, clientOf(event)) const at = clientToDrawing(input.overlay.current, clientOf(event))
const bounds = parentFrame(input.overlay.current) const bounds = parentFrame(input.overlay.current)
if (!at || !bounds) return if (!at || !bounds) return
input.onShape( const visible = visibleDrawing(input, bounds)
shape === 'circle' input.onShape(defaultAccess(shape, at, visible, bounds))
? defaultCircleAt(input, at, bounds)
: rectanglePolygon(defaultAt(input, at, bounds)),
)
} }
/** A click with the select tool (none during the move mode, R-12), or with a drawing tool. */ /** A click with the select tool (none during the move mode, R-12), or with a drawing tool. */
+1
View File
@@ -23,6 +23,7 @@ export function initialData(): EditorData {
drawings: [], drawings: [],
drawingFailures: {}, drawingFailures: {},
...emptyHistory(), ...emptyHistory(),
historySteps: 0,
saving: false, saving: false,
submitting: false, submitting: false,
reloading: false, reloading: false,
+2 -1
View File
@@ -94,7 +94,8 @@ export function stepHistory(
const replaced = preview.replaceDocument(step.text) const replaced = preview.replaceDocument(step.text)
if (!replaced.ok) return replaced if (!replaced.ok) return replaced
const patch = replacedPatch(state, step.text, replaced.state, step.history) 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 } return { ok: true }
} }
+2
View File
@@ -136,6 +136,8 @@ export interface EditorData {
drawingFailures: Record<string, CatalogueFailure> drawingFailures: Record<string, CatalogueFailure>
undo: HistoryEntry[] undo: HistoryEntry[]
redo: HistoryEntry[] redo: HistoryEntry[]
/** Counts the undo and redo steps taken, so a listener tells them from an edit. */
historySteps: number
saving: boolean saving: boolean
submitting: boolean submitting: boolean
reloading: boolean reloading: boolean
@@ -279,7 +279,7 @@ describe('placing and the form (A4, A5)', () => {
expect(place([])).toBe('') 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 user = await mount(withDetail())
const dispatch = vi.fn() const dispatch = vi.fn()
renderHook(() => useAccessTargetClear(dispatch)) 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 user.click(within(shown).getByRole('button', { name: 'Créer' }))
await waitFor(() => expect(creation()).toBeNull()) await waitFor(() => expect(creation()).toBeNull())
expect(dispatch).not.toHaveBeenCalled() expect(dispatch).not.toHaveBeenCalled()
const cleared = { type: 'access_target', viewId: null }
act(() => store.getState().undoStep()) act(() => store.getState().undoStep())
expect(dispatch).toHaveBeenCalledWith({ expect(dispatch).toHaveBeenCalledTimes(1)
type: 'access_target', expect(dispatch).toHaveBeenLastCalledWith(cleared)
viewId: null, // 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() dispatch.mockClear()
act(() => store.getState().redoStep()) act(() => store.getState().redoStep())
expect(dispatch).toHaveBeenCalledTimes(1) expect(dispatch).toHaveBeenCalledTimes(1)
expect(dispatch).toHaveBeenLastCalledWith(cleared)
}) })
it('the committed access lands in the internal category', async () => { it('the committed access lands in the internal category', async () => {