diff --git a/apps/app/src/components/editor/HistoryButtons.tsx b/apps/app/src/components/editor/HistoryButtons.tsx index c364cd26..ea1946b1 100644 --- a/apps/app/src/components/editor/HistoryButtons.tsx +++ b/apps/app/src/components/editor/HistoryButtons.tsx @@ -1,4 +1,4 @@ -import { useRef, type FocusEvent } from 'react' +import { useRef, type FocusEvent, type RefObject } from 'react' import { useEditorReadOnly } from '@/editor/editingContext' import { commitFocusedField } from '@/editor/focusRequest' import { canRedo, canUndo, useEditorStore } from '@/editor/state/editorStore' @@ -32,13 +32,22 @@ function asField(element: Element | EventTarget | null): HTMLElement | null { * 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 * store, so the typed text is the last change the undo reverts. A field whose commit was - * refused keeps its error: the step is skipped and focus goes back to it. + * refused keeps its error: the step is skipped and focus goes back to it. The field is + * remembered across the pair: from a field, « Annuler » is two Shift+Tab away. */ -function usePress(direction: Direction, readOnly: boolean) { - const left = useRef(null) +function usePress( + direction: Direction, + readOnly: boolean, + left: RefObject, +) { const forget = () => { left.current = null } + // Tab from « Rétablir » to « Annuler » (or back) is still the same press of the pair. + const onBlur = (event: FocusEvent) => { + const pair = event.currentTarget.parentElement + if (!pair?.contains(event.relatedTarget as Node | null)) forget() + } const onPointerDown = () => { left.current = asField(document.activeElement) commitFocusedField() @@ -62,18 +71,24 @@ function usePress(direction: Direction, readOnly: boolean) { return { onPointerDown, onFocus, - onBlur: forget, + onBlur, onPointerLeave: forget, onClick, } } -function HistoryButton({ direction }: { direction: Direction }) { +function HistoryButton({ + direction, + left, +}: { + direction: Direction + left: RefObject +}) { const t = useT() const readOnly = useEditorReadOnly() const steppable = useEditorStore(direction === 'undo' ? canUndo : canRedo) const name = t(`editor.history.${direction}_title`) - const press = usePress(direction, readOnly) + const press = usePress(direction, readOnly, left) return (