fix(editor): final review fixes (touch undo guard, focus, save line)

Important 1: a touch press fires pointer leave between up and click; only a
mouse leave (or a pointer cancel) forgets the refused field, so Annuler no
longer undoes an unrelated change. Important 2: the LAN spec reads the
revision before the attach, through shownRevision, which throws instead of NaN
(spec stays not run). Minor 1: refocusIfHidden refocuses a still shown element
the commit blurred (references search on a 1 024 px crossing). Minor 2:
discardChanges keeps lastSave (reload path still drops it). Minor 3: the
overview filter term lives in UiState / the read-only twin, not the card.
Minor 4: categories section drops its inner h3 and the unused
editor.general.categories keys. AC8: badge test.

Assertions rewritten or added (R-24):
- editor-tabs-general « Général no longer holds the categories editor »:
  queryByText of the deleted heading (now vacuous) -> queryByLabelText of the
  category name fields.
- editor-focus-request « Annotations: selects the annotation »: findAllByText
  /anno-b|Sélection/ -> the properties panel holds Centre x = 20 (anno-b) and
  owns the focused heading.
- editor-shell-layout panel walk: added after.at(-1) === 'side'.
- editor-recovery title « Annuler les modifications » -> « Abandonner ».
- new: touch/cancel/mouse press, references search crossing, discard keeps
  lastSave, filter kept, one categories heading, badge 1/2/0, shownRevision.
This commit is contained in:
bchanot committed 2026-11-09 12:19:32 +01:00
1 parent 942d52a835
commit 0467885527
22 files changed
+327 -38

No files matched your search

@@ -1,4 +1,9 @@
import { useRef, type FocusEvent, type RefObject } from 'react'
import {
useRef,
type FocusEvent,
type PointerEvent,
type RefObject,
} from 'react'
import { useEditorReadOnly } from '@/editor/editingContext'
import { commitFocusedField } from '@/editor/focusRequest'
import { canRedo, canUndo, useEditorStore } from '@/editor/state/editorStore'
@@ -33,7 +38,9 @@ function asField(element: Element | EventTarget | null): HTMLElement | null {
* 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. The field is
* remembered across the pair: from a field, « Annuler » is two Shift+Tab away.
* remembered across the pair: from a field, « Annuler » is two Shift+Tab away. A touch
* press fires pointer leave between its up and its click, so only a mouse leaving the
* button forgets the field (a hovering pointer that left meant no press), or a cancel.
*/
function usePress(
direction: Direction,
@@ -48,6 +55,9 @@ function usePress(
const pair = event.currentTarget.parentElement
if (!pair?.contains(event.relatedTarget as Node | null)) forget()
}
const onPointerLeave = (event: PointerEvent<HTMLElement>) => {
if (event.pointerType === 'mouse') forget()
}
const onPointerDown = () => {
left.current = asField(document.activeElement) ?? left.current
commitFocusedField()
@@ -72,7 +82,8 @@ function usePress(
onPointerDown,
onFocus,
onBlur,
onPointerLeave: forget,
onPointerLeave,
onPointerCancel: forget,
onClick,
}
}
@@ -274,6 +274,8 @@ function SidePanel({
doc={doc}
viewId={scene.viewId}
readOnly={false}
filter={ui.filter}
onFilter={(term) => dispatch({ type: 'filter', term })}
onSelectView={(id) => dispatch({ type: 'view', viewId: id })}
onSelectAnnotation={(id) => dispatch({ type: 'select', id })}
onOpenSection={scene.onOpenSection}
@@ -1,4 +1,4 @@
import { useMemo, useState } from 'react'
import { useMemo } from 'react'
import type {
AnatomicalSheetDocument,
SheetView,
@@ -29,6 +29,9 @@ export interface OverviewPanelProps {
readOnly: boolean
/** Put before every id (the read-only twin beside the kept edit panel). */
idPrefix?: string
/** The « Filtrer par nom » term, held by the parent so a selection does not lose it. */
filter: string
onFilter: (term: string) => void
onSelectView: (viewId: string) => void
onSelectAnnotation: (annotationId: string) => void
onOpenSection: (section: AnnotationSection, opener: HTMLElement) => void
@@ -193,8 +196,7 @@ function CategoriesCard(props: OverviewPanelProps) {
function AnnotationsCard(props: OverviewPanelProps) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const { doc, viewId, onSelectAnnotation } = props
const [term, setTerm] = useState('')
const { doc, viewId, filter: term, onFilter, onSelectAnnotation } = props
const prefix = props.idPrefix ?? ''
const total = useOverviewCounts(doc).annotationsByView.get(viewId) ?? 0
const groups = useMemo(
@@ -221,7 +223,7 @@ function AnnotationsCard(props: OverviewPanelProps) {
aria-label={t('editor.overview.filter')}
placeholder={t('editor.overview.filter')}
value={term}
onChange={(event) => setTerm(event.target.value)}
onChange={(event) => onFilter(event.target.value)}
/>
{groups.length > 0 ? (
<AnnotationList
@@ -20,7 +20,7 @@ import { useLocaleStore } from '@/state/localeStore'
const CARD = 'rounded-lg border border-[var(--color-border)] p-3'
/** Annotation categories: list, rename, colour, add, delete with dependents. */
/** Annotation categories: list, rename, colour, add, delete; the frame names the section. */
export default function CategoriesSection({
doc,
readOnly,
@@ -30,15 +30,11 @@ export default function CategoriesSection({
readOnly: boolean
focus?: FocusRequest
}) {
const t = useT()
useRowFocus(focus)
const locale = useLocaleStore((s) => s.locale)
const [deleting, setDeleting] = useState<DeleteTarget | null>(null)
return (
<section aria-labelledby="general-categories" className="space-y-3">
<h3 id="general-categories" className="text-base font-semibold">
{t('editor.general.categories')}
</h3>
<div data-testid="editor-section-categories" className="space-y-3">
<ul className="space-y-3">
{doc.categories.map((category) => (
<CategoryRow
@@ -58,7 +54,7 @@ export default function CategoriesSection({
<CategoryForm />
</EditOnly>
<DeleteDialog target={deleting} onClose={() => setDeleting(null)} />
</section>
</div>
)
}
@@ -149,6 +149,7 @@ function ReadOnlyAnnotations({
const handleVersion = useEditorStore((s) => s.handleVersion)
const [viewId, setViewId] = useState(mainViewId(doc))
const [selectedId, setSelectedId] = useState<string | null>(null)
const [filter, setFilter] = useState('')
useViewFallback(doc, viewId, setViewId, setSelectedId)
useAnnotationRequest(focus, doc, {
viewId,
@@ -162,6 +163,12 @@ function ReadOnlyAnnotations({
const model = useViewModel(doc, viewId)
const view = doc.views.find((v) => v.id === viewId)
const selected = model.annotations.find((a) => a.id === selectedId)
// A view of its own has its own list: the filter and the selection start over.
const showView = (id: string) => {
setViewId(id)
setSelectedId(null)
setFilter('')
}
if (!preview || !view) return null
return (
<div className="grid gap-4 lg:grid-cols-[minmax(0,1fr)_22rem]">
@@ -169,10 +176,7 @@ function ReadOnlyAnnotations({
<ViewSwitcher
views={doc.views}
current={viewId}
onSelect={(id) => {
setViewId(id)
setSelectedId(null)
}}
onSelect={showView}
/>
{selected && (
<SelectionTag
@@ -208,10 +212,9 @@ function ReadOnlyAnnotations({
viewId={viewId}
idPrefix={READ_ONLY_IDS}
readOnly
onSelectView={(id) => {
setViewId(id)
setSelectedId(null)
}}
filter={filter}
onFilter={setFilter}
onSelectView={showView}
onSelectAnnotation={setSelectedId}
onOpenSection={onOpenSection}
/>
@@ -20,6 +20,8 @@ export interface UiState {
serial: number
tracing: Tracing | null
hint: string | null
/** The overview's « Filtrer par nom » term: it outlives a selection, not a view. */
filter: string
}
export type UiAction =
@@ -33,6 +35,7 @@ export type UiAction =
| { type: 'trace_add'; point: Point }
| { type: 'trace_cancel' }
| { type: 'hint'; hint: string | null }
| { type: 'filter'; term: string }
export const initialUi: UiState = {
viewId: null,
@@ -42,6 +45,7 @@ export const initialUi: UiState = {
serial: 0,
tracing: null,
hint: null,
filter: '',
}
const TOOL_OF: Record<Tracing['target'], ToolId> = {
@@ -108,5 +112,7 @@ export function uiReducer(state: UiState, action: UiAction): UiState {
return { ...state, tracing: null, hint: null }
case 'hint':
return { ...state, hint: action.hint }
case 'filter':
return { ...state, filter: action.term }
}
}
+6 -2
View File
@@ -140,10 +140,14 @@ export function heldElement(): HTMLElement | null {
/**
* After the render that hides parts: `held` (taken before) that left the page or sits in a
* hidden part hands its focus to the shown tab. An element still visible keeps its place.
* hidden part hands its focus to the shown tab; one still shown gets its focus back when
* the commit blurred it (never `body`), and the tab takes it if the element refuses.
*/
export function refocusIfHidden(held: HTMLElement | null): void {
if (held && (!held.isConnected || held.closest('[hidden]'))) focusActiveTab()
if (!held) return
const gone = !held.isConnected || held.closest('[hidden]')
if (!gone && document.activeElement !== held) held.focus()
if (gone || document.activeElement !== held) focusActiveTab()
}
/**
+7 -3
View File
@@ -208,8 +208,9 @@ function applyReload(
}
/**
* « Annuler les modifications » (confirmed): back to the saved text; with `staleBase` the
* server draft is reloaded instead (the saved text is an older revision's).
* « Abandonner les modifications » (confirmed): back to the saved text, which is the save
* `lastSave` describes, so the save line keeps it; with `staleBase` the server draft is
* reloaded instead (the saved text is an older revision's).
*/
export async function discardChanges(
api: StoreApi,
@@ -228,6 +229,9 @@ export async function discardChanges(
: { ok: false, reason: 'blocked', kind: 'not_ready' }
}
removeCopy(state.userId ?? '', state.versionId ?? '')
api.set(reloadPatch(state, state.savedText, replaced.state, state.savedRevision))
api.set({
...reloadPatch(state, state.savedText, replaced.state, state.savedRevision),
lastSave: state.lastSave,
})
return { ok: true }
}
-1
View File
@@ -1356,7 +1356,6 @@ export const en: Dict = {
'editor.general.author_remove': 'Remove {name}',
'editor.general.license': 'Licence',
'editor.general.notes': 'Notes',
'editor.general.categories': 'Annotation categories',
'editor.general.category_name_fr': 'Category name (FR)',
'editor.general.category_name_en': 'Category name (EN)',
'editor.general.category_color': 'Colour',
-1
View File
@@ -1387,7 +1387,6 @@ export const fr: Dict = {
'editor.general.author_remove': 'Retirer {name}',
'editor.general.license': 'Licence',
'editor.general.notes': 'Notes',
'editor.general.categories': 'Catégories d\'annotation',
'editor.general.category_name_fr': 'Nom de la catégorie (FR)',
'editor.general.category_name_en': 'Nom de la catégorie (EN)',
'editor.general.category_color': 'Couleur',