feat(editor): search, sort, count and add in the Références tab

Search (citation, DOI, URL) with « Effacer la recherche » and Escape;
« Trier par » sheet order, citation (locale collator), citation count,
type (business order); every sort stable on stored order, display only:
document, stored order, ids and history unchanged (unit + e2e). Count
« {n} références » / « {n} sur {total} » counts matching references
only. « Monter » / « Descendre » only in sheet order without search
(A1). « Ajouter une référence » resets search and sort and appends a
draft tile in edit; a tile in edit stays pinned while sorted or
searched; tile state pruned of absent ids. Reset path ready for « Tout
afficher » (R2.4).

Gates: typecheck, lint, test:run (app 1452), build, e2e:ui 251;
e2e:shell 16 specs on db:sandbox (editor-equine never listed): 115
passed / 102 skipped / 2 failed: editor-structures (test-side, fixed,
rerun with editor-references 12 passed / 11 skipped) and admin.spec A0
« Target crashed » during axe on the rights screen (not R2: that
screen renders every grant, 1 282 accumulated e2e fixtures; reported as
an open risk); full-row guard: no pre-existing row changed in any table.

## R-24 (removed or rewritten assertions)
| file | old | new |
|---|---|---|
| editor-references-tab.test.tsx | `#tab-reference-new-citation` present on load | null until « Ajouter une référence » |
| same | no match text « Aucune référence ne correspond. » | notice « Aucune référence ne contient « zzz ». » |
| same | add: form always visible, add clears the search, `getByText('Nouvelle source')` | click « Ajouter une référence » first; new tile header open, focused, search empty, 4 tiles |
| same | « a refused add keeps the search » | REMOVED (P24: add resets the search at once); replaced by « draft opens with citation focused, refused add keeps the draft, no write » |
| editor-tabs-lists.test.tsx | add form directly | click « Ajouter une référence » before the form assertions (same assertions after) |
| editor-keep-alive.test.tsx | form survives tab round trip | same, after opening the draft |
| editor-no-loss.test.tsx | reference form keeps typed text | same, after opening the draft |
| e2e editor-shell.spec (2), editor-shell-crossing.spec, editor-structures.spec | fill `#tab-reference-new-*` directly; `getByText(citation)` visible | open the draft first; structures asserts the new header is focused |
| e2e editor-references.spec keyboard walk | focus search, Tab reaches header 0 | focus « Ajouter une référence » (search, sort, add precede the headers) |
Other readers of `editor.reference.add`/new ids: grep of src, tests, e2e incl. editor-equine found none left unlisted. editor-equine is not affected ("updated, not run": nothing to update).

## i18n (fr / en)
Added: `editor.references.add` Ajouter une référence / Add a reference; `add_help` Type et citation obligatoires. / Type and citation required.; `clear_search` Effacer la recherche / Clear the search; `sort_by` Trier par / Sort by; `sort.sheet|citation|count|type` Ordre de la fiche, Citation (A → Z), Nombre de citations, Type / Sheet order, Citation (A → Z), Number of citations, Type; `count_one|other` {count} référence(s) / reference(s); `count_of_one|other` {count} référence(s) sur {total} / {count} reference(s) of {total}; `reorder_hint` Pour réorganiser, affichez l'ordre de la fiche sans recherche. / To reorder, show the sheet order without a search.; `no_result` Aucune référence ne contient « {q} ». / No reference contains “{q}”.; `no_result_scope` La recherche porte sur la citation, le DOI et l'URL. / The search covers the citation, the DOI and the URL. (REVUE sentence split in two keys so the first half is bold).
Changed: none. Removed: none (`editor.references.no_match` kept, the panel's EmptyLine still uses it).
This commit is contained in:
bchanot committed 2026-11-12 21:18:56 +01:00
1 parent c21995b3fb
commit 3baac4dbb5
26 files changed
+1544 -219

No files matched your search

@@ -0,0 +1,97 @@
import { useEffect, useState, type RefObject } from 'react'
import type { ElementKey, Reference } from '@faunosteo/core-wasm/types'
import {
focusRow,
useFocusRequest,
type FocusRequest,
} from '@/editor/focusRequest'
import { citersOf } from '@/editor/references/citations'
import { referenceMatches } from '@/editor/references/filter'
import { useSheetDocument } from '@/editor/sheetDocument'
import { tileKeys, useTileState } from './useTileState'
import { useReferenceView } from './useReferenceView'
const NO_CITERS = new Map<string, never[]>()
/** The document's references with the tab's tile state and display order. */
export function useReferenceLibrary(readOnly: boolean, resetSignal: number) {
const doc = useSheetDocument()
const all: readonly Reference[] = doc?.references ?? []
const keys = tileKeys(all)
const tiles = useTileState(doc ? keys : null)
const citers = doc ? citersOf(doc) : NO_CITERS
const editedAt = readOnly ? -1 : keys.indexOf(tiles.editingKey ?? '')
const shown = useReferenceView(all, citers, editedAt, resetSignal)
return { loaded: doc !== null, all, keys, tiles, citers, shown }
}
export type ReferenceLibraryState = ReturnType<typeof useReferenceLibrary>
/** Focuses a row after the commit that shows it (the request ran before it rendered). */
function useLateFocus() {
const [late, setLate] = useState<ElementKey | null>(null)
useEffect(() => {
if (late === null) return
focusRow(late)
setLate(null)
}, [late])
return setLate
}
const positionOf = (all: readonly Reference[], key: ElementKey) =>
all.findIndex((r) => key.kind === 'reference' && r.id === key.id)
/**
* « Aller à » a reference: its tile is opened, the search is cleared (only it, P3) when it
* hides the tile, and the tile's button is focused after the commit that shows it (P8).
*/
function useReferenceFocus(
focus: FocusRequest | undefined,
lib: ReferenceLibraryState,
later: (key: ElementKey) => void,
) {
const { all, keys, tiles, shown } = lib
useFocusRequest(focus, (target) => {
const at = target.key === null ? -1 : positionOf(all, target.key)
if (target.key === null || at < 0) return 'none'
tiles.reveal(keys[at])
if (referenceMatches(all[at], shown.view.query)) {
return focusRow(target.key) ? 'found' : 'none'
}
shown.update({ query: '' })
later(target.key)
return 'found'
})
}
/** What the library's controls do: search, « Ajouter une référence », edit and « Aller à ». */
export function useReferenceActions(
lib: ReferenceLibraryState,
focus: FocusRequest | undefined,
searchRef: RefObject<HTMLInputElement | null>,
) {
const { keys, tiles, shown } = lib
const [draft, setDraft] = useState<number | null>(null)
const later = useLateFocus()
useReferenceFocus(focus, lib, later)
return {
draft,
clearSearch: () => {
shown.update({ query: '' })
searchRef.current?.focus()
},
// P24 (and P22): leave the tile in edit first, then a clean view and the draft.
startAdd: () => {
if (!tiles.finish()) return
shown.reset()
setDraft((serial) => (serial ?? 0) + 1)
},
added: (id: string) => {
setDraft(null)
tiles.reveal(`${id}#0`)
later({ kind: 'reference', id })
},
edit: (position: number) => {
if (tiles.startEdit(keys[position])) shown.markEdited(position)
},
}
}