From 0bb150f860341c111cb78079f59b156ff1a192cd Mon Sep 17 00:00:00 2001 From: bchanot Date: Sun, 8 Nov 2026 20:23:40 +0100 Subject: [PATCH] fix(app): focus the shown tab after a crossing, one commit per typed field - A field blurred by a 1 024 px crossing or a read-only switch hands its focus to the shown tab (or Plus), never to body (focusRequest, useViewport). - TextField no longer commits a blurred value twice when it unmounts before the render that brings the new value: one undo step, one count. - Annuler and Retablir share the field left by Tab, so Shift+Tab to Annuler and Enter still skips the undo of a refused field. - Adding a reference clears the finding filter and the search. Unit tests: no-loss (focus on tab and on Plus, one undo entry), history buttons (field kept across the pair), references panel (add clears, refused add keeps). --- .../src/components/editor/HistoryButtons.tsx | 34 ++++++++++++++----- .../components/editor/fields/TextField.tsx | 5 ++- .../editor/references/ReferenceForm.tsx | 11 ++++-- .../editor/references/ReferencesPanel.tsx | 7 +++- apps/app/src/editor/focusRequest.ts | 34 ++++++++++++------- apps/app/src/editor/useViewport.ts | 15 ++++++-- .../unit/editor-history-buttons.test.tsx | 20 +++++++++++ apps/app/tests/unit/editor-no-loss.test.tsx | 18 ++++++++++ .../unit/editor-references-panel.test.tsx | 34 +++++++++++++++++++ 9 files changed, 149 insertions(+), 29 deletions(-) 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 (