From 89da8d3f5580bfe404ec2c033b423f7b48de6545 Mon Sep 17 00:00:00 2001 From: bchanot Date: Sun, 8 Nov 2026 18:09:10 +0100 Subject: [PATCH] fix(editor): keyboard refused-commit rule, missing history tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Keyboard activation after a refused blur commit skips the undo and refocuses the field. Adds restore, Général-from-Annotations and DeleteDialog tests; SaveLine helpers. --- .../src/components/editor/HistoryButtons.tsx | 43 +++++++++------ apps/app/src/components/editor/SaveLine.tsx | 54 ++++++++++++------- .../unit/editor-history-buttons.test.tsx | 52 ++++++++++++++++++ apps/app/tests/unit/editor-save-line.test.ts | 19 +++++++ 4 files changed, 133 insertions(+), 35 deletions(-) diff --git a/apps/app/src/components/editor/HistoryButtons.tsx b/apps/app/src/components/editor/HistoryButtons.tsx index 135f985b..c364cd26 100644 --- a/apps/app/src/components/editor/HistoryButtons.tsx +++ b/apps/app/src/components/editor/HistoryButtons.tsx @@ -1,4 +1,4 @@ -import { useRef } from 'react' +import { useRef, type FocusEvent } from 'react' import { useEditorReadOnly } from '@/editor/editingContext' import { commitFocusedField } from '@/editor/focusRequest' import { canRedo, canUndo, useEditorStore } from '@/editor/state/editorStore' @@ -19,30 +19,36 @@ const BUTTON = 'xl:min-h-9 xl:min-w-0 focus-visible:outline-none focus-visible:ring-2 ' + 'focus-visible:ring-[var(--color-accent)]' -/** The text field the press left, kept to learn whether its commit was refused. */ -function leavingField(): HTMLElement | null { - const active = document.activeElement +/** The element when it is a text field of the editor, else null. */ +function asField(element: Element | EventTarget | null): HTMLElement | null { const isField = - active instanceof HTMLInputElement || - active instanceof HTMLTextAreaElement || - active instanceof HTMLSelectElement - return isField ? active : null + element instanceof HTMLInputElement || + element instanceof HTMLTextAreaElement || + element instanceof HTMLSelectElement + return isField ? element : null } /** - * The press, in two steps: the pointer-down commits the typed text (the field's blur), the - * click 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, focus goes back. + * 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. */ function usePress(direction: Direction, readOnly: boolean) { const left = useRef(null) + const forget = () => { + left.current = null + } const onPointerDown = () => { - left.current = leavingField() + left.current = asField(document.activeElement) commitFocusedField() } + const onFocus = (event: FocusEvent) => { + left.current = asField(event.relatedTarget) ?? left.current + } const onClick = () => { const field = left.current - left.current = null + forget() if (field?.getAttribute('aria-invalid') === 'true') { field.focus() return @@ -53,7 +59,13 @@ function usePress(direction: Direction, readOnly: boolean) { if (direction === 'undo') store.undoStep() else store.redoStep() } - return { onPointerDown, onClick, onPointerLeave: () => (left.current = null) } + return { + onPointerDown, + onFocus, + onBlur: forget, + onPointerLeave: forget, + onClick, + } } function HistoryButton({ direction }: { direction: Direction }) { @@ -61,6 +73,7 @@ function HistoryButton({ direction }: { direction: Direction }) { const readOnly = useEditorReadOnly() const steppable = useEditorStore(direction === 'undo' ? canUndo : canRedo) const name = t(`editor.history.${direction}_title`) + const press = usePress(direction, readOnly) return (