feat(editor): « Citer » button and family labels in references
This commit is contained in:
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)]">
|
||||
|
||||
Reference in new issue
Block a user