fix(editor): keyboard refused-commit rule, missing history tests

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.
This commit is contained in:
bchanot committed 2026-11-08 18:09:10 +01:00
1 parent 99b669bf8f
commit 89da8d3f55
4 files changed
+133 -35

No files matched your search

@@ -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<HTMLElement | null>(null)
const forget = () => {
left.current = null
}
const onPointerDown = () => {
left.current = leavingField()
left.current = asField(document.activeElement)
commitFocusedField()
}
const onFocus = (event: FocusEvent<HTMLElement>) => {
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 (
<button
type="button"
@@ -68,7 +81,7 @@ function HistoryButton({ direction }: { direction: Direction }) {
title={name}
aria-disabled={readOnly || !steppable}
aria-keyshortcuts={SHORTCUT[direction]}
{...usePress(direction, readOnly)}
{...press}
className={BUTTON}
>
<EditorGlyph name={direction} />
+34 -20
View File
@@ -19,7 +19,7 @@ function revisionText(t: Translate, key: string, revision: number): string {
return t(key).replace('{revision}', String(revision))
}
/** The line for a state; `withTime` false gives the form the status region announces. */
/** The line for a state; `time` is the server's save time already formatted, or null. */
function lineText(
t: Translate,
plural: ReturnType<typeof usePlural>,
@@ -51,14 +51,16 @@ function lineText(
/** What the status region announces: the state, never each change of the count. */
function announcement(t: Translate, state: SaveLineState): string {
switch (state.kind) {
case 'copy_issue':
return t(`editor.draft.status.copy_${state.issue}`)
case 'saving':
return t('editor.draft.status.saving')
case 'unsaved':
return t('editor.draft.status.unsaved')
case 'revision':
return ''
case 'saved':
return revisionText(t, 'editor.draft.status.saved', state.revision)
default:
return lineText(t, () => '', state, null)
case 'revision':
return ''
}
}
@@ -79,6 +81,27 @@ function useSaveLineState(): SaveLineState {
})
}
/** The longest forms, laid out invisibly so the line keeps its width. */
function useWidestForms(): string[] {
const t = useT()
const plural = usePlural()
const timed = revisionText(t, 'editor.draft.status.saved_at', WIDEST_REVISION)
return [
timed.replace('{time}', WIDEST_TIME),
plural('editor.draft.status.unsaved_count', 999),
]
}
/** The line's text for the store's state, the server time in Paris time. */
function useLineText(state: SaveLineState): string {
const t = useT()
const plural = usePlural()
const locale = useLocaleStore((s) => s.locale)
const savedAt = state.kind === 'saved' ? state.savedAt : null
const time = savedAt === null ? null : formatParisTime(savedAt, locale)
return lineText(t, plural, state, time)
}
/**
* The save line (board 04). The visible line is a plain paragraph; a hidden status region
* announces the state changes. The longest forms are laid out invisibly in the same cell,
@@ -86,31 +109,22 @@ function useSaveLineState(): SaveLineState {
*/
export default function SaveLine() {
const t = useT()
const plural = usePlural()
const locale = useLocaleStore((s) => s.locale)
const state = useSaveLineState()
const savedAt = state.kind === 'saved' ? state.savedAt : null
const time = savedAt === null ? null : formatParisTime(savedAt, locale)
const widest = [
revisionText(t, 'editor.draft.status.saved_at', WIDEST_REVISION).replace(
'{time}',
WIDEST_TIME,
),
plural('editor.draft.status.unsaved_count', 999),
]
const text = useLineText(state)
const widest = useWidestForms()
return (
<>
<p className="grid text-sm tabular-nums text-[var(--color-ink-muted)]">
<span data-testid="save-line" className="col-start-1 row-start-1">
{lineText(t, plural, state, time)}
{text}
</span>
{widest.map((text) => (
{widest.map((form) => (
<span
key={text}
key={form}
aria-hidden="true"
className="invisible col-start-1 row-start-1 h-0 overflow-hidden whitespace-nowrap"
>
{text}
{form}
</span>
))}
</p>