feat(editor): « Citer » button and family labels in references

This commit is contained in:
bchanot committed 2026-11-15 19:40:47 +01:00
1 parent 240c2ba0e7
commit 8c3fcca0b9
16 files changed
+507 -53

No files matched your search

@@ -4,7 +4,7 @@ import ActionButton from '@/components/editor/fields/ActionButton'
import { useItemMode } from '@/components/editor/findings/useItemMode'
import Dialog from '@/components/ui/Dialog'
import { useEditorReadOnly } from '@/editor/editingContext'
import type { EditorTarget } from '@/editor/navigation'
import { elementLabel, type EditorTarget } from '@/editor/navigation'
import { findingPath } from '@/editor/references/panel'
import { useSheetDocument } from '@/editor/sheetDocument'
import type { Guards } from '@/editor/useEditorNavigation'
@@ -141,7 +141,7 @@ export default function CitationsDialog(props: CitationsDialogProps) {
open={citedBy !== null}
layer
size="lg"
title={t(title)}
title={t(elementLabel(title, citedBy))}
titleId={CITATIONS_TITLE_ID}
returnFocus={props.returnFocus}
onClose={() => (onClose() ? undefined : 'kept')}
@@ -1,13 +1,27 @@
import { useEffect, useRef } from 'react'
import SelectField from '@/components/editor/fields/SelectField'
import Button from '@/components/ui/Button'
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
import Select from '@/components/ui/Select'
import type { SelectOption } from '@/components/editor/fields/SelectField'
import { useSheetDocument } from '@/editor/sheetDocument'
import { useT } from '@/i18n/useT'
/** The select's choice (Rev 6): local to the form, never in the draft until « Citer ». */
export interface CiteChoice {
/** The placeholder, then the references the draft does not cite yet. */
options: readonly SelectOption[]
/** The chosen id while it is still offered, else '' (the placeholder). */
chosen: string
onChoose: (id: string) => void
/** « Enregistrer » found it chosen but not cited: the line at the select. */
pending: boolean
}
export interface DraftListProps {
/** The draft's cited ids, in the order they will be saved. */
ids: readonly string[]
readOnly: boolean
choice: CiteChoice
onAdd: (id: string) => void
onRemove: (id: string) => void
/** « Modifier la référence »: the reference dialog over this one. */
@@ -19,8 +33,11 @@ export interface DraftListProps {
/** The add choice: its id, so the focus can land on it once the last row is removed. */
const ADD_ID = 'citations-add-reference'
/** « Citer »: its id, where a refused save puts the focus. */
export const CITE_ID = 'citations-cite-reference'
/** The references the draft does not cite yet, after the « choose » placeholder. */
function useCitableOptions(ids: readonly string[]) {
export function useCitableOptions(ids: readonly string[]) {
const t = useT()
const doc = useSheetDocument()
const options = (doc?.references ?? [])
@@ -90,23 +107,64 @@ function DraftRow({ id, props, onRemove }: RowProps) {
)
}
/** « Citer une référence existante » among those not cited yet, and « Nouvelle référence ». */
/**
* « Citer une référence existante »: browsing or picking only chooses; « Citer » cites the
* choice, empties the select and gives it the focus back.
*/
function CiteField({ props }: { props: DraftListProps }) {
const t = useT()
const { choice } = props
const error = choice.pending
? t('editor.citations.pending_choice')
: undefined
const cite = () => {
props.onAdd(choice.chosen)
choice.onChoose('')
document.getElementById(ADD_ID)?.focus()
}
return (
<div className="min-w-0 flex-1">
<FormField
id={ADD_ID}
label={t('editor.findings.reference_add')}
error={error}
>
<div className="flex items-center gap-2">
<Select
id={ADD_ID}
value={choice.chosen}
onChange={(event) => choice.onChoose(event.target.value)}
aria-invalid={choice.pending}
aria-describedby={fieldDescribedBy(ADD_ID, undefined, error)}
className="min-w-0 flex-1"
>
{choice.options.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</Select>
<Button
id={CITE_ID}
type="button"
variant="secondary"
disabled={choice.chosen === ''}
onClick={cite}
>
{t('editor.citations.cite')}
</Button>
</div>
</FormField>
</div>
)
}
/** The way to cite an existing reference (edit tier only), and « Nouvelle référence ». */
function AddRow({ props }: { props: DraftListProps }) {
const t = useT()
const options = useCitableOptions(props.ids)
return (
<div className="flex flex-wrap items-end gap-2">
{!props.readOnly && (
<div className="min-w-0 flex-1">
<SelectField
id={ADD_ID}
label={t('editor.findings.reference_add')}
value=""
options={options}
onCommit={(id) => id !== '' && props.onAdd(id)}
/>
</div>
)}
{!props.readOnly && <CiteField props={props} />}
<Button
type="button"
variant="secondary"
@@ -1,4 +1,4 @@
import { useEffect, useRef } from 'react'
import { useEffect, useRef, useState } from 'react'
import type { ElementKey } from '@faunosteo/core-wasm/types'
import { FormActions } from '@/components/editor/findings/DiscardConfirm'
import ReadOnlyNotice from '@/components/editor/findings/ReadOnlyNotice'
@@ -18,7 +18,10 @@ import { useEditCommand } from '@/editor/useEditCommand'
import type { Guards } from '@/editor/useEditorNavigation'
import type { ReferenceMoves } from '@/editor/useReferenceMoves'
import { useT } from '@/i18n/useT'
import CitationsDraftList from './CitationsDraftList'
import CitationsDraftList, {
CITE_ID,
useCitableOptions,
} from './CitationsDraftList'
export interface CitationsEditFormProps {
citedBy: ElementKey
@@ -83,20 +86,53 @@ function useCreation(props: CitationsEditFormProps, form: Draft) {
})
}
/**
* The select's choice (Rev 6): local, outside the draft (a choice alone is never asked
* about), shown only while the draft still offers it, else the placeholder.
*/
function useCiteChoice(ids: readonly string[]) {
const options = useCitableOptions(ids)
const [state, setState] = useState({ id: '', asked: false })
const chosen = options.some((o) => o.value === state.id) ? state.id : ''
return {
options,
chosen,
pending: state.asked && chosen !== '',
onChoose: (id: string) => setState({ id, asked: false }),
/** A choice waits for « Citer »: true, its line shows and « Citer » takes the focus. */
holdsSave: () => {
if (chosen === '') return false
setState({ id: chosen, asked: true })
document.getElementById(CITE_ID)?.focus()
return true
},
}
}
/** The form's state: the draft, its guard, its save, its cancel and its creation. */
function useCitationsForm(props: CitationsEditFormProps) {
const form = useFormDraft(NO_EDITS, props.guards, sameEdits, 'citations')
const saving = useCitationsSave(props, form)
const choice = useCiteChoice(saving.shown)
const cancel = () =>
form.dirty && !props.readOnly
? form.guard.askToLeave()
: props.onDone(false)
return { ...form, ...saving, cancel, create: useCreation(props, form) }
const submit = () => (choice.holdsSave() ? undefined : saving.save())
return {
...form,
...saving,
choice,
cancel,
submit,
create: useCreation(props, form),
}
}
/**
* The references dialog's edit mode (Rev 5): a draft of the citations (cite, remove, a new
* reference cited), one `update` on « Enregistrer » (none when nothing changed), nothing on
* reference cited), one `update` on « Enregistrer » (none when nothing changed; refused
* while a choice in the select waits for « Citer », Rev 6), nothing on
* « Annuler » or a close. The draft's layer is `citations`: a dirty one is asked before a
* close, a Back or a leave. Read only: the draft stays, only « Abandonner » is left.
*/
@@ -115,13 +151,14 @@ export default function CitationsEditForm(props: CitationsEditFormProps) {
noValidate
onSubmit={(event) => {
event.preventDefault()
form.save()
form.submit()
}}
>
{props.readOnly && <ReadOnlyNotice />}
<CitationsDraftList
ids={form.shown}
readOnly={props.readOnly}
choice={form.choice}
onAdd={(id) => form.set(addId(form.draft, id))}
onRemove={(id) => form.set(removeId(form.draft, id))}
onEditReference={props.references.open}
@@ -1,6 +1,14 @@
import type { LocalizedText, Reference } from '@faunosteo/core-wasm/types'
import type {
ElementKey,
LocalizedText,
Reference,
} from '@faunosteo/core-wasm/types'
import Icon from '@/components/ui/Icon'
import { targetOfKey, type EditorTarget } from '@/editor/navigation'
import {
elementLabel,
targetOfKey,
type EditorTarget,
} from '@/editor/navigation'
import { usePlural } from '@/editor/plural'
import type { CitationEntry } from '@/editor/references/panel'
import { useT } from '@/i18n/useT'
@@ -47,19 +55,22 @@ function ShowAllLink({ onShowAll }: { onShowAll: () => void }) {
)
}
/** « {n} références citées par ce constat » and the way to the whole library. */
/** « {n} références citées par ce constat » (or « cette famille ») and the whole library. */
function Summary({
count,
citedBy,
onShowAll,
}: {
count: number
citedBy: ElementKey
onShowAll: () => void
}) {
const plural = usePlural()
const label = elementLabel('editor.references.panel_count', citedBy)
return (
<div className="flex flex-wrap items-center justify-between gap-x-3">
<p className="text-sm text-[var(--color-ink-muted)]">
{count > 0 && plural('editor.references.panel_count', count)}
{count > 0 && plural(label, count)}
</p>
<ShowAllLink onShowAll={onShowAll} />
</div>
@@ -152,10 +163,10 @@ export default function CitationsPanel(props: Props) {
return (
<>
<FindingPath path={panel.path} />
<Summary count={count} onShowAll={onShowAll} />
<Summary count={count} citedBy={props.citedBy} onShowAll={onShowAll} />
{count === 0 && (
<p className="text-sm text-[var(--color-ink-muted)]">
{t('editor.references.panel_empty')}
{t(elementLabel('editor.references.panel_empty', props.citedBy))}
</p>
)}
<Entries panel={panel} props={props} />
@@ -1,6 +1,7 @@
import type { ElementKey } from '@faunosteo/core-wasm/types'
import type { CiterItem } from '@/editor/references/citations'
import { sameKey } from '@/editor/findings/tree'
import { elementLabel } from '@/editor/navigation'
import { usePlural } from '@/editor/plural'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
@@ -12,7 +13,7 @@ export default function CitersList({
onGoTo,
}: {
citers: readonly CiterItem[]
/** The finding the citations panel belongs to: its row carries « ce constat ». */
/** The panel's own element: its row carries « ce constat » or « cette famille ». */
current?: ElementKey
onGoTo: (key: ElementKey) => void
}) {
@@ -26,6 +27,13 @@ export default function CitersList({
</p>
)
}
// The current family's row says « cette famille » once, without a second badge (Rev 6).
const isCurrent = (citer: CiterItem) =>
current !== undefined && sameKey(citer.key, current)
const ownLabel = elementLabel(
'editor.references.this_finding',
current ?? null,
)
// « Aller à » alone repeats per citer: the name says where each one goes (M6).
const where = (citer: CiterItem) =>
[...citer.trail, citer.name].map((name) => name[locale]).join(' › ')
@@ -43,12 +51,12 @@ export default function CitersList({
{' › '}
</span>
{citer.name[locale]}
{current && sameKey(citer.key, current) && (
{isCurrent(citer) && (
<span className="ml-1 rounded-md bg-[var(--color-surface-strong)] px-2 py-0.5 text-xs">
{t('editor.references.this_finding')}
{t(ownLabel)}
</span>
)}
{citer.isFamily && (
{citer.isFamily && !isCurrent(citer) && (
<span className="ml-1 rounded-md bg-[var(--color-surface-strong)] px-2 py-0.5 text-xs">
{t('editor.references.family')}
</span>
@@ -1,5 +1,7 @@
import type { ElementKey } from '@faunosteo/core-wasm/types'
import Button from '@/components/ui/Button'
import Icon from '@/components/ui/Icon'
import { elementLabel } from '@/editor/navigation'
import { useT } from '@/i18n/useT'
/**
@@ -9,9 +11,12 @@ import { useT } from '@/i18n/useT'
export default function NoResult({
query,
onClear,
filteredBy,
}: {
query: string
onClear: () => void
/** The family or finding the list is filtered by, null for the whole library. */
filteredBy: ElementKey | null
}) {
const t = useT()
// A filtered finding that cites nothing: no search to clear, the chip has the way out.
@@ -21,7 +26,7 @@ export default function NoResult({
data-testid="editor-references-no-result"
className="text-sm text-[var(--color-ink-muted)]"
>
{t('editor.references.panel_empty')}
{t(elementLabel('editor.references.panel_empty', filteredBy))}
</p>
)
}
@@ -1,8 +1,10 @@
import type { KeyboardEvent, RefObject } from 'react'
import type { ElementKey } from '@faunosteo/core-wasm/types'
import ActionButton from '@/components/editor/fields/ActionButton'
import Icon from '@/components/ui/Icon'
import Input from '@/components/ui/Input'
import Select from '@/components/ui/Select'
import { elementLabel } from '@/editor/navigation'
import { usePlural } from '@/editor/plural'
import { REFERENCE_SORTS, type ReferenceSort } from '@/editor/references/sort'
import { useT } from '@/i18n/useT'
@@ -25,9 +27,11 @@ interface Props {
filter: FilterChip | null
}
/** The finding filter: its name, « × » and « Revenir au constat ». */
/** The finding filter: its name, « × » and « Revenir au constat » (family wording for one). */
export interface FilterChip {
name: string
/** The filtering element: a family takes the family wording. */
key: ElementKey
onRemove: () => void
onBack: () => void
}
@@ -39,7 +43,9 @@ const BUTTON =
/** « Références du constat : X » with « × », and the way back to the finding. */
function FilterBar({ filter }: { filter: FilterChip }) {
const t = useT()
const fill = (key: string) => t(key).replace('{name}', () => filter.name)
const label = (key: string) => elementLabel(key, filter.key)
const fill = (key: string) =>
t(label(key)).replace('{name}', () => filter.name)
return (
<div
data-testid="editor-references-filter"
@@ -61,7 +67,7 @@ function FilterBar({ filter }: { filter: FilterChip }) {
onClick={filter.onBack}
className={`${BUTTON} text-sm text-[var(--color-accent-strong)] underline`}
>
{t('editor.references.filter_back')}
{t(label('editor.references.filter_back'))}
</button>
</div>
)
@@ -6,6 +6,7 @@ import {
useFocusRequest,
type FocusRequest,
} from '@/editor/focusRequest'
import { elementLabel } from '@/editor/navigation'
import { citersOf } from '@/editor/references/citations'
import {
citedIds,
@@ -33,7 +34,8 @@ function useFilterFinding(layer: ReferenceLayer) {
const gone = doc !== null && layer.filter !== null && finding === null
// The vanished finding is the trigger; the move and its wording are the latest ones.
const clear = useRef<() => void>(() => undefined)
clear.current = () => layer.clearFilter(t('editor.references.filter_gone'))
const gonePhrase = elementLabel('editor.references.filter_gone', layer.filter)
clear.current = () => layer.clearFilter(t(gonePhrase))
useEffect(() => {
if (gone) clear.current()
}, [gone])
@@ -20,6 +20,7 @@ import {
type FindingKind,
} from '@/editor/findings/tree'
import { useFocusRequest, visibleElement } from '@/editor/focusRequest'
import { elementLabel } from '@/editor/navigation'
import { useEditorStore } from '@/editor/state/editorStore'
import { useSheetDocument } from '@/editor/sheetDocument'
import type { EditorPlaceProps, FindingLayer } from '@/editor/tabs'
@@ -109,9 +110,12 @@ function useGoneRule(layer: FindingLayer, loaded: boolean) {
if (key === null || currentItems().some(present)) return
if (handled.current === stamp) return
handled.current = stamp
// A family's own dialog names the family; a creation keeps the finding wording.
const shown = 'key' in view ? view.key : null
const gone = t(elementLabel('editor.findings.gone', shown))
// After this commit's effects: a tile in edit re-registers its guard in them, and the
// forced close below must still be able to ask it.
queueMicrotask(() => layer.replace(null, t('editor.findings.gone')))
queueMicrotask(() => layer.replace(null, gone))
}, [loaded, view, layer, t])
}
@@ -61,6 +61,7 @@ function useFilterChip(lib: ReferenceLibraryState, layer: ReferenceLayer) {
const key = layer.filter
return {
name: finding.name[locale],
key,
onRemove: () => layer.clearFilter(),
onBack: () => layer.backToFinding(key),
}
@@ -90,7 +91,11 @@ function LibraryNotices(body: BodyProps) {
)}
{body.lib.all.length === 0 && <EmptyLine />}
{body.lib.all.length > 0 && shown.positions.length === 0 && (
<NoResult query={shown.view.query} onClear={body.act.clearSearch} />
<NoResult
query={shown.view.query}
onClear={body.act.clearSearch}
filteredBy={body.props.layer.filter}
/>
)}
{reorderable && shown.positions.length > 0 && (
<p className="text-sm text-[var(--color-ink-muted)]">
+10
View File
@@ -198,6 +198,16 @@ export function isCitable(key: ElementKey): boolean {
return key.kind === 'finding' && key.path.length > 0
}
/** A family: a finding key of one id (read from the key, so a vanished family is one too). */
export function isFamilyKey(key: ElementKey): boolean {
return key.kind === 'finding' && key.path.length === 1
}
/** The label key that names `key`: its `_family` variant for a family (Rev 6). */
export function elementLabel(label: string, key: ElementKey | null): string {
return key && isFamilyKey(key) ? `${label}_family` : label
}
/** Only a citable element has a citations dialog: any other key leaves the state as it is. */
function openCitations(state: NavigationState, citedBy: ElementKey) {
const finding = { key: citedBy, citations: true }
+13
View File
@@ -1229,15 +1229,23 @@ export const en: Dict = {
'editor.annotations.deselected': 'No annotation selected.',
'editor.references.search': 'Search a reference',
'editor.references.dialog_title': 'References of the finding',
'editor.references.dialog_title_family': 'References of the family',
'editor.citations.edit': 'Edit',
'editor.citations.edit_title': 'Edit the references of the finding',
'editor.citations.edit_title_family': 'Edit the references of the family',
'editor.citations.edit_reference': 'Edit the reference',
'editor.citations.new_reference': 'New reference',
'editor.citations.cite': 'Cite',
'editor.citations.pending_choice': 'Reference chosen but not cited yet: choose « Cite » or go back to the empty choice.',
'editor.references.panel_count_one': '{count} reference cited by this finding',
'editor.references.panel_count_other': '{count} references cited by this finding',
'editor.references.panel_count_family_one': '{count} reference cited by this family',
'editor.references.panel_count_family_other': '{count} references cited by this family',
'editor.references.panel_show_all': 'Show all in the References tab',
'editor.references.panel_empty': 'This finding cites no reference.',
'editor.references.panel_empty_family': 'This family cites no reference.',
'editor.references.this_finding': 'this finding',
'editor.references.this_finding_family': 'this family',
'editor.references.missing': 'Reference not found',
'editor.references.cited_one': 'Cited {count} time',
'editor.references.cited_other': 'Cited {count} times',
@@ -1259,9 +1267,13 @@ export const en: Dict = {
'editor.references.added_show': 'Show',
'editor.references.gone': 'This reference no longer exists',
'editor.references.filter_chip': 'References of the finding: {name}',
'editor.references.filter_chip_family': 'References of the family: {name}',
'editor.references.filter_remove': 'Remove the filter of the finding {name}',
'editor.references.filter_remove_family': 'Remove the filter of the family {name}',
'editor.references.filter_back': 'Back to the finding',
'editor.references.filter_back_family': 'Back to the family',
'editor.references.filter_gone': 'The finding no longer exists: the filter is removed',
'editor.references.filter_gone_family': 'The family no longer exists: the filter is removed',
'editor.references.clear_search': 'Clear the search',
'editor.references.sort_by': 'Sort by',
'editor.references.sort.sheet': 'Sheet order',
@@ -1649,6 +1661,7 @@ export const en: Dict = {
'editor.findings.discard': 'Discard',
'editor.findings.draft_dropped': 'Entry discarded',
'editor.findings.gone': 'This finding no longer exists',
'editor.findings.gone_family': 'This family no longer exists',
// Provisional EN, pending the owner's wording.
'editor.leave.draft_dropped':
'An unsaved entry was discarded when you left the editor',
+13
View File
@@ -1260,15 +1260,23 @@ export const fr: Dict = {
'editor.annotations.deselected': 'Aucune annotation sélectionnée.',
'editor.references.search': 'Rechercher une référence',
'editor.references.dialog_title': 'Références du constat',
'editor.references.dialog_title_family': 'Références de la famille',
'editor.citations.edit': 'Modifier',
'editor.citations.edit_title': 'Modifier les références du constat',
'editor.citations.edit_title_family': 'Modifier les références de la famille',
'editor.citations.edit_reference': 'Modifier la référence',
'editor.citations.new_reference': 'Nouvelle référence',
'editor.citations.cite': 'Citer',
'editor.citations.pending_choice': 'Référence choisie mais pas encore citée : choisissez « Citer » ou revenez au choix vide.',
'editor.references.panel_count_one': '{count} référence citée par ce constat',
'editor.references.panel_count_other': '{count} références citées par ce constat',
'editor.references.panel_count_family_one': '{count} référence citée par cette famille',
'editor.references.panel_count_family_other': '{count} références citées par cette famille',
'editor.references.panel_show_all': 'Tout afficher dans l\'onglet Références',
'editor.references.panel_empty': 'Ce constat ne cite aucune référence.',
'editor.references.panel_empty_family': 'Cette famille ne cite aucune référence.',
'editor.references.this_finding': 'ce constat',
'editor.references.this_finding_family': 'cette famille',
'editor.references.missing': 'Référence introuvable',
'editor.references.cited_one': 'Citée {count} fois',
'editor.references.cited_other': 'Citée {count} fois',
@@ -1290,9 +1298,13 @@ export const fr: Dict = {
'editor.references.added_show': 'Afficher',
'editor.references.gone': "Cette référence n'existe plus",
'editor.references.filter_chip': 'Références du constat : {name}',
'editor.references.filter_chip_family': 'Références de la famille : {name}',
'editor.references.filter_remove': 'Retirer le filtre du constat {name}',
'editor.references.filter_remove_family': 'Retirer le filtre de la famille {name}',
'editor.references.filter_back': 'Revenir au constat',
'editor.references.filter_back_family': 'Revenir à la famille',
'editor.references.filter_gone': "Le constat n'existe plus : le filtre est retiré",
'editor.references.filter_gone_family': "La famille n'existe plus : le filtre est retiré",
'editor.references.clear_search': 'Effacer la recherche',
'editor.references.sort_by': 'Trier par',
'editor.references.sort.sheet': 'Ordre de la fiche',
@@ -1679,6 +1691,7 @@ export const fr: Dict = {
'editor.findings.discard': 'Abandonner',
'editor.findings.draft_dropped': 'Saisie abandonnée',
'editor.findings.gone': "Ce constat n'existe plus",
'editor.findings.gone_family': "Cette famille n'existe plus",
'editor.leave.draft_dropped':
"Une saisie non enregistrée a été abandonnée en quittant l'éditeur",
'editor.findings.flip_narrow': 'Élargissez la fenêtre pour poursuivre : votre saisie est conservée.',