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 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<UiAction>): 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],
)
@@ -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. */
+1
View File
@@ -23,6 +23,7 @@ export function initialData(): EditorData {
drawings: [],
drawingFailures: {},
...emptyHistory(),
historySteps: 0,
saving: false,
submitting: false,
reloading: false,
+2 -1
View File
@@ -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 }
}
+2
View File
@@ -136,6 +136,8 @@ export interface EditorData {
drawingFailures: Record<string, CatalogueFailure>
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
@@ -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 () => {