fix(editor): redo and move-mode handle keys keep pending work

Final review I-1. Redo (Ctrl+Shift+Z and the header « Refaire ») is
inactive while a started move or redraw, a keyboard series or a size
change is pending (holdsRedo): the work stays, the user commits or
cancels it first. In a move mode, Insert and Delete on a vertex and
Enter or Space on « + » and « − » do nothing, as the press machine.

Tests: no assertion removed. The history-guards move-mode helper and
reset run in act, since « Refaire » now re-renders on a started mode.
This commit is contained in:
bchanot committed 2026-11-18 13:00:03 +01:00
1 parent c6b8c7e48a
commit ae80511964
7 files changed
+211 -12

No files matched your search

@@ -9,6 +9,7 @@ import { commitFocusedField } from '@/editor/focusRequest'
import { canRedo, canUndo, useEditorStore } from '@/editor/state/editorStore'
import {
cancelPending,
holdsRedo,
useAnnotationWork,
} from '@/editor/annotations/workStore'
import { useT } from '@/i18n/useT'
@@ -37,6 +38,15 @@ function asField(element: Element | EventTarget | null): HTMLElement | null {
return isField ? element : null
}
/**
* A12: both are inactive during a creation (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,
work: ReturnType<typeof useAnnotationWork.getState>,
) => work.creation !== null || (direction === 'redo' && holdsRedo(work))
/**
* The press, in two steps: leaving a field commits the typed text (its blur: forced at
* pointer-down, natural when Tab reaches the button), the click or key then reads the
@@ -71,7 +81,7 @@ function usePress(
}
const onClick = () => {
// A12: undo and redo are inactive during a creation (R-13).
if (useAnnotationWork.getState().creation) return
if (inactive(direction, useAnnotationWork.getState())) return
const field = left.current
forget()
if (field?.isConnected && field.getAttribute('aria-invalid') === 'true') {
@@ -106,7 +116,7 @@ function HistoryButton({
const t = useT()
const readOnly = useEditorReadOnly()
const steppable = useEditorStore(direction === 'undo' ? canUndo : canRedo)
const creating = useAnnotationWork((s) => s.creation !== null)
const held = useAnnotationWork((s) => inactive(direction, s))
const name = t(`editor.history.${direction}_title`)
const press = usePress(direction, readOnly, left)
return (
@@ -114,7 +124,7 @@ function HistoryButton({
type="button"
aria-label={name}
title={`${name} (${t(`editor.history.${direction}_shortcut`)})`}
aria-disabled={readOnly || !steppable || creating}
aria-disabled={readOnly || !steppable || held}
aria-keyshortcuts={SHORTCUT[direction]}
{...press}
className={BUTTON}
@@ -104,6 +104,8 @@ function vertexKey(input: ZoneEditInput, index: number, event: KeyboardEvent) {
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
@@ -139,16 +141,17 @@ export function useZoneEdit(input: ZoneEditInput) {
},
// BDR-050: leaving the vertex commits its series; a move mode waits for « Valider ».
onVertexBlur: () => commitKeys(input),
// Final review I-1: in a move mode « + » and « − » do nothing (no click there either).
onInsertKey: (after: number, event: KeyboardEvent) => {
if (!isPress(event)) return
event.preventDefault()
insertAfter(input, after)
if (!inMoveMode()) insertAfter(input, after)
},
// The bubble's own index is the chosen vertex (it is drawn only over that one).
onRemoveKey: (_index: number, event: KeyboardEvent) => {
if (!isPress(event)) return
event.preventDefault()
deleteChosen(input)
if (!inMoveMode()) deleteChosen(input)
},
}
}
@@ -155,6 +155,16 @@ export function cancelPending(): boolean {
return true
}
/**
* Work a redo would drop without a word (final review I-1): a started mode, a keyboard
* series or a slider change. Redo waits until it is committed or cancelled; an unstarted
* mode loses nothing and does not hold it.
*/
export function holdsRedo(work: Pick<Work, 'mode' | 'preview'>): boolean {
if (work.mode?.started) return true
return work.preview !== null && work.preview.source !== 'move'
}
const fresh = <T extends { base: string }>(item: T | null, text: string) =>
item && item.base !== text ? null : item
+9 -2
View File
@@ -1,5 +1,9 @@
import { useEffect } from 'react'
import { cancelPending, useAnnotationWork } from './annotations/workStore'
import {
cancelPending,
holdsRedo,
useAnnotationWork,
} from './annotations/workStore'
import { isTextField } from './focusRequest'
import { useEditorStore } from './state/editorStore'
@@ -23,7 +27,10 @@ export function useUndoShortcuts(writable: boolean): void {
return
event.preventDefault()
const store = useEditorStore.getState()
if (event.shiftKey) store.redoStep()
// I-1: a redo would drop pending work without a word; it waits for it.
if (event.shiftKey) {
if (!holdsRedo(useAnnotationWork.getState())) store.redoStep()
}
// R-13: a pending preview or mode is only cancelled; the next undo undoes.
else if (!cancelPending()) store.undoStep()
}